From 98d17d60a566c0f77ca4157dca5bf22b355bbf69 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Tue, 22 Dec 2020 09:17:39 -0600 Subject: [PATCH] Refactor ShowHabitRootView; convert ShowHabitActivity to Kotlin --- .../activities/HabitsActivityComponent.kt | 1 + ...abitActivity.java => ShowHabitActivity.kt} | 26 +-- ...howHabitModule.java => ShowHabitModule.kt} | 21 +-- .../habits/show/ShowHabitPresenter.kt | 53 ++++++ .../habits/show/ShowHabitRootView.java | 171 ------------------ .../habits/show/ShowHabitRootView.kt | 97 ++++++++++ .../habits/show/ShowHabitViewModel.kt | 25 +++ .../src/main/res/layout/show_habit.xml | 59 +++++- .../src/main/res/layout/show_habit_inner.xml | 81 --------- 9 files changed, 250 insertions(+), 284 deletions(-) rename android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/{ShowHabitActivity.java => ShowHabitActivity.kt} (60%) rename android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/{ShowHabitModule.java => ShowHabitModule.kt} (65%) create mode 100644 android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitPresenter.kt delete mode 100644 android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitRootView.java create mode 100644 android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitRootView.kt create mode 100644 android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitViewModel.kt delete mode 100644 android/uhabits-android/src/main/res/layout/show_habit_inner.xml diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/HabitsActivityComponent.kt b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/HabitsActivityComponent.kt index a135b02d4..3cc205971 100644 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/HabitsActivityComponent.kt +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/HabitsActivityComponent.kt @@ -40,6 +40,7 @@ import org.isoron.uhabits.core.ui.screens.habits.list.* HabitModule::class ), dependencies = arrayOf(HabitsApplicationComponent::class)) interface HabitsActivityComponent { + val showHabitPresenter: ShowHabitPresenter val colorPickerDialogFactory: ColorPickerDialogFactory val habitCardListAdapter: HabitCardListAdapter val listHabitsBehavior: ListHabitsBehavior diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.java b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt similarity index 60% rename from android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.java rename to android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt index 104056810..2cff3bf30 100644 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.java +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitActivity.kt @@ -16,24 +16,14 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ +package org.isoron.uhabits.activities.habits.show -package org.isoron.uhabits.activities.habits.show; +import android.os.* +import org.isoron.uhabits.activities.* -import android.os.*; - -import org.isoron.uhabits.activities.*; - -/** - * Activity that allows the user to see more information about a single habit. - *

- * Shows all the metadata for the habit, in addition to several charts. - */ -public class ShowHabitActivity extends HabitsActivity -{ - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setScreen(getComponent().getShowHabitScreen()); +class ShowHabitActivity : HabitsActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setScreen(component.showHabitScreen) } -} +} \ No newline at end of file diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitModule.java b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitModule.kt similarity index 65% rename from android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitModule.java rename to android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitModule.kt index 7dc325daf..f21122e6a 100644 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitModule.java +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitModule.kt @@ -16,23 +16,20 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ +package org.isoron.uhabits.activities.habits.show -package org.isoron.uhabits.activities.habits.show; - -import org.isoron.uhabits.activities.*; -import org.isoron.uhabits.core.ui.screens.habits.show.*; - -import dagger.*; +import dagger.* +import org.isoron.uhabits.activities.* +import org.isoron.uhabits.core.ui.screens.habits.show.* @Module -public abstract class ShowHabitModule -{ +abstract class ShowHabitModule { @Binds - abstract ShowHabitBehavior.Screen getScreen(ShowHabitScreen screen); + abstract fun getScreen(screen: ShowHabitScreen): ShowHabitBehavior.Screen @Binds - abstract ShowHabitMenuBehavior.Screen getMenuScreen(ShowHabitScreen screen); + abstract fun getMenuScreen(screen: ShowHabitScreen): ShowHabitMenuBehavior.Screen @Binds - abstract ShowHabitMenuBehavior.System getSystem(HabitsDirFinder system); -} + abstract fun getSystem(system: HabitsDirFinder): ShowHabitMenuBehavior.System +} \ No newline at end of file diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitPresenter.kt b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitPresenter.kt new file mode 100644 index 000000000..67b97de53 --- /dev/null +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitPresenter.kt @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2016-2020 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.activities.habits.show + +import org.isoron.androidbase.activities.* +import org.isoron.uhabits.core.models.* +import javax.inject.* + +@ActivityScope +class ShowHabitPresenter +@Inject constructor( + val habit: Habit, +) { + private val listeners = mutableListOf() + + private fun build() = ShowHabitViewModel( + title = habit.name, + isNumerical = habit.isNumerical, + ) + + fun addListener(listener: Listener) { + listeners.add(listener) + } + + fun removeListener(listener: Listener) { + listeners.remove(listener) + } + + fun requestData(listener: Listener) { + listener.onData(build()) + } + + interface Listener { + fun onData(data: ShowHabitViewModel) + } +} \ No newline at end of file diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitRootView.java b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitRootView.java deleted file mode 100644 index 463084482..000000000 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitRootView.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.activities.habits.show; - -import android.content.*; -import android.os.*; -import android.view.*; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.appcompat.widget.*; - -import org.isoron.androidbase.activities.*; -import org.isoron.androidbase.utils.*; -import org.isoron.uhabits.R; -import org.isoron.uhabits.activities.habits.show.views.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.utils.*; - -import javax.inject.*; - -import butterknife.*; - -@ActivityScope -public class ShowHabitRootView extends BaseRootView - implements ModelObservable.Listener -{ - @NonNull - private Habit habit; - - @BindView(R.id.frequencyCard) - FrequencyCard frequencyCard; - - @BindView(R.id.streakCard) - StreakCard streakCard; - - @BindView(R.id.subtitleCard) - SubtitleCard subtitleCard; - - @BindView(R.id.notesCard) - NotesCard notesCard; - - @BindView(R.id.habitNotes) - TextView habitNotes; - - @BindView(R.id.overviewCard) - OverviewCard overviewCard; - - @BindView(R.id.scoreCard) - ScoreCard scoreCard; - - @BindView(R.id.historyCard) - HistoryCard historyCard; - - @BindView(R.id.barCard) - BarCard barCard; - - @BindView(R.id.toolbar) - Toolbar toolbar; - - @BindView(R.id.targetCard) - TargetCard targetCard; - - @NonNull - private Controller controller; - - @Inject - public ShowHabitRootView(@NonNull @ActivityContext Context context, - @NonNull Habit habit) - { - super(context); - this.habit = habit; - - addView(inflate(getContext(), R.layout.show_habit, null)); - ButterKnife.bind(this); - - controller = new Controller() {}; - setDisplayHomeAsUp(true); - initCards(); - initToolbar(); - } - - @Override - public int getToolbarColor() - { - StyledResources res = new StyledResources(getContext()); - if (!res.getBoolean(R.attr.useHabitColorAsPrimary)) - return super.getToolbarColor(); - - return PaletteUtilsKt.toThemedAndroidColor(habit.getColor(), getContext()); - } - - @Override - public void onModelChange() - { - new Handler(Looper.getMainLooper()).post(() -> { - toolbar.setTitle(habit.getName()); - }); - - controller.onToolbarChanged(); - } - - public void setController(@NonNull Controller controller) - { - this.controller = controller; - historyCard.setController(controller); - } - - @Override - protected void initToolbar() - { - super.initToolbar(); - toolbar.setTitle(habit.getName()); - } - - @Override - protected void onAttachedToWindow() - { - super.onAttachedToWindow(); - habit.getObservable().addListener(this); - } - - @Override - protected void onDetachedFromWindow() - { - habit.getObservable().removeListener(this); - super.onDetachedFromWindow(); - } - - private void initCards() - { - subtitleCard.setHabit(habit); - notesCard.setHabit(habit); - overviewCard.setHabit(habit); - scoreCard.setHabit(habit); - historyCard.setHabit(habit); - streakCard.setHabit(habit); - frequencyCard.setHabit(habit); - barCard.setHabit(habit); - targetCard.setHabit(habit); - - if(habit.isNumerical()) { - overviewCard.setVisibility(View.GONE); - streakCard.setVisibility(View.GONE); - } else { - targetCard.setVisibility(View.GONE); - } - } - - public interface Controller extends HistoryCard.Controller - { - default void onToolbarChanged() {} - } -} diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitRootView.kt b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitRootView.kt new file mode 100644 index 000000000..417aacf73 --- /dev/null +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitRootView.kt @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.uhabits.activities.habits.show + +import android.content.* +import android.view.* +import org.isoron.androidbase.activities.* +import org.isoron.androidbase.utils.* +import org.isoron.uhabits.* +import org.isoron.uhabits.activities.habits.show.views.* +import org.isoron.uhabits.core.models.* +import org.isoron.uhabits.databinding.* +import org.isoron.uhabits.utils.* +import javax.inject.* + +@ActivityScope +class ShowHabitRootView +@Inject constructor( + @ActivityContext context: Context, + private val habit: Habit, + private val presenter: ShowHabitPresenter, +) : BaseRootView(context), ShowHabitPresenter.Listener { + + private var controller: Controller = object : Controller {} + private var binding = ShowHabitBinding.inflate(LayoutInflater.from(context)) + + init { + addView(binding.root) + displayHomeAsUp = true + + binding.subtitleCard.habit = habit + binding.notesCard.habit = habit + binding.overviewCard.habit = habit + binding.scoreCard.habit = habit + binding.historyCard.habit = habit + binding.streakCard.habit = habit + binding.frequencyCard.habit = habit + binding.barCard.habit = habit + binding.targetCard.habit = habit + + initToolbar() + } + + override fun getToolbarColor(): Int { + val res = StyledResources(context) + return if (!res.getBoolean(R.attr.useHabitColorAsPrimary)) super.getToolbarColor() + else habit.color.toThemedAndroidColor(context) + } + + fun setController(controller: Controller) { + this.controller = controller + binding.historyCard.setController(controller) + } + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + presenter.addListener(this) + presenter.requestData(this) + } + + override fun onDetachedFromWindow() { + presenter.removeListener(this) + super.onDetachedFromWindow() + } + + override fun onData(data: ShowHabitViewModel) { + binding.toolbar.title = data.title + if (data.isNumerical) { + binding.overviewCard.visibility = GONE + binding.streakCard.visibility = GONE + } else { + binding.targetCard.visibility = GONE + } + controller.onToolbarChanged() + } + + interface Controller : HistoryCard.Controller { + fun onToolbarChanged() {} + } + +} \ No newline at end of file diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitViewModel.kt b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitViewModel.kt new file mode 100644 index 000000000..b915bcb6a --- /dev/null +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/show/ShowHabitViewModel.kt @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2016-2020 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.activities.habits.show + +data class ShowHabitViewModel( + val title: String, + val isNumerical: Boolean, +) \ No newline at end of file diff --git a/android/uhabits-android/src/main/res/layout/show_habit.xml b/android/uhabits-android/src/main/res/layout/show_habit.xml index 533814bdf..ac3f0b14e 100644 --- a/android/uhabits-android/src/main/res/layout/show_habit.xml +++ b/android/uhabits-android/src/main/res/layout/show_habit.xml @@ -31,8 +31,63 @@ app:popupTheme="?toolbarPopupTheme" android:layout_alignParentTop="true"/> - - + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file