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
|
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.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.commands.*
|
||||||
import org.isoron.uhabits.core.models.*
|
import org.isoron.uhabits.core.models.*
|
||||||
import org.isoron.uhabits.core.utils.*
|
import org.isoron.uhabits.core.utils.*
|
||||||
|
import org.isoron.uhabits.utils.*
|
||||||
|
import java.util.*
|
||||||
import javax.inject.*
|
import javax.inject.*
|
||||||
|
|
||||||
@ActivityScope
|
@ActivityScope
|
||||||
@@ -30,10 +37,12 @@ class ShowHabitPresenter
|
|||||||
@Inject constructor(
|
@Inject constructor(
|
||||||
val habit: Habit,
|
val habit: Habit,
|
||||||
val commandRunner: CommandRunner,
|
val commandRunner: CommandRunner,
|
||||||
|
@ActivityContext val context: Context,
|
||||||
) : CommandRunner.Listener {
|
) : CommandRunner.Listener {
|
||||||
|
|
||||||
private val listeners = mutableListOf<Listener>()
|
private val listeners = mutableListOf<Listener>()
|
||||||
private var data = ShowHabitViewModel()
|
private var data = ShowHabitViewModel()
|
||||||
|
private val resources = context.resources
|
||||||
|
|
||||||
fun onResume() {
|
fun onResume() {
|
||||||
commandRunner.addListener(this)
|
commandRunner.addListener(this)
|
||||||
@@ -74,15 +83,59 @@ class ShowHabitPresenter
|
|||||||
val scoreToday = scores.todayValue.toFloat()
|
val scoreToday = scores.todayValue.toFloat()
|
||||||
val scoreLastMonth = scores.getValue(lastMonth).toFloat()
|
val scoreLastMonth = scores.getValue(lastMonth).toFloat()
|
||||||
val scoreLastYear = scores.getValue(lastYear).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(
|
data = ShowHabitViewModel(
|
||||||
title = habit.name,
|
title = habit.name,
|
||||||
description = habit.description,
|
description = habit.description,
|
||||||
|
question = habit.question,
|
||||||
color = habit.color,
|
color = habit.color,
|
||||||
isNumerical = habit.isNumerical,
|
isNumerical = habit.isNumerical,
|
||||||
scoreToday = scoreToday,
|
scoreToday = scoreToday,
|
||||||
scoreMonthDiff = scoreToday - scoreLastMonth,
|
scoreMonthDiff = scoreToday - scoreLastMonth,
|
||||||
scoreYearDiff = scoreToday - scoreLastYear,
|
scoreYearDiff = scoreToday - scoreLastYear,
|
||||||
totalCount = habit.repetitions.totalCount,
|
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.overviewCard.presenter = presenter
|
||||||
binding.notesCard.presenter = presenter
|
binding.notesCard.presenter = presenter
|
||||||
|
binding.subtitleCard.presenter = presenter
|
||||||
|
|
||||||
binding.subtitleCard.habit = habit
|
|
||||||
binding.scoreCard.habit = habit
|
binding.scoreCard.habit = habit
|
||||||
binding.historyCard.habit = habit
|
binding.historyCard.habit = habit
|
||||||
binding.streakCard.habit = habit
|
binding.streakCard.habit = habit
|
||||||
|
|||||||
@@ -24,10 +24,14 @@ import org.isoron.uhabits.core.models.*
|
|||||||
data class ShowHabitViewModel(
|
data class ShowHabitViewModel(
|
||||||
val title: String = "",
|
val title: String = "",
|
||||||
val description: String = "",
|
val description: String = "",
|
||||||
|
val question: String = "",
|
||||||
val isNumerical: Boolean = false,
|
val isNumerical: Boolean = false,
|
||||||
val scoreToday: Float = 0f,
|
val scoreToday: Float = 0f,
|
||||||
val scoreMonthDiff: Float = 0f,
|
val scoreMonthDiff: Float = 0f,
|
||||||
val scoreYearDiff: Float = 0f,
|
val scoreYearDiff: Float = 0f,
|
||||||
val totalCount: Long = 0L,
|
val totalCount: Long = 0L,
|
||||||
val color: PaletteColor = PaletteColor(1),
|
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
|
package org.isoron.uhabits.activities.habits.show.views
|
||||||
|
|
||||||
import android.annotation.*
|
|
||||||
import android.content.*
|
import android.content.*
|
||||||
import android.util.*
|
import android.util.*
|
||||||
import android.view.*
|
import android.view.*
|
||||||
|
import android.widget.*
|
||||||
import org.isoron.androidbase.utils.*
|
import org.isoron.androidbase.utils.*
|
||||||
import org.isoron.uhabits.*
|
import org.isoron.uhabits.activities.habits.show.*
|
||||||
import org.isoron.uhabits.activities.habits.list.views.*
|
|
||||||
import org.isoron.uhabits.core.models.*
|
import org.isoron.uhabits.core.models.*
|
||||||
import org.isoron.uhabits.core.tasks.*
|
|
||||||
import org.isoron.uhabits.databinding.*
|
import org.isoron.uhabits.databinding.*
|
||||||
import org.isoron.uhabits.utils.*
|
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 {
|
||||||
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)
|
val fontAwesome = InterfaceUtils.getFontAwesome(context)
|
||||||
binding = ShowHabitSubtitleBinding.inflate(LayoutInflater.from(context), this)
|
|
||||||
binding.targetIcon.typeface = fontAwesome
|
binding.targetIcon.typeface = fontAwesome
|
||||||
binding.frequencyIcon.typeface = fontAwesome
|
binding.frequencyIcon.typeface = fontAwesome
|
||||||
binding.reminderIcon.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")
|
override fun onAttachedToWindow() {
|
||||||
private fun initEditMode() {
|
super.onAttachedToWindow()
|
||||||
binding.questionLabel.setTextColor(getAndroidTestColor(1))
|
presenter.addListener(this)
|
||||||
binding.questionLabel.text = "Have you meditated today?"
|
presenter.requestData(this)
|
||||||
binding.reminderLabel.text = "08:00"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun toText(freq: Frequency): String {
|
override fun onDetachedFromWindow() {
|
||||||
val resources = resources
|
presenter.removeListener(this)
|
||||||
val num = freq.numerator
|
super.onDetachedFromWindow()
|
||||||
val den = freq.denominator
|
}
|
||||||
if (num == den) return resources.getString(R.string.every_day)
|
|
||||||
if (num == 1) {
|
override fun onData(data: ShowHabitViewModel) {
|
||||||
if (den == 7) return resources.getString(R.string.every_week)
|
val color = data.color.toThemedAndroidColor(context)
|
||||||
if (den % 7 == 0) return resources.getString(R.string.every_x_weeks, den / 7)
|
binding.frequencyLabel.text = data.frequencyText
|
||||||
return if (den >= 30) resources.getString(R.string.every_month) else resources.getString(R.string.every_x_days, den)
|
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
|
||||||
}
|
}
|
||||||
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))
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateReminderText(reminder: Reminder) {
|
postInvalidate()
|
||||||
binding.reminderLabel.text = formatTime(context, reminder.hour, reminder.minute)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createRefreshTask(): Task {
|
|
||||||
// Never called
|
|
||||||
throw IllegalStateException()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user