-
Notifications
You must be signed in to change notification settings - Fork 0
feat: added support for dynamic theme colors #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,9 @@ import androidx.core.content.ContextCompat | |
| import androidx.core.view.isVisible | ||
| import androidx.lifecycle.lifecycleScope | ||
| import kotlinx.coroutines.flow.first | ||
| import com.google.android.material.color.DynamicColors | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.runBlocking | ||
| import net.ardevd.tagius.core.data.TokenManager | ||
| import net.ardevd.tagius.databinding.ActivityMainBinding | ||
| import net.ardevd.tagius.features.auth.ui.LoginFragment | ||
|
|
@@ -72,6 +74,14 @@ class MainActivity : AppCompatActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| // Dynamic color (if set) | ||
| runBlocking { | ||
| val tokenManager = TokenManager(applicationContext) | ||
| val useDynamicColors = tokenManager.dynamicColorsFlow.first() | ||
| if (useDynamicColors) { | ||
| DynamicColors.applyToActivitiesIfAvailable(application) | ||
| } | ||
| } | ||
|
Comment on lines
+77
to
+84
|
||
| binding = ActivityMainBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,21 +19,25 @@ class SettingsBottomSheet( | |||||||||||||||||||||||||
| private var _binding: FragmentSettingsBinding? = null | ||||||||||||||||||||||||||
| private val binding get() = _binding!! | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { | ||||||||||||||||||||||||||
| override fun onCreateView( | ||||||||||||||||||||||||||
| inflater: LayoutInflater, | ||||||||||||||||||||||||||
| container: ViewGroup?, | ||||||||||||||||||||||||||
| savedInstanceState: Bundle? | ||||||||||||||||||||||||||
| ): View { | ||||||||||||||||||||||||||
| _binding = FragmentSettingsBinding.inflate(inflater, container, false) | ||||||||||||||||||||||||||
| return binding.root | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||||||||||||||||||||||||||
| super.onViewCreated(view, savedInstanceState) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| val tokenManager = TokenManager(requireContext()) | ||||||||||||||||||||||||||
| // Show version info | ||||||||||||||||||||||||||
| val version = BuildConfig.VERSION_NAME | ||||||||||||||||||||||||||
| binding.versionText.text = "v$version" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Display the stored URL so the user knows which server they are on | ||||||||||||||||||||||||||
| viewLifecycleOwner.lifecycleScope.launch { | ||||||||||||||||||||||||||
| val url = TokenManager(requireContext()).serverUrlFlow.first() | ||||||||||||||||||||||||||
| val url = tokenManager.serverUrlFlow.first() | ||||||||||||||||||||||||||
| binding.serverUrlText.text = url | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -42,6 +46,18 @@ class SettingsBottomSheet( | |||||||||||||||||||||||||
| dismiss() | ||||||||||||||||||||||||||
| onLogout() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // System Color setting handling | ||||||||||||||||||||||||||
| viewLifecycleOwner.lifecycleScope.launch { | ||||||||||||||||||||||||||
| val useDynamicColors = tokenManager.dynamicColorsFlow.first() | ||||||||||||||||||||||||||
| binding.dynamicColorsSwitch.isChecked = useDynamicColors | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| binding.dynamicColorsSwitch.setOnCheckedChangeListener { _, isChecked -> | ||||||||||||||||||||||||||
| viewLifecycleOwner.lifecycleScope.launch { | ||||||||||||||||||||||||||
| tokenManager.saveDynamicColors(isChecked) | ||||||||||||||||||||||||||
|
Comment on lines
+53
to
+58
|
||||||||||||||||||||||||||
| binding.dynamicColorsSwitch.isChecked = useDynamicColors | |
| } | |
| binding.dynamicColorsSwitch.setOnCheckedChangeListener { _, isChecked -> | |
| viewLifecycleOwner.lifecycleScope.launch { | |
| tokenManager.saveDynamicColors(isChecked) | |
| binding.dynamicColorsSwitch.setOnCheckedChangeListener(null) | |
| binding.dynamicColorsSwitch.isChecked = useDynamicColors | |
| binding.dynamicColorsSwitch.setOnCheckedChangeListener { _, isChecked -> | |
| viewLifecycleOwner.lifecycleScope.launch { | |
| tokenManager.saveDynamicColors(isChecked) | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -58,6 +58,18 @@ | |||||||
| </LinearLayout> | ||||||||
| </com.google.android.material.card.MaterialCardView> | ||||||||
|
|
||||||||
| <com.google.android.material.materialswitch.MaterialSwitch | ||||||||
| android:id="@+id/dynamicColorsSwitch" | ||||||||
| android:layout_width="0dp" | ||||||||
| android:layout_height="wrap_content" | ||||||||
| android:text="@string/settings_dynamic_colors" | ||||||||
| android:textAppearance="?attr/textAppearanceLabelLarge" | ||||||||
| android:layout_marginTop="16dp" | ||||||||
| android:layout_marginStart="16dp" | ||||||||
| android:layout_marginEnd="24dp" | ||||||||
| app:layout_constraintTop_toBottomOf="@id/serverCard" | ||||||||
| app:layout_constraintStart_toStartOf="@id/serverCard" /> | ||||||||
|
||||||||
| app:layout_constraintStart_toStartOf="@id/serverCard" /> | |
| app:layout_constraintStart_toStartOf="@id/serverCard" | |
| app:layout_constraintEnd_toEndOf="@id/serverCard" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DynamicColors.applyToActivitiesIfAvailable(application)registers lifecycle callbacks and is typically intended to be called fromApplication.onCreate()(before any Activity is created). Calling it insideMainActivity.onCreate()is likely too late for the current activity, so dynamic colors may not be applied at all in a single-activity app. Prefer applying dynamic colors in anApplicationsubclass, or use the activity-scoped API (e.g., apply to the current activity) so this activity actually gets the dynamic theme.