mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Refactor SubtitleCard
This commit is contained in:
@@ -19,10 +19,17 @@
|
||||
|
||||
package org.isoron.uhabits.activities.habits.show
|
||||
|
||||
import android.annotation.*
|
||||
import android.content.*
|
||||
import android.content.res.*
|
||||
import org.isoron.androidbase.activities.*
|
||||
import org.isoron.uhabits.*
|
||||
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.utils.*
|
||||
import org.isoron.uhabits.utils.*
|
||||
import java.util.*
|
||||
import javax.inject.*
|
||||
|
||||
@ActivityScope
|
||||
@@ -30,10 +37,12 @@ class ShowHabitPresenter
|
||||
@Inject constructor(
|
||||
val habit: Habit,
|
||||
val commandRunner: CommandRunner,
|
||||
@ActivityContext val context: Context,
|
||||
) : CommandRunner.Listener {
|
||||
|
||||
private val listeners = mutableListOf<Listener>()
|
||||
private var data = ShowHabitViewModel()
|
||||
private val resources = context.resources
|
||||
|
||||
fun onResume() {
|
||||
commandRunner.addListener(this)
|
||||
@@ -74,15 +83,59 @@ class ShowHabitPresenter
|
||||
val scoreToday = scores.todayValue.toFloat()
|
||||
val scoreLastMonth = scores.getValue(lastMonth).toFloat()
|
||||
val scoreLastYear = scores.getValue(lastYear).toFloat()
|
||||
val reminderText = if (habit.hasReminder()) {
|
||||
formatTime(context, habit.reminder.hour, habit.reminder.minute)!!
|
||||
} else {
|
||||
resources.getString(R.string.reminder_off)
|
||||
}
|
||||
data = 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),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -46,8 +46,8 @@ class ShowHabitRootView
|
||||
|
||||
binding.overviewCard.presenter = presenter
|
||||
binding.notesCard.presenter = presenter
|
||||
binding.subtitleCard.presenter = presenter
|
||||
|
||||
binding.subtitleCard.habit = habit
|
||||
binding.scoreCard.habit = habit
|
||||
binding.historyCard.habit = habit
|
||||
binding.streakCard.habit = habit
|
||||
|
||||
@@ -24,10 +24,14 @@ 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 = "",
|
||||
)
|
||||
@@ -18,85 +18,68 @@
|
||||
*/
|
||||
package org.isoron.uhabits.activities.habits.show.views
|
||||
|
||||
import android.annotation.*
|
||||
import android.content.*
|
||||
import android.util.*
|
||||
import android.view.*
|
||||
import android.widget.*
|
||||
import org.isoron.androidbase.utils.*
|
||||
import org.isoron.uhabits.*
|
||||
import org.isoron.uhabits.activities.habits.list.views.*
|
||||
import org.isoron.uhabits.activities.habits.show.*
|
||||
import org.isoron.uhabits.core.models.*
|
||||
import org.isoron.uhabits.core.tasks.*
|
||||
import org.isoron.uhabits.databinding.*
|
||||
import org.isoron.uhabits.utils.*
|
||||
import org.isoron.uhabits.utils.PaletteUtils.getAndroidTestColor
|
||||
import java.util.*
|
||||
|
||||
class SubtitleCard(context: Context?, attrs: AttributeSet?) : HabitCard(context, attrs) {
|
||||
class SubtitleCard(
|
||||
context: Context,
|
||||
attrs: AttributeSet,
|
||||
) : LinearLayout(context, attrs), ShowHabitPresenter.Listener {
|
||||
|
||||
private val binding = ShowHabitSubtitleBinding.inflate(LayoutInflater.from(context), this)
|
||||
lateinit var presenter: ShowHabitPresenter
|
||||
|
||||
init {
|
||||
init()
|
||||
}
|
||||
|
||||
private lateinit var binding: ShowHabitSubtitleBinding
|
||||
|
||||
public override fun refreshData() {
|
||||
val habit = habit
|
||||
val color = habit.color.toThemedAndroidColor(context)
|
||||
if (habit.isNumerical) {
|
||||
binding.targetText.text = "${habit.targetValue.toShortString()} ${habit.unit}"
|
||||
} else {
|
||||
binding.targetIcon.visibility = View.GONE
|
||||
binding.targetText.visibility = View.GONE
|
||||
}
|
||||
binding.reminderLabel.text = resources.getString(R.string.reminder_off)
|
||||
binding.questionLabel.visibility = View.VISIBLE
|
||||
binding.questionLabel.setTextColor(color)
|
||||
binding.questionLabel.text = habit.question
|
||||
binding.frequencyLabel.text = toText(habit.frequency)
|
||||
if (habit.hasReminder()) updateReminderText(habit.reminder)
|
||||
if (habit.question.isEmpty()) binding.questionLabel.visibility = View.GONE
|
||||
invalidate()
|
||||
}
|
||||
|
||||
private fun init() {
|
||||
val fontAwesome = InterfaceUtils.getFontAwesome(context)
|
||||
binding = ShowHabitSubtitleBinding.inflate(LayoutInflater.from(context), this)
|
||||
binding.targetIcon.typeface = fontAwesome
|
||||
binding.frequencyIcon.typeface = fontAwesome
|
||||
binding.reminderIcon.typeface = fontAwesome
|
||||
if (isInEditMode) initEditMode()
|
||||
if (isInEditMode) onData(ShowHabitViewModel(
|
||||
isNumerical = false,
|
||||
frequencyText = "Every day",
|
||||
question = "How many steps did you walk today?",
|
||||
color = PaletteColor(1),
|
||||
|
||||
))
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
private fun initEditMode() {
|
||||
binding.questionLabel.setTextColor(getAndroidTestColor(1))
|
||||
binding.questionLabel.text = "Have you meditated today?"
|
||||
binding.reminderLabel.text = "08:00"
|
||||
override fun onAttachedToWindow() {
|
||||
super.onAttachedToWindow()
|
||||
presenter.addListener(this)
|
||||
presenter.requestData(this)
|
||||
}
|
||||
|
||||
private fun toText(freq: Frequency): String {
|
||||
val resources = resources
|
||||
val num = freq.numerator
|
||||
val den = freq.denominator
|
||||
if (num == den) return resources.getString(R.string.every_day)
|
||||
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)
|
||||
return if (den >= 30) resources.getString(R.string.every_month) else resources.getString(R.string.every_x_days, den)
|
||||
}
|
||||
val times_every = resources.getString(R.string.times_every)
|
||||
return String.format(Locale.US, "%d %s %d %s", num, times_every, den,
|
||||
resources.getString(R.string.days))
|
||||
override fun onDetachedFromWindow() {
|
||||
presenter.removeListener(this)
|
||||
super.onDetachedFromWindow()
|
||||
}
|
||||
|
||||
private fun updateReminderText(reminder: Reminder) {
|
||||
binding.reminderLabel.text = formatTime(context, reminder.hour, reminder.minute)
|
||||
override fun onData(data: ShowHabitViewModel) {
|
||||
val color = data.color.toThemedAndroidColor(context)
|
||||
binding.frequencyLabel.text = data.frequencyText
|
||||
binding.questionLabel.setTextColor(color)
|
||||
binding.questionLabel.text = data.question
|
||||
binding.reminderLabel.text = data.reminderText
|
||||
binding.targetText.text = data.targetText
|
||||
|
||||
binding.questionLabel.visibility = View.VISIBLE
|
||||
binding.targetIcon.visibility = View.VISIBLE
|
||||
binding.targetText.visibility = View.VISIBLE
|
||||
if (!data.isNumerical) {
|
||||
binding.targetIcon.visibility = View.GONE
|
||||
binding.targetText.visibility = View.GONE
|
||||
}
|
||||
if (data.question.isEmpty()) {
|
||||
binding.questionLabel.visibility = View.GONE
|
||||
}
|
||||
|
||||
override fun createRefreshTask(): Task {
|
||||
// Never called
|
||||
throw IllegalStateException()
|
||||
postInvalidate()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user