|
| 1 | +/* |
| 2 | + * Nextcloud Notes - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + */ |
| 7 | +package it.niedermann.owncloud.notes.branding |
| 8 | + |
| 9 | +import android.os.Bundle |
| 10 | +import android.util.TypedValue |
| 11 | +import android.view.Menu |
| 12 | +import android.view.MenuInflater |
| 13 | +import androidx.annotation.ColorInt |
| 14 | +import androidx.appcompat.app.AppCompatActivity |
| 15 | +import androidx.core.view.forEach |
| 16 | +import androidx.fragment.app.Fragment |
| 17 | +import androidx.lifecycle.lifecycleScope |
| 18 | +import com.nextcloud.android.common.ui.util.extensions.adjustUIForAPILevel35 |
| 19 | +import kotlinx.coroutines.Dispatchers |
| 20 | +import kotlinx.coroutines.launch |
| 21 | + |
| 22 | +/** |
| 23 | + * An abstract base [Fragment] implementation that provides common branding support for UI |
| 24 | + * components. |
| 25 | + * |
| 26 | + * This class reads and applies brand-specific colors (`colorPrimary`, `colorAccent`, etc.) when the |
| 27 | + * fragment starts, and adjusts UI elements such as toolbar menu icons accordingly. |
| 28 | + * |
| 29 | + * Subclasses can extend this to inherit branding behavior while implementing their specific logic. |
| 30 | + * |
| 31 | + * @see BrandingUtil for brand color resolution and application. |
| 32 | + * @see Branded for the interface definition related to branding behavior. |
| 33 | + */ |
| 34 | +abstract class BrandedFragment : Fragment(), Branded { |
| 35 | + @JvmField |
| 36 | + @ColorInt |
| 37 | + protected var colorAccent: Int = 0 |
| 38 | + |
| 39 | + @JvmField |
| 40 | + @ColorInt |
| 41 | + protected var colorPrimary: Int = 0 |
| 42 | + |
| 43 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 44 | + if (activity is AppCompatActivity) { |
| 45 | + val appCompatActivity = activity as AppCompatActivity |
| 46 | + appCompatActivity.adjustUIForAPILevel35() |
| 47 | + } |
| 48 | + super.onCreate(savedInstanceState) |
| 49 | + } |
| 50 | + |
| 51 | + override fun onStart() { |
| 52 | + super.onStart() |
| 53 | + |
| 54 | + val context = requireContext() |
| 55 | + val typedValue = TypedValue() |
| 56 | + |
| 57 | + context.theme.resolveAttribute( |
| 58 | + com.google.android.material.R.attr.colorAccent, |
| 59 | + typedValue, |
| 60 | + true |
| 61 | + ) |
| 62 | + colorAccent = typedValue.data |
| 63 | + |
| 64 | + context.theme.resolveAttribute( |
| 65 | + com.google.android.material.R.attr.colorPrimary, |
| 66 | + typedValue, |
| 67 | + true |
| 68 | + ) |
| 69 | + colorPrimary = typedValue.data |
| 70 | + |
| 71 | + @ColorInt |
| 72 | + val color = BrandingUtil.readBrandMainColor(context) |
| 73 | + applyBrand(color) |
| 74 | + } |
| 75 | + |
| 76 | + @Suppress("DEPRECATION") |
| 77 | + @Deprecated("Deprecated in Java") |
| 78 | + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { |
| 79 | + super.onCreateOptionsMenu(menu, inflater) |
| 80 | + val utils = BrandingUtil.of(colorAccent, requireContext()) |
| 81 | + |
| 82 | + menu.forEach { menu -> |
| 83 | + menu.icon?.let { icon -> |
| 84 | + utils.platform.colorToolbarMenuIcon(requireContext(), menu) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Launches the given [block] of code in the [Dispatchers.IO] context using the [lifecycleScope]. |
| 91 | + * |
| 92 | + * This is useful for running long-running or blocking operations (e.g., file or network I/O) |
| 93 | + * that should not block the main thread. The coroutine will be automatically canceled when |
| 94 | + * the lifecycle is destroyed. |
| 95 | + * |
| 96 | + * @param block The code block to be executed on the IO dispatcher. |
| 97 | + */ |
| 98 | + fun lifecycleScopeIOJob(block: () -> Unit) { |
| 99 | + lifecycleScope.launch(Dispatchers.IO) { |
| 100 | + block() |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Executes the given [block] on the main (UI) thread. |
| 106 | + * |
| 107 | + * This is typically used to perform UI-related tasks such as updating views from a background |
| 108 | + * thread. Requires [activity] to be non-null; otherwise, the block will not be executed. |
| 109 | + * |
| 110 | + * @param block The code block to be executed on the main thread. |
| 111 | + */ |
| 112 | + fun onMainThread(block: () -> Unit) { |
| 113 | + activity?.runOnUiThread { |
| 114 | + block() |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments