Clean up code after conversions

This commit is contained in:
Quentin Hibon
2021-01-21 18:17:58 +01:00
parent 18db571507
commit 6992b5186e
44 changed files with 284 additions and 349 deletions

View File

@@ -101,20 +101,6 @@ abstract class HabitList : Iterable<Habit> {
abstract var primaryOrder: Order
abstract var secondaryOrder: Order
// /**
// * Changes the order of the elements on the list.
// *
// * @param order the new order criterion
// */
// abstract fun setPrimaryOrder(order: Order)
// /**
// * Changes the previous order of the elements on the list.
// *
// * @param order the new order criterion
// */
// abstract fun setSecondaryOrder(order: Order)
/**
* Returns the index of the given habit in the list, or -1 if the list does
* not contain the habit.
@@ -172,7 +158,7 @@ abstract class HabitList : Iterable<Habit> {
*
* @param habits the list of habits that have been modified.
*/
abstract fun update(habits: List<Habit?>?)
abstract fun update(habits: List<Habit>)
/**
* Notifies the list that a certain habit has been modified.

View File

@@ -33,7 +33,7 @@ import java.util.TimeZone
class Timestamp(unixTime: Long) : Comparable<Timestamp> {
val unixTime: Long
constructor(cal: GregorianCalendar) : this(cal.timeInMillis) {}
constructor(cal: GregorianCalendar) : this(cal.timeInMillis)
fun toLocalDate(): LocalDate {
val millisSince2000 = unixTime - 946684800000L

View File

@@ -119,16 +119,12 @@ class MemoryHabitList : HabitList {
private fun getComparatorByOrder(order: Order): Comparator<Habit> {
val nameComparatorAsc = Comparator<Habit> { habit1, habit2 ->
habit1.name.compareTo(
habit2.name
)
habit1.name.compareTo(habit2.name)
}
val nameComparatorDesc =
Comparator { h1: Habit, h2: Habit -> nameComparatorAsc.compare(h2, h1) }
val colorComparatorAsc = Comparator<Habit> { (color1), (color2) ->
color1.compareTo(
color2
)
color1.compareTo(color2)
}
val colorComparatorDesc =
Comparator { h1: Habit, h2: Habit -> colorComparatorAsc.compare(h2, h1) }
@@ -140,9 +136,7 @@ class MemoryHabitList : HabitList {
val scoreComparatorAsc =
Comparator { h1: Habit, h2: Habit -> scoreComparatorDesc.compare(h2, h1) }
val positionComparator =
Comparator<Habit> { habit1, habit2 ->
habit1.position.compareTo(habit2.position)
}
Comparator<Habit> { habit1, habit2 -> habit1.position.compareTo(habit2.position) }
val statusComparatorDesc = Comparator { h1: Habit, h2: Habit ->
if (h1.isCompletedToday() != h2.isCompletedToday()) {
return@Comparator if (h1.isCompletedToday()) -1 else 1
@@ -206,7 +200,7 @@ class MemoryHabitList : HabitList {
}
@Synchronized
override fun update(habits: List<Habit?>?) {
override fun update(habits: List<Habit>) {
resort()
}

View File

@@ -31,7 +31,7 @@ import javax.inject.Inject
* Implementation of a [HabitList] that is backed by SQLite.
*/
class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory) : HabitList() {
private val repository: Repository<HabitRecord>
private val repository: Repository<HabitRecord> = modelFactory.buildHabitListRepository()
private val list: MemoryHabitList = MemoryHabitList()
private var loaded = false
private fun loadRecords() {
@@ -197,13 +197,11 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
}
@Synchronized
override fun update(habits: List<Habit?>?) {
override fun update(habits: List<Habit>) {
loadRecords()
list.update(habits)
for (h in habits!!) {
val record = repository.find(
h!!.id!!
) ?: continue
for (h in habits) {
val record = repository.find(h.id!!) ?: continue
record.copyFrom(h)
repository.save(record)
}
@@ -219,8 +217,4 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
fun reload() {
loaded = false
}
init {
repository = modelFactory.buildHabitListRepository()
}
}

View File

@@ -47,11 +47,6 @@ class EntryRecord {
}
fun toEntry(): Entry {
return Entry(
Timestamp(
timestamp!!
),
value!!
)
return Entry(Timestamp(timestamp!!), value!!)
}
}

View File

@@ -85,8 +85,9 @@ class HabitRecord {
@field:Column
var uuid: String? = null
fun copyFrom(model: Habit?) {
id = model!!.id
fun copyFrom(model: Habit) {
id = model.id
name = model.name
description = model.description
highlight = 0

View File

@@ -141,7 +141,7 @@ class ReminderScheduler @Inject constructor(
): SchedulerResult
fun scheduleWidgetUpdate(updateTime: Long): SchedulerResult?
fun log(componentName: String?, msg: String?)
fun log(componentName: String, msg: String)
}
enum class SchedulerResult {

View File

@@ -42,7 +42,7 @@ class NotificationTray @Inject constructor(
private val preferences: Preferences,
private val systemTray: SystemTray
) : CommandRunner.Listener, Preferences.Listener {
private val active: HashMap<Habit, NotificationData>
private val active: HashMap<Habit, NotificationData> = HashMap()
fun cancel(habit: Habit) {
val notificationId = getNotificationId(habit)
systemTray.removeNotification(notificationId)
@@ -86,8 +86,7 @@ class NotificationTray @Inject constructor(
}
private fun reshowAll() {
for (habit in active.keys) {
val data = active[habit]!!
for ((habit, data) in active.entries) {
taskRunner.execute(ShowNotificationTask(habit, data))
}
}
@@ -101,15 +100,15 @@ class NotificationTray @Inject constructor(
reminderTime: Long
)
fun log(msg: String?)
fun log(msg: String)
}
internal class NotificationData(val timestamp: Timestamp, val reminderTime: Long)
private inner class ShowNotificationTask(private val habit: Habit, data: NotificationData) :
Task {
var todayValue = 0
private val timestamp: Timestamp
private val reminderTime: Long
private val timestamp: Timestamp = data.timestamp
private val reminderTime: Long = data.reminderTime
override fun doInBackground() {
val today = getTodayWithOffset()
todayValue = habit.computedEntries.get(today).value
@@ -172,18 +171,9 @@ class NotificationTray @Inject constructor(
val weekday = timestamp.weekday
return reminderDays[weekday]
}
init {
timestamp = data.timestamp
reminderTime = data.reminderTime
}
}
companion object {
const val REMINDERS_CHANNEL_ID = "REMINDERS"
}
init {
active = HashMap()
}
}

View File

@@ -62,6 +62,7 @@ class HabitCardListCache @Inject constructor(
private val data: CacheData
private var filteredHabits: HabitList
private val taskRunner: TaskRunner
@Synchronized
fun cancelTasks() {
currentFetchTask?.cancel()
@@ -195,6 +196,7 @@ class HabitCardListCache @Inject constructor(
val habits: MutableList<Habit>
val checkmarks: HashMap<Long?, IntArray>
val scores: HashMap<Long?, Double>
@Synchronized
fun copyCheckmarksFrom(oldData: CacheData) {
val empty = IntArray(checkmarkCount)
@@ -263,18 +265,14 @@ class HabitCardListCache @Inject constructor(
if (runner != null) runner!!.publishProgress(this, -1)
for (position in newData.habits.indices) {
if (isCancelled) return
val (_, _, _, id, _, _, _, _, _, _, _, _, _, _, computedEntries, _, scores) = newData.habits[position]
if (targetId != null && targetId != id) continue
newData.scores[id] = scores[today].value
val habit = newData.habits[position]
if (targetId != null && targetId != habit.id) continue
newData.scores[habit.id] = habit.scores[today].value
val list: MutableList<Int> = ArrayList()
for (
(_, value) in computedEntries
.getByInterval(dateFrom, today)
) {
for ((_, value) in habit.computedEntries.getByInterval(dateFrom, today))
list.add(value)
}
val entries = list.toTypedArray()
newData.checkmarks[id] = ArrayUtils.toPrimitive(entries)
newData.checkmarks[habit.id] = ArrayUtils.toPrimitive(entries)
runner!!.publishProgress(this, position)
}
}

View File

@@ -32,6 +32,7 @@ import java.io.File
import java.io.IOException
import java.util.LinkedList
import javax.inject.Inject
import kotlin.math.roundToInt
open class ListHabitsBehavior @Inject constructor(
private val habitList: HabitList,
@@ -51,14 +52,11 @@ open class ListHabitsBehavior @Inject constructor(
val oldValue = entries.get(timestamp!!).value.toDouble()
screen.showNumberPicker(
oldValue / 1000,
habit.unit,
{ newValue: Double ->
val value = Math.round(newValue * 1000).toDouble()
commandRunner.run(
CreateRepetitionCommand(habitList, habit, timestamp, value.toInt())
)
}
)
habit.unit
) { newValue: Double ->
val value = (newValue * 1000).roundToInt()
commandRunner.run(CreateRepetitionCommand(habitList, habit, timestamp, value))
}
}
fun onExportCSV() {
@@ -154,7 +152,7 @@ open class ListHabitsBehavior @Inject constructor(
callback: NumberPickerCallback
)
fun showSendBugReportToDeveloperScreen(log: String?)
fun showSendBugReportToDeveloperScreen(log: String)
fun showSendFileScreen(filename: String)
fun showConfirmInstallSyncKey(callback: OnConfirmedCallback)
}

View File

@@ -109,10 +109,7 @@ class ListHabitsMenuBehavior @Inject constructor(
interface Adapter {
fun refresh()
fun setFilter(matcher: HabitMatcher?)
// fun setSecondaryOrder(order: HabitList.Order)
// fun setPrimaryOrder(order: HabitList.Order)
// fun getPrimaryOrder(): HabitList.Order
fun setFilter(matcher: HabitMatcher)
var primaryOrder: HabitList.Order
var secondaryOrder: HabitList.Order
}

View File

@@ -51,19 +51,15 @@ class ListHabitsSelectionMenuBehavior @Inject constructor(
}
fun onArchiveHabits() {
commandRunner.run(
ArchiveHabitsCommand(habitList, adapter.selected)
)
commandRunner.run(ArchiveHabitsCommand(habitList, adapter.selected))
adapter.clearSelection()
}
fun onChangeColor() {
val selected = adapter.selected
val (color) = selected[0]
screen.showColorPicker(color) { selectedColor: PaletteColor? ->
commandRunner.run(
ChangeHabitColorCommand(habitList, selected, selectedColor!!)
)
screen.showColorPicker(color) { selectedColor: PaletteColor ->
commandRunner.run(ChangeHabitColorCommand(habitList, selected, selectedColor))
adapter.clearSelection()
}
}
@@ -73,9 +69,7 @@ class ListHabitsSelectionMenuBehavior @Inject constructor(
screen.showDeleteConfirmationScreen(
{
adapter.performRemove(selected)
commandRunner.run(
DeleteHabitsCommand(habitList, selected)
)
commandRunner.run(DeleteHabitsCommand(habitList, selected))
adapter.clearSelection()
},
selected.size
@@ -88,9 +82,7 @@ class ListHabitsSelectionMenuBehavior @Inject constructor(
}
fun onUnarchiveHabits() {
commandRunner.run(
UnarchiveHabitsCommand(habitList, adapter.selected)
)
commandRunner.run(UnarchiveHabitsCommand(habitList, adapter.selected))
adapter.clearSelection()
}