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()
}

View File

@@ -29,16 +29,16 @@ import org.junit.Before
import org.junit.Test
class RepositoryTest : BaseUnitTest() {
private var repository: Repository<ThingRecord>? = null
private var db: Database? = null
private lateinit var repository: Repository<ThingRecord>
private lateinit var db: Database
@Before
@Throws(Exception::class)
override fun setUp() {
super.setUp()
db = buildMemoryDatabase()
repository = Repository(ThingRecord::class.java, db!!)
db!!.execute("drop table if exists tests")
db!!.execute(
repository = Repository(ThingRecord::class.java, db)
db.execute("drop table if exists tests")
db.execute(
"create table tests(" +
"id integer not null primary key autoincrement, " +
"color_number integer not null, score float not null, " +
@@ -49,11 +49,11 @@ class RepositoryTest : BaseUnitTest() {
@Test
@Throws(Exception::class)
fun testFind() {
db!!.execute(
db.execute(
"insert into tests(id, color_number, name, score) " +
"values (10, 20, 'hello', 8.0)"
)
val record = repository!!.find(10L)
val record = repository.find(10L)
Assert.assertNotNull(record)
MatcherAssert.assertThat(record!!.id, IsEqual.equalTo(10L))
MatcherAssert.assertThat(record.color, IsEqual.equalTo(20))
@@ -64,38 +64,30 @@ class RepositoryTest : BaseUnitTest() {
@Test
@Throws(Exception::class)
fun testSave_withId() {
val record = ThingRecord()
record.id = 50L
record.color = 10
record.name = "hello"
record.score = 5.0
repository!!.save(record)
MatcherAssert.assertThat(
record,
IsEqual.equalTo(
repository!!.find(50L)
)
)
val record = ThingRecord().apply {
id = 50L
color = 10
name = "hello"
score = 5.0
}
repository.save(record)
MatcherAssert.assertThat(record, IsEqual.equalTo(repository.find(50L)))
record.name = "world"
record.score = 128.0
repository!!.save(record)
MatcherAssert.assertThat(
record,
IsEqual.equalTo(
repository!!.find(50L)
)
)
repository.save(record)
MatcherAssert.assertThat(record, IsEqual.equalTo(repository.find(50L)))
}
@Test
@Throws(Exception::class)
fun testSave_withNull() {
val record = ThingRecord()
record.color = 50
record.name = null
record.score = 12.0
repository!!.save(record)
val retrieved = repository!!.find(record.id!!)
val record = ThingRecord().apply {
color = 50
name = null
score = 12.0
}
repository.save(record)
val retrieved = repository.find(record.id!!)
Assert.assertNotNull(retrieved)
Assert.assertNull(retrieved!!.name)
MatcherAssert.assertThat(record, IsEqual.equalTo(retrieved))
@@ -104,16 +96,18 @@ class RepositoryTest : BaseUnitTest() {
@Test
@Throws(Exception::class)
fun testSave_withoutId() {
val r1 = ThingRecord()
r1.color = 10
r1.name = "hello"
r1.score = 16.0
repository!!.save(r1)
val r2 = ThingRecord()
r2.color = 20
r2.name = "world"
r2.score = 2.0
repository!!.save(r2)
val r1 = ThingRecord().apply {
color = 10
name = "hello"
score = 16.0
}
repository.save(r1)
val r2 = ThingRecord().apply {
color = 20
name = "world"
score = 2.0
}
repository.save(r2)
MatcherAssert.assertThat(r1.id, IsEqual.equalTo(1L))
MatcherAssert.assertThat(r2.id, IsEqual.equalTo(2L))
}
@@ -121,40 +115,27 @@ class RepositoryTest : BaseUnitTest() {
@Test
@Throws(Exception::class)
fun testRemove() {
val rec1 = ThingRecord()
rec1.color = 10
rec1.name = "hello"
rec1.score = 16.0
repository!!.save(rec1)
val rec2 = ThingRecord()
rec2.color = 20
rec2.name = "world"
rec2.score = 32.0
repository!!.save(rec2)
val rec1 = ThingRecord().apply {
color = 10
name = "hello"
score = 16.0
}
repository.save(rec1)
val rec2 = ThingRecord().apply {
color = 20
name = "world"
score = 32.0
}
repository.save(rec2)
val id = rec1.id!!
MatcherAssert.assertThat(
rec1,
IsEqual.equalTo(
repository!!.find(id)
)
)
MatcherAssert.assertThat(
rec2,
IsEqual.equalTo(
repository!!.find(rec2.id!!)
)
)
repository!!.remove(rec1)
MatcherAssert.assertThat(rec1, IsEqual.equalTo(repository.find(id)))
MatcherAssert.assertThat(rec2, IsEqual.equalTo(repository.find(rec2.id!!)))
repository.remove(rec1)
MatcherAssert.assertThat(rec1.id, IsEqual.equalTo(null))
Assert.assertNull(repository!!.find(id))
MatcherAssert.assertThat(
rec2,
IsEqual.equalTo(
repository!!.find(rec2.id!!)
)
)
repository!!.remove(rec1) // should have no effect
Assert.assertNull(repository!!.find(id))
Assert.assertNull(repository.find(id))
MatcherAssert.assertThat(rec2, IsEqual.equalTo(repository.find(rec2.id!!)))
repository.remove(rec1) // should have no effect
Assert.assertNull(repository.find(id))
}
@Table(name = "tests")

View File

@@ -381,5 +381,5 @@ class EntryListTest {
}
}
fun day(offset: Int) = DateUtils.getToday().minus(offset)!!
fun day(offset: Int) = DateUtils.getToday().minus(offset)
}

View File

@@ -31,7 +31,7 @@ import org.junit.Test
class HabitRecordTest : BaseUnitTest() {
@Test
fun testCopyRestore1() {
val original = modelFactory.buildHabit().apply() {
val original = modelFactory.buildHabit().apply {
name = "Hello world"
question = "Did you greet the world today?"
color = PaletteColor(1)
@@ -50,7 +50,7 @@ class HabitRecordTest : BaseUnitTest() {
@Test
fun testCopyRestore2() {
val original = modelFactory.buildHabit().apply() {
val original = modelFactory.buildHabit().apply {
name = "Hello world"
question = "Did you greet the world today?"
color = PaletteColor(5)