From af4b5e74ee573d83d3144d049895946fa5c2a3a3 Mon Sep 17 00:00:00 2001 From: Prem Nirmal Date: Fri, 4 Feb 2022 12:00:37 +0000 Subject: [PATCH] Broadcast autosort and update nav bar colour --- .../ticker/portfolio/PortfolioFragment.kt | 16 +++++++++++----- .../ticker/settings/SettingsFragment.kt | 5 +++++ .../ticker/settings/WidgetSettingsFragment.kt | 5 +++++ .../premnirmal/ticker/widget/WidgetData.kt | 7 +++++++ app/src/main/res/values-night/colors.xml | 2 +- app/version.properties | 4 ++-- 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/portfolio/PortfolioFragment.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/portfolio/PortfolioFragment.kt index b0fe8ca5..b00aa841 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/portfolio/PortfolioFragment.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/portfolio/PortfolioFragment.kt @@ -10,6 +10,7 @@ import android.view.ViewGroup import android.widget.PopupMenu import androidx.appcompat.app.AlertDialog import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.ItemTouchHelper import com.github.premnirmal.ticker.analytics.ClickEvent import com.github.premnirmal.ticker.base.BaseFragment @@ -24,6 +25,7 @@ import com.github.premnirmal.ticker.ui.SpacingDecoration import com.github.premnirmal.tickerwidget.R import kotlinx.android.synthetic.main.fragment_portfolio.stockList import kotlinx.android.synthetic.main.fragment_portfolio.view_flipper +import kotlinx.coroutines.launch /** * Created by premnirmal on 2/25/16. @@ -137,6 +139,11 @@ class PortfolioFragment : BaseFragment(), ChildFragment, QuoteClickListener, OnS viewModel.portfolio.observe(viewLifecycleOwner) { update() } + lifecycleScope.launch { + widgetData.autoSortEnabled.collect { + update() + } + } } override fun onResume() { @@ -175,17 +182,16 @@ class PortfolioFragment : BaseFragment(), ChildFragment, QuoteClickListener, OnS parent.onDragStarted() val widgetData = viewModel.dataForWidgetId(widgetId) if (widgetData.autoSortEnabled()) { - widgetData.setAutoSort(false) - update() - viewModel.broadcastUpdateWidget(widgetId) InAppMessage.showMessage(requireActivity(), R.string.autosort_disabled) - } else { - itemTouchHelper?.startDrag(viewHolder) } + itemTouchHelper?.startDrag(viewHolder) } override fun onStopDrag() { parent.onDragEnded() + val widgetData = viewModel.dataForWidgetId(widgetId) + widgetData.setAutoSort(false) + viewModel.broadcastUpdateWidget(widgetId) } override fun setData(bundle: Bundle) { diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/settings/SettingsFragment.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/settings/SettingsFragment.kt index e96ff96b..a68b9a23 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/settings/SettingsFragment.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/settings/SettingsFragment.kt @@ -198,6 +198,11 @@ class SettingsFragment : PreferenceFragmentCompat(), ChildFragment, widgetData.setAutoSort(newValue as Boolean) true } + lifecycleScope.launch { + widgetData.autoSortEnabled.collect { + autoSortPref.isChecked = it + } + } } run { diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/settings/WidgetSettingsFragment.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/settings/WidgetSettingsFragment.kt index 3b62768c..f66c75b2 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/settings/WidgetSettingsFragment.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/settings/WidgetSettingsFragment.kt @@ -134,6 +134,11 @@ class WidgetSettingsFragment : BaseFragment(), ChildFragment, OnClickListener { updatePreview(widgetData) } } + lifecycleScope.launch { + widgetData.autoSortEnabled.collect { + setting_autosort_checkbox.isChecked = it + } + } } override fun onClick(v: View) { diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/widget/WidgetData.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/widget/WidgetData.kt index a6bd9227..249bc77a 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/widget/WidgetData.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/widget/WidgetData.kt @@ -14,6 +14,8 @@ import com.github.premnirmal.ticker.components.Injector import com.github.premnirmal.ticker.model.IStocksProvider import com.github.premnirmal.ticker.network.data.Quote import com.github.premnirmal.tickerwidget.R +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow import javax.inject.Inject class WidgetData { @@ -52,6 +54,9 @@ class WidgetData { val widgetId: Int private val tickerList: MutableList private val preferences: SharedPreferences + private val _autoSortEnabled = MutableStateFlow(false) + val autoSortEnabled: StateFlow + get() = _autoSortEnabled constructor( position: Int, @@ -73,6 +78,7 @@ class WidgetData { ) } save() + _autoSortEnabled.value = autoSortEnabled() } constructor( @@ -228,6 +234,7 @@ class WidgetData { preferences.edit() .putBoolean(AUTOSORT, autoSort) .apply() + _autoSortEnabled.value = autoSort } fun hideHeader(): Boolean = preferences.getBoolean(HIDE_HEADER, false) diff --git a/app/src/main/res/values-night/colors.xml b/app/src/main/res/values-night/colors.xml index c30443ff..a2435501 100644 --- a/app/src/main/res/values-night/colors.xml +++ b/app/src/main/res/values-night/colors.xml @@ -20,7 +20,7 @@ #009900 #98545454 #ffbdbdbd - #2f2f2f + @color/default_bg @color/card_bg @color/card_bg #ffffff diff --git a/app/version.properties b/app/version.properties index 64259288..97e5b061 100644 --- a/app/version.properties +++ b/app/version.properties @@ -1,2 +1,2 @@ -versionName=3.9.766 -versionCode=300900766 \ No newline at end of file +versionName=3.9.767 +versionCode=300900767 \ No newline at end of file