mirror of https://github.com/iSoron/uhabits.git
parent
bda3e42e2b
commit
ae4ac801a0
@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016-2020 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.activities
|
|
||||||
|
|
||||||
import android.content.*
|
|
||||||
import android.util.*
|
|
||||||
import android.widget.*
|
|
||||||
|
|
||||||
abstract class DataView<T>(
|
|
||||||
context: Context,
|
|
||||||
attrs: AttributeSet,
|
|
||||||
) : LinearLayout(context, attrs), Presenter.Listener<T> {
|
|
||||||
|
|
||||||
lateinit var presenter: Presenter<T>
|
|
||||||
|
|
||||||
override fun onAttachedToWindow() {
|
|
||||||
super.onAttachedToWindow()
|
|
||||||
presenter.addListener(this)
|
|
||||||
presenter.requestData(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDetachedFromWindow() {
|
|
||||||
presenter.removeListener(this)
|
|
||||||
super.onDetachedFromWindow()
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract override fun onData(data: T)
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016-2020 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.activities
|
|
||||||
|
|
||||||
import org.isoron.uhabits.core.commands.*
|
|
||||||
|
|
||||||
abstract class Presenter<M>(
|
|
||||||
val commandRunner: CommandRunner,
|
|
||||||
) : CommandRunner.Listener {
|
|
||||||
|
|
||||||
private val listeners = mutableListOf<Listener<M>>()
|
|
||||||
private var data: M? = null
|
|
||||||
|
|
||||||
fun onResume() {
|
|
||||||
commandRunner.addListener(this)
|
|
||||||
data = refresh()
|
|
||||||
notifyListeners()
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract fun refresh(): M
|
|
||||||
|
|
||||||
fun onPause() {
|
|
||||||
commandRunner.removeListener(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addListener(listener: Listener<M>) {
|
|
||||||
listeners.add(listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeListener(listener: Listener<M>) {
|
|
||||||
listeners.remove(listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun requestData(listener: Listener<M>) {
|
|
||||||
if (data == null) data = refresh()
|
|
||||||
listener.onData(data!!)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCommandExecuted(command: Command?, refreshKey: Long?) {
|
|
||||||
data = refresh()
|
|
||||||
notifyListeners()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun notifyListeners() {
|
|
||||||
for (l in listeners) l.onData(data!!)
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Listener<T> {
|
|
||||||
fun onData(data: T)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package org.isoron.uhabits.activities.habits.show
|
|
||||||
|
|
||||||
import dagger.*
|
|
||||||
import org.isoron.uhabits.activities.*
|
|
||||||
import org.isoron.uhabits.core.ui.screens.habits.show.*
|
|
||||||
|
|
||||||
@Module
|
|
||||||
abstract class ShowHabitModule {
|
|
||||||
@Binds
|
|
||||||
abstract fun getScreen(screen: ShowHabitScreen): ShowHabitBehavior.Screen
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
abstract fun getMenuScreen(screen: ShowHabitScreen): ShowHabitMenuBehavior.Screen
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
abstract fun getSystem(system: HabitsDirFinder): ShowHabitMenuBehavior.System
|
|
||||||
}
|
|
@ -1,113 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016-2020 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.activities.habits.show
|
|
||||||
|
|
||||||
import android.annotation.*
|
|
||||||
import android.content.*
|
|
||||||
import org.isoron.androidbase.activities.*
|
|
||||||
import org.isoron.uhabits.*
|
|
||||||
import org.isoron.uhabits.activities.*
|
|
||||||
import org.isoron.uhabits.activities.habits.list.views.*
|
|
||||||
import org.isoron.uhabits.core.commands.*
|
|
||||||
import org.isoron.uhabits.core.models.*
|
|
||||||
import org.isoron.uhabits.core.preferences.*
|
|
||||||
import org.isoron.uhabits.core.utils.*
|
|
||||||
import org.isoron.uhabits.utils.*
|
|
||||||
import java.util.*
|
|
||||||
import javax.inject.*
|
|
||||||
|
|
||||||
@ActivityScope
|
|
||||||
class ShowHabitPresenter
|
|
||||||
@Inject constructor(
|
|
||||||
val habit: Habit,
|
|
||||||
val preferences: Preferences,
|
|
||||||
commandRunner: CommandRunner,
|
|
||||||
@ActivityContext val context: Context,
|
|
||||||
) : Presenter<ShowHabitViewModel>(commandRunner) {
|
|
||||||
|
|
||||||
private val resources = context.resources
|
|
||||||
|
|
||||||
override fun refresh(): ShowHabitViewModel {
|
|
||||||
val today = DateUtils.getTodayWithOffset()
|
|
||||||
val lastMonth = today.minus(30)
|
|
||||||
val lastYear = today.minus(365)
|
|
||||||
|
|
||||||
val reminderText = if (habit.hasReminder()) {
|
|
||||||
formatTime(context, habit.reminder.hour, habit.reminder.minute)!!
|
|
||||||
} else {
|
|
||||||
resources.getString(R.string.reminder_off)
|
|
||||||
}
|
|
||||||
|
|
||||||
val scores = habit.scores
|
|
||||||
val scoreToday = scores.todayValue.toFloat()
|
|
||||||
val scoreLastMonth = scores.getValue(lastMonth).toFloat()
|
|
||||||
val scoreLastYear = scores.getValue(lastYear).toFloat()
|
|
||||||
|
|
||||||
return ShowHabitViewModel(
|
|
||||||
title = habit.name,
|
|
||||||
description = habit.description,
|
|
||||||
question = habit.question,
|
|
||||||
color = habit.color,
|
|
||||||
isNumerical = habit.isNumerical,
|
|
||||||
scoreToday = scoreToday,
|
|
||||||
scoreMonthDiff = scoreToday - scoreLastMonth,
|
|
||||||
scoreYearDiff = scoreToday - scoreLastYear,
|
|
||||||
totalCount = habit.repetitions.totalCount,
|
|
||||||
targetText = "${habit.targetValue.toShortString()} ${habit.unit}",
|
|
||||||
frequencyText = habit.frequency.format(),
|
|
||||||
reminderText = reminderText,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("StringFormatMatches")
|
|
||||||
private fun Frequency.format(): String {
|
|
||||||
val num = this.numerator
|
|
||||||
val den = this.denominator
|
|
||||||
if (num == den) {
|
|
||||||
return resources.getString(R.string.every_day)
|
|
||||||
}
|
|
||||||
if (den == 7) {
|
|
||||||
return resources.getString(R.string.x_times_per_week, num)
|
|
||||||
}
|
|
||||||
if (den == 30 || den == 31) {
|
|
||||||
return resources.getString(R.string.x_times_per_month, num)
|
|
||||||
}
|
|
||||||
if (num == 1) {
|
|
||||||
if (den == 7) {
|
|
||||||
return resources.getString(R.string.every_week)
|
|
||||||
}
|
|
||||||
if (den % 7 == 0) {
|
|
||||||
return resources.getString(R.string.every_x_weeks, den / 7)
|
|
||||||
}
|
|
||||||
if (den == 30 || den == 31) {
|
|
||||||
return resources.getString(R.string.every_month)
|
|
||||||
}
|
|
||||||
return resources.getString(R.string.every_x_days, den)
|
|
||||||
}
|
|
||||||
return String.format(
|
|
||||||
Locale.US,
|
|
||||||
"%d %s %d %s",
|
|
||||||
num,
|
|
||||||
resources.getString(R.string.times_every),
|
|
||||||
den,
|
|
||||||
resources.getString(R.string.days),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,100 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
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.*
|
|
||||||
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,
|
|
||||||
targetCardPresenter: TargetCardPresenter,
|
|
||||||
) : BaseRootView(context), Presenter.Listener<ShowHabitViewModel> {
|
|
||||||
|
|
||||||
private var controller: Controller = object : Controller {}
|
|
||||||
private var binding = ShowHabitBinding.inflate(LayoutInflater.from(context))
|
|
||||||
|
|
||||||
init {
|
|
||||||
addView(binding.root)
|
|
||||||
displayHomeAsUp = true
|
|
||||||
|
|
||||||
binding.overviewCard.presenter = presenter
|
|
||||||
binding.notesCard.presenter = presenter
|
|
||||||
binding.subtitleCard.presenter = presenter
|
|
||||||
binding.targetCard.presenter = targetCardPresenter
|
|
||||||
|
|
||||||
binding.scoreCard.habit = habit
|
|
||||||
binding.historyCard.habit = habit
|
|
||||||
binding.streakCard.habit = habit
|
|
||||||
binding.frequencyCard.habit = habit
|
|
||||||
binding.barCard.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() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016-2020 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.activities.habits.show
|
|
||||||
|
|
||||||
import org.isoron.uhabits.core.models.*
|
|
||||||
|
|
||||||
data class ShowHabitViewModel(
|
|
||||||
val title: String = "",
|
|
||||||
val description: String = "",
|
|
||||||
val question: String = "",
|
|
||||||
val isNumerical: Boolean = false,
|
|
||||||
val scoreToday: Float = 0f,
|
|
||||||
val scoreMonthDiff: Float = 0f,
|
|
||||||
val scoreYearDiff: Float = 0f,
|
|
||||||
val totalCount: Long = 0L,
|
|
||||||
val color: PaletteColor = PaletteColor(1),
|
|
||||||
val targetText: String = "",
|
|
||||||
val frequencyText: String = "",
|
|
||||||
val reminderText: String = "",
|
|
||||||
)
|
|
Loading…
Reference in new issue