From c1d14f93bfeaab4a539e875d3eca4c497126003a Mon Sep 17 00:00:00 2001 From: Tommaso Berlose Date: Fri, 8 Jan 2021 17:49:12 +0100 Subject: [PATCH] Improve performances --- .../ui/fragments/MainFragment.kt | 193 ++++++------------ .../ui/fragments/tabs/CalendarFragment.kt | 5 +- .../ui/viewmodels/MainViewModel.kt | 47 ++++- app/src/main/res/layout/fragment_app_main.xml | 5 +- app/src/main/res/layout/the_widget_sans.xml | 3 +- app/src/main/res/values/strings.xml | 4 +- app/src/main/res/values/styles.xml | 1 + 7 files changed, 119 insertions(+), 139 deletions(-) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/MainFragment.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/MainFragment.kt index 7f32ba2..41ba0b2 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/MainFragment.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/MainFragment.kt @@ -48,7 +48,7 @@ import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode -class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeListener { +class MainFragment : Fragment() { companion object { fun newInstance() = MainFragment() @@ -77,22 +77,16 @@ class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - // Init clock - if (Preferences.showClock) { - binding.widgetDetail.time.setTextColor(ColorHelper.getClockFontColor(requireActivity().isDarkTheme())) - binding.widgetDetail.time.setTextSize(TypedValue.COMPLEX_UNIT_SP, - Preferences.clockTextSize.toPixel(requireContext())) - binding.widgetDetail.timeAmPm.setTextColor(ColorHelper.getClockFontColor(requireActivity().isDarkTheme())) - binding.widgetDetail.timeAmPm.setTextSize(TypedValue.COMPLEX_UNIT_SP, - Preferences.clockTextSize.toPixel(requireContext()) / 5 * 2) - } - binding.widgetDetail.timeContainer.isVisible = Preferences.showClock - - binding.preview.layoutParams = binding.preview.layoutParams.apply { - height = PREVIEW_BASE_HEIGHT.toPixel(requireContext()) + if (Preferences.showClock) 100.toPixel(requireContext()) else 0 - } subscribeUi(viewModel) + if (binding.preview.height == 0) { + binding.preview.layoutParams = binding.preview.layoutParams.apply { + height = PREVIEW_BASE_HEIGHT.toPixel(requireContext()) + if (Preferences.showClock) 100.toPixel( + requireContext() + ) else 0 + } + } + // Warnings if (getString(R.string.xiaomi_manufacturer).equals(Build.MANUFACTURER, ignoreCase = true) && Preferences.showXiaomiWarning) { MaterialBottomSheetDialog(requireContext(), getString(R.string.xiaomi_warning_title), getString(R.string.xiaomi_warning_message)) @@ -130,12 +124,8 @@ class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList private var uiJob: Job? = null private fun updateUI() { - Log.d("ciao", "UPDATE UI") uiJob?.cancel() - binding.preview.clearAnimation() - binding.widgetDetail.timeContainer.clearAnimation() - if (Preferences.showPreview) { lifecycleScope.launch(Dispatchers.IO) { val bgColor: Int = ContextCompat.getColor( @@ -156,6 +146,7 @@ class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList binding.widgetDetail.widgetShapeBackground.setImageDrawable(wallpaperDrawable) } } + WidgetHelper.runWithCustomTypeface(requireContext()) { typeface -> uiJob = lifecycleScope.launch(Dispatchers.IO) { val generatedView = MainWidget.generateWidgetView(requireContext(), typeface).root @@ -170,110 +161,18 @@ class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList if (binding.preview.width > 0) binding.preview.width else generatedView.measuredWidth, generatedView.measuredHeight ) + withContext(Dispatchers.Main) { - // Clock - binding.widgetDetail.time.setTextColor(ColorHelper.getClockFontColor(requireActivity().isDarkTheme())) - binding.widgetDetail.timeAmPm.setTextColor(ColorHelper.getClockFontColor(requireActivity().isDarkTheme())) - binding.widgetDetail.time.setTextSize( - TypedValue.COMPLEX_UNIT_SP, - Preferences.clockTextSize.toPixel(requireContext()) - ) - binding.widgetDetail.timeAmPm.setTextSize( - TypedValue.COMPLEX_UNIT_SP, - Preferences.clockTextSize.toPixel(requireContext()) / 5 * 2 - ) - binding.widgetDetail.timeAmPm.isVisible = Preferences.showAMPMIndicator + binding.widget.animate().alpha(0f).start() + binding.widgetLoader.animate().scaleX(1f).scaleY(1f).alpha(1f) - // Clock bottom margin - binding.widgetDetail.clockBottomMarginNone.isVisible = - Preferences.showClock && Preferences.clockBottomMargin == Constants.ClockBottomMargin.NONE.value - binding.widgetDetail.clockBottomMarginSmall.isVisible = - Preferences.showClock && Preferences.clockBottomMargin == Constants.ClockBottomMargin.SMALL.value - binding.widgetDetail.clockBottomMarginMedium.isVisible = - Preferences.showClock && Preferences.clockBottomMargin == Constants.ClockBottomMargin.MEDIUM.value - binding.widgetDetail.clockBottomMarginLarge.isVisible = - Preferences.showClock && Preferences.clockBottomMargin == Constants.ClockBottomMargin.LARGE.value - - if ((Preferences.showClock && (binding.widgetDetail.time.alpha < 1f)) || (!Preferences.showClock && (binding.widgetDetail.time.alpha > 0f))) { - if (Preferences.showClock) { - binding.widgetDetail.timeContainer.layoutParams = binding.widgetDetail.timeContainer.layoutParams.apply { - height = RelativeLayout.LayoutParams.WRAP_CONTENT - } - binding.widgetDetail.timeContainer.measure(0, 0) - } - val initialHeight = binding.widgetDetail.timeContainer.measuredHeight - ValueAnimator.ofFloat( - if (Preferences.showClock) 0f else 1f, - if (Preferences.showClock) 1f else 0f - ).apply { - duration = 500L - addUpdateListener { - val animatedValue = animatedValue as Float - binding.widgetDetail.timeContainer.layoutParams = - binding.widgetDetail.timeContainer.layoutParams.apply { - height = (initialHeight * animatedValue).toInt() - } - binding.widgetDetail.time.alpha = animatedValue - } - addListener( - onStart = { - if (Preferences.showClock) { - binding.widgetDetail.timeContainer.isVisible = true - } - }, - onEnd = { - if (!Preferences.showClock) { - binding.widgetDetail.timeContainer.isVisible = false - } - } - ) - }.start() - - ValueAnimator.ofInt( - binding.preview.height, - PREVIEW_BASE_HEIGHT.toPixel(requireContext()) + if (Preferences.showClock) 100.toPixel( - requireContext() - ) else 0 - ).apply { - duration = 500L - addUpdateListener { - val animatedValue = animatedValue as Int - val layoutParams = binding.preview.layoutParams - layoutParams.height = animatedValue - binding.preview.layoutParams = layoutParams - } - }.start() - } else { - binding.widgetDetail.timeContainer.layoutParams = binding.widgetDetail.timeContainer.layoutParams.apply { - height = RelativeLayout.LayoutParams.WRAP_CONTENT - } - binding.widgetDetail.timeContainer.measure(0, 0) - } - - if (binding.preview.height == 0) { - ValueAnimator.ofInt( - binding.preview.height, - PREVIEW_BASE_HEIGHT.toPixel(requireContext()) + if (Preferences.showClock) 100.toPixel( - requireContext() - ) else 0 - ).apply { - duration = 300L - addUpdateListener { - val animatedValue = animatedValue as Int - val layoutParams = binding.preview.layoutParams - layoutParams.height = animatedValue - binding.preview.layoutParams = layoutParams - } - }.start() + .setDuration(200L).start() + binding.widgetDetail.bitmapContainer.apply { + setImageBitmap(bitmap) } binding.widgetLoader.animate().scaleX(0f).scaleY(0f).alpha(0f) .setDuration(200L).start() - binding.widgetDetail.bitmapContainer.apply { - setImageBitmap(bitmap) - scaleX = 0.9f - scaleY = 0.9f - } binding.widget.animate().alpha(1f).start() } } @@ -285,6 +184,43 @@ class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList } } + private fun updateClock() { + // Clock + binding.widgetDetail.time.setTextColor(ColorHelper.getClockFontColor(requireActivity().isDarkTheme())) + binding.widgetDetail.timeAmPm.setTextColor(ColorHelper.getClockFontColor(requireActivity().isDarkTheme())) + binding.widgetDetail.time.setTextSize( + TypedValue.COMPLEX_UNIT_SP, + Preferences.clockTextSize.toPixel(requireContext()) + ) + binding.widgetDetail.timeAmPm.setTextSize( + TypedValue.COMPLEX_UNIT_SP, + Preferences.clockTextSize.toPixel(requireContext()) / 5 * 2 + ) + binding.widgetDetail.timeAmPm.isVisible = Preferences.showAMPMIndicator + + // Clock bottom margin + binding.widgetDetail.clockBottomMarginNone.isVisible = + Preferences.showClock && Preferences.clockBottomMargin == Constants.ClockBottomMargin.NONE.value + binding.widgetDetail.clockBottomMarginSmall.isVisible = + Preferences.showClock && Preferences.clockBottomMargin == Constants.ClockBottomMargin.SMALL.value + binding.widgetDetail.clockBottomMarginMedium.isVisible = + Preferences.showClock && Preferences.clockBottomMargin == Constants.ClockBottomMargin.MEDIUM.value + binding.widgetDetail.clockBottomMarginLarge.isVisible = + Preferences.showClock && Preferences.clockBottomMargin == Constants.ClockBottomMargin.LARGE.value + + if (Preferences.showClock) { + binding.widgetDetail.timeContainer.expand() + } else { + binding.widgetDetail.timeContainer.collapse() + } + + binding.preview.layoutParams = binding.preview.layoutParams.apply { + height = PREVIEW_BASE_HEIGHT.toPixel(requireContext()) + if (Preferences.showClock) 100.toPixel( + requireContext() + ) else 0 + } + } + private fun subscribeUi(viewModel: MainViewModel) { viewModel.showWallpaper.observe(viewLifecycleOwner) { if (it) { @@ -328,39 +264,34 @@ class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList viewModel.fragmentScrollY.observe(viewLifecycleOwner) { binding.toolbar.cardElevation = if (it > 0) 24f else 0f } + + viewModel.clockPreferencesUpdate.observe(viewLifecycleOwner) { + updateClock() + } + + viewModel.widgetPreferencesUpdate.observe(viewLifecycleOwner) { + onUpdateUiEvent(null) + } } override fun onResume() { super.onResume() - Preferences.preferences.registerOnSharedPreferenceChangeListener(this) EventBus.getDefault().register(this) updateUI() } override fun onPause() { - Preferences.preferences.unregisterOnSharedPreferenceChangeListener(this) EventBus.getDefault().unregister(this) super.onPause() } private var delayJob: Job? = null - override fun onSharedPreferenceChanged(preferences: SharedPreferences, p1: String) { - delayJob?.cancel() - delayJob = lifecycleScope.launch(Dispatchers.IO) { - delay(300) - withContext(Dispatchers.Main) { - updateUI() - } - } - MainWidget.updateWidget(requireContext()) - } - class UpdateUiMessageEvent class ChangeTabEvent(val page: Int) @Subscribe(threadMode = ThreadMode.MAIN) - fun onMessageEvent(ignore: UpdateUiMessageEvent) { + fun onUpdateUiEvent(ignore: UpdateUiMessageEvent?) { delayJob?.cancel() delayJob = lifecycleScope.launch(Dispatchers.IO) { delay(300) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/tabs/CalendarFragment.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/tabs/CalendarFragment.kt index 1ac70e9..b1a644a 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/tabs/CalendarFragment.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/tabs/CalendarFragment.kt @@ -28,6 +28,7 @@ import com.tommasoberlose.anotherwidget.ui.viewmodels.MainViewModel import com.tommasoberlose.anotherwidget.helpers.CalendarHelper import com.tommasoberlose.anotherwidget.helpers.IntentHelper import com.tommasoberlose.anotherwidget.helpers.SettingsStringHelper +import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget import com.tommasoberlose.anotherwidget.utils.checkGrantedPermission import com.tommasoberlose.anotherwidget.utils.isDefaultSet import com.tommasoberlose.anotherwidget.utils.toast @@ -185,7 +186,7 @@ class CalendarFragment : Fragment() { binding.showAllDayToggle.setOnCheckedChangeListener { _, isChecked -> Preferences.calendarAllDay = isChecked - updateCalendar() + MainWidget.updateWidget(requireContext()) } binding.actionChangeAttendeeFilter.setOnClickListener { @@ -230,7 +231,7 @@ class CalendarFragment : Fragment() { binding.showOnlyBusyEventsToggle.setOnCheckedChangeListener { _, isChecked -> Preferences.showOnlyBusyEvents = isChecked - updateCalendar() + MainWidget.updateWidget(requireContext()) } binding.actionShowDiffTime.setOnClickListener { diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/viewmodels/MainViewModel.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/viewmodels/MainViewModel.kt index 162f4e8..c088649 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/viewmodels/MainViewModel.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/viewmodels/MainViewModel.kt @@ -55,6 +55,7 @@ class MainViewModel : ViewModel() { val openEventDetails = Preferences.asLiveData(Preferences::openEventDetails) val calendarAppName = Preferences.asLiveData(Preferences::calendarAppName) val widgetUpdateFrequency = Preferences.asLiveData(Preferences::widgetUpdateFrequency) + val dateFormat = Preferences.asLiveData(Preferences::dateFormat) // Clock Settings val showClock = Preferences.asLiveData(Preferences::showClock) @@ -70,7 +71,6 @@ class MainViewModel : ViewModel() { val showAMPMIndicator = Preferences.asLiveData(Preferences::showAMPMIndicator) val clockAppName = Preferences.asLiveData(Preferences::clockAppName) - val dateFormat = Preferences.asLiveData(Preferences::dateFormat) val clockBottomMargin = Preferences.asLiveData(Preferences::clockBottomMargin) // Weather Settings @@ -95,4 +95,49 @@ class MainViewModel : ViewModel() { // UI val fragmentScrollY = MutableLiveData() + val clockPreferencesUpdate = MediatorLiveData().apply { + addSource(Preferences.asLiveData(Preferences::showClock)) { value = true } + addSource(Preferences.asLiveData(Preferences::clockTextSize)) { value = true } + addSource(Preferences.asLiveData(Preferences::clockTextColor)) { value = true } + addSource(Preferences.asLiveData(Preferences::clockTextAlpha)) { value = true } + addSource(Preferences.asLiveData(Preferences::clockTextColorDark)) { value = true } + addSource(Preferences.asLiveData(Preferences::clockTextAlphaDark)) { value = true } + addSource(Preferences.asLiveData(Preferences::showAMPMIndicator)) { value = true } + addSource(Preferences.asLiveData(Preferences::clockBottomMargin)) { value = true } + } + val widgetPreferencesUpdate = MediatorLiveData().apply { + addSource(Preferences.asLiveData(Preferences::textGlobalColor)) { value = true } + addSource(Preferences.asLiveData(Preferences::textGlobalAlpha)) { value = true } + addSource(Preferences.asLiveData(Preferences::textSecondaryColor)) { value = true } + addSource(Preferences.asLiveData(Preferences::textSecondaryAlpha)) { value = true } + addSource(Preferences.asLiveData(Preferences::backgroundCardColor)) { value = true } + addSource(Preferences.asLiveData(Preferences::backgroundCardAlpha)) { value = true } + addSource(Preferences.asLiveData(Preferences::textGlobalColorDark)) { value = true } + addSource(Preferences.asLiveData(Preferences::textGlobalAlphaDark)) { value = true } + addSource(Preferences.asLiveData(Preferences::dateFormat)) { value = true } + addSource(Preferences.asLiveData(Preferences::textSecondaryColorDark)) { value = true } + addSource(Preferences.asLiveData(Preferences::textSecondaryAlphaDark)) { value = true } + addSource(Preferences.asLiveData(Preferences::backgroundCardColorDark)) { value = true } + addSource(Preferences.asLiveData(Preferences::backgroundCardAlphaDark)) { value = true } + addSource(Preferences.asLiveData(Preferences::textMainSize)) { value = true } + addSource(Preferences.asLiveData(Preferences::textSecondSize)) { value = true } + addSource(Preferences.asLiveData(Preferences::textShadow)) { value = true } + addSource(Preferences.asLiveData(Preferences::textShadowDark)) { value = true } + addSource(Preferences.asLiveData(Preferences::customFont)) { value = true } + addSource(Preferences.asLiveData(Preferences::customFontFile)) { value = true } + addSource(Preferences.asLiveData(Preferences::customFontName)) { value = true } + addSource(Preferences.asLiveData(Preferences::customFontVariant)) { value = true } + addSource(Preferences.asLiveData(Preferences::secondRowInformation)) { value = true } + addSource(Preferences.asLiveData(Preferences::showDividers)) { value = true } + addSource(Preferences.asLiveData(Preferences::secondRowTopMargin)) { value = true } + addSource(Preferences.asLiveData(Preferences::showEvents)) { value = true } + addSource(Preferences.asLiveData(Preferences::calendarAllDay)) { value = true } + addSource(Preferences.asLiveData(Preferences::showDiffTime)) { value = true } + addSource(Preferences.asLiveData(Preferences::showNextEvent)) { value = true } + + addSource(Preferences.asLiveData(Preferences::showWeather)) { value = true } + addSource(Preferences.asLiveData(Preferences::weatherTempUnit)) { value = true } + addSource(Preferences.asLiveData(Preferences::weatherIconPack)) { value = true } + addSource(Preferences.asLiveData(Preferences::showPreview)) { value = true } + } } diff --git a/app/src/main/res/layout/fragment_app_main.xml b/app/src/main/res/layout/fragment_app_main.xml index bbc7b41..6486d21 100644 --- a/app/src/main/res/layout/fragment_app_main.xml +++ b/app/src/main/res/layout/fragment_app_main.xml @@ -5,7 +5,6 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" - android:background="?android:attr/colorPrimaryDark" tools:context=".ui.activities.MainActivity"> + android:layout_height="wrap_content" + android:animateLayoutChanges="true"> diff --git a/app/src/main/res/layout/the_widget_sans.xml b/app/src/main/res/layout/the_widget_sans.xml index 789e531..697b42a 100644 --- a/app/src/main/res/layout/the_widget_sans.xml +++ b/app/src/main/res/layout/the_widget_sans.xml @@ -15,7 +15,8 @@ android:gravity="center" android:paddingLeft="8dp" android:paddingRight="8dp" - android:id="@+id/main_layout"> + android:id="@+id/main_layout" + android:animateLayoutChanges="true"> Clock - Tap on clock opens + Clock Show Clock Clock is visible Clock is hidden Text size - Clock + Default clock app Clock bottom margin None Small diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 8841b62..7ad292a 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -11,6 +11,7 @@ @color/colorAccent @color/black_20 true + true