diff --git a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/MainActivity.kt b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/MainActivity.kt index dcc257d..86969aa 100644 --- a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/MainActivity.kt +++ b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/MainActivity.kt @@ -18,7 +18,7 @@ import androidx.fragment.app.FragmentManager import com.eurekateam.fmradio.enums.HeadsetState import com.eurekateam.fmradio.enums.PowerState import com.eurekateam.fmradio.fragments.ChannelListFragment -import com.eurekateam.fmradio.fragments.FavouriteFragment +import com.eurekateam.fmradio.fragments.FavoriteFragment import com.eurekateam.fmradio.fragments.MainFragment import com.eurekateam.fmradio.utils.Log import com.google.android.material.bottomnavigation.BottomNavigationView @@ -103,14 +103,14 @@ class MainActivity : AppCompatActivity() { registerReceiver(mWiredHeadsetReceiver, receiverFilter) val mRadioMainFragment = MainFragment() val mRadioChannelListFragment = ChannelListFragment() - val mFavouriteFragment = FavouriteFragment() + val mFavoriteFragment = FavoriteFragment() changeFragment(mRadioMainFragment, MainFragment::class.java.name) mIntent = Intent(this, FMRadioService::class.java) findViewById(R.id.bottom_nav_bar).setOnItemSelectedListener { when (it.itemId) { R.id.radio_main -> changeFragment(mRadioMainFragment, MainFragment::class.java.name) R.id.channel_list -> changeFragment(mRadioChannelListFragment, ChannelListFragment::class.java.name) - R.id.fav_list -> changeFragment(mFavouriteFragment, FavouriteFragment::class.java.name) + R.id.fav_list -> changeFragment(mFavoriteFragment, FavoriteFragment::class.java.name) } true } diff --git a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/ChannelListFragment.kt b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/ChannelListFragment.kt index e6ab219..f9be422 100644 --- a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/ChannelListFragment.kt +++ b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/ChannelListFragment.kt @@ -1,7 +1,6 @@ package com.eurekateam.fmradio.fragments import android.app.ProgressDialog -import android.content.res.Configuration import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -14,6 +13,7 @@ import com.eurekateam.fmradio.NativeFMInterface import com.eurekateam.fmradio.R import com.eurekateam.fmradio.MainActivity import com.eurekateam.fmradio.adapters.ListViewAdapter +import com.eurekateam.fmradio.utils.MaterialHelper import com.eurekateam.fmradio.utils.IWaitUntil import com.eurekateam.fmradio.utils.WaitUntil import com.google.android.material.floatingactionbutton.FloatingActionButton @@ -37,21 +37,13 @@ class ChannelListFragment : mFloatingActionButton = mRootView.findViewById(R.id.refresh_channel_list) mFloatingActionButton.setOnClickListener(this) mListView.adapter = ListViewAdapter(requireContext()) - var mIsLight = true - val nightModeFlags = requireContext().resources.configuration.uiMode and - Configuration.UI_MODE_NIGHT_MASK - when (nightModeFlags) { - Configuration.UI_MODE_NIGHT_YES -> mIsLight = false - Configuration.UI_MODE_NIGHT_NO -> mIsLight = true - Configuration.UI_MODE_NIGHT_UNDEFINED -> mIsLight = true - } - mRootView.findViewById(R.id.channel_list_layout).apply { - if (mIsLight) { - setBackgroundColor(resources.getColor(android.R.color.system_accent1_50, requireContext().theme)) - } else { - setBackgroundColor(resources.getColor(android.R.color.system_accent1_100, requireContext().theme)) - } - } + mRootView.findViewById(R.id.channel_list_layout).setBackgroundColor( + MaterialHelper.build { + context = requireContext() + light = android.R.color.system_accent1_50 + dark = android.R.color.system_accent1_100 + }.getValue() + ) return mRootView } diff --git a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/FavoriteFragment.kt b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/FavoriteFragment.kt new file mode 100644 index 0000000..b591e82 --- /dev/null +++ b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/FavoriteFragment.kt @@ -0,0 +1,31 @@ +package com.eurekateam.fmradio.fragments + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.GridView +import androidx.fragment.app.Fragment +import com.eurekateam.fmradio.R +import com.eurekateam.fmradio.utils.MaterialHelper +import com.eurekateam.fmradio.adapters.PebbleLayoutAdapter + +class FavoriteFragment : Fragment(R.layout.fragment_fav_list) { + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + val mRootView = inflater.inflate(R.layout.fragment_fav_list, container, false) + val mGridView = mRootView.findViewById(R.id.fav_list_grid) + mGridView.adapter = PebbleLayoutAdapter(requireContext()) + mRootView.findViewById(R.id.fav_list_grid).setBackgroundColor( + MaterialHelper.build { + context = requireContext() + light = android.R.color.system_accent1_100 + dark = android.R.color.system_accent1_50 + }.getValue() + ) + return mRootView + } +} diff --git a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/FavouriteFragment.kt b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/FavouriteFragment.kt deleted file mode 100644 index 4bcec5c..0000000 --- a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/FavouriteFragment.kt +++ /dev/null @@ -1,39 +0,0 @@ -package com.eurekateam.fmradio.fragments - -import android.content.res.Configuration -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.GridView -import androidx.fragment.app.Fragment -import com.eurekateam.fmradio.R -import com.eurekateam.fmradio.adapters.PebbleLayoutAdapter - -class FavouriteFragment : Fragment(R.layout.fragment_fav_list) { - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - val mRootView = inflater.inflate(R.layout.fragment_fav_list, container, false) - val mGridView = mRootView.findViewById(R.id.fav_list_grid) - mGridView.adapter = PebbleLayoutAdapter(requireContext()) - var mIsLight = true - val nightModeFlags = requireContext().resources.configuration.uiMode and - Configuration.UI_MODE_NIGHT_MASK - when (nightModeFlags) { - Configuration.UI_MODE_NIGHT_YES -> mIsLight = false - Configuration.UI_MODE_NIGHT_NO -> mIsLight = true - Configuration.UI_MODE_NIGHT_UNDEFINED -> mIsLight = true - } - mRootView.findViewById(R.id.fav_list_grid).apply { - if (mIsLight) { - setBackgroundColor(resources.getColor(android.R.color.system_accent1_50, requireContext().theme)) - } else { - setBackgroundColor(resources.getColor(android.R.color.system_accent1_100, requireContext().theme)) - } - } - return mRootView - } -} diff --git a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/MainFragment.kt b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/MainFragment.kt index 6bf0924..27e315e 100644 --- a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/MainFragment.kt +++ b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/fragments/MainFragment.kt @@ -26,6 +26,7 @@ import com.eurekateam.fmradio.enums.HeadsetState import com.eurekateam.fmradio.enums.PowerState import com.eurekateam.fmradio.utils.IWaitUntil import com.eurekateam.fmradio.utils.Log +import com.eurekateam.fmradio.utils.MaterialHelper import com.eurekateam.fmradio.utils.WaitUntil import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.textview.MaterialTextView @@ -90,29 +91,21 @@ class MainFragment : mOutputSwitch.setOnClickListener(this) mFavButton.setOnClickListener(this) mSharedPref = PreferenceManager.getDefaultSharedPreferences(requireContext()) - var mIsLight = true - val nightModeFlags = requireContext().resources.configuration.uiMode and - Configuration.UI_MODE_NIGHT_MASK - when (nightModeFlags) { - Configuration.UI_MODE_NIGHT_YES -> mIsLight = false - Configuration.UI_MODE_NIGHT_NO -> mIsLight = true - Configuration.UI_MODE_NIGHT_UNDEFINED -> mIsLight = true - } for (mResID in listOf(R.id.app_banner, R.id.fm_freq, R.id.freq_misc)) { mRootView.findViewById(mResID).apply { - if (mIsLight) { - setTextColor(resources.getColor(android.R.color.system_accent2_500, requireContext().theme)) - } else { - setTextColor(resources.getColor(android.R.color.system_accent2_100, requireContext().theme)) - } + setTextColor(MaterialHelper.build { + context = requireContext() + light = android.R.color.system_accent2_500 + dark = android.R.color.system_accent2_100 + }.getValue()) } } mRootView.findViewById(R.id.main_fragment).apply { - if (mIsLight) { - setBackgroundColor(resources.getColor(android.R.color.system_accent1_100, requireContext().theme)) - } else { - setBackgroundColor(resources.getColor(android.R.color.system_accent1_700, requireContext().theme)) - } + setBackgroundColor(MaterialHelper.build { + context = requireContext() + light = android.R.color.system_accent1_100 + dark = android.R.color.system_accent1_700 + }.getValue()) } val mVolume = mSharedPref.getInt(SharedPreferencesConst.PREF_VOLUME, 8) mFMInterface.mDevCtl.setValue(SetType.SET_TYPE_FM_VOLUME, mVolume) @@ -320,7 +313,7 @@ class MainFragment : * @return Long array with zeros removed */ private fun removeZeros(array: IntArray): IntArray { - val mArray: MutableList = emptyMutableList() + val mArray: MutableList = mutableListOf() Log.d("removeZeros: Got array size ${array.size}") for (i in array.indices) { if (array[i] > 0L) { diff --git a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/utils/MaterialHelper.kt b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/utils/MaterialHelper.kt new file mode 100644 index 0000000..6e67a6a --- /dev/null +++ b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/utils/MaterialHelper.kt @@ -0,0 +1,29 @@ +package com.eurekateam.fmradio.utils + +import android.content.Context +import android.content.res.Configuration + +class MaterialHelper (private val mContext : Context, private val mOnLight : Int, private val mOnDark : Int) +{ + private constructor(builder: Builder) : this(builder.context, builder.light, builder.dark) + + class Builder { + lateinit var context: Context + var light = 0 + var dark = 0 + + fun build() = MaterialHelper(this) + } + + companion object { + inline fun build(block: Builder.() -> Unit) = Builder().apply(block).build() + } + + fun getValue() : Int = + when (mContext.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { + Configuration.UI_MODE_NIGHT_YES -> mContext.resources.getColor(mOnLight, mContext.theme) + Configuration.UI_MODE_NIGHT_NO -> mContext.resources.getColor(mOnDark, mContext.theme) + Configuration.UI_MODE_NIGHT_UNDEFINED -> mContext.resources.getColor(mOnLight, mContext.theme) + else -> 0 + } +}