Clean up code after conversions

This commit is contained in:
Quentin Hibon
2021-01-21 23:42:04 +01:00
parent f882e18be9
commit f1c88797a3
26 changed files with 152 additions and 217 deletions

View File

@@ -30,13 +30,13 @@ import javax.annotation.concurrent.ThreadSafe
@ThreadSafe
abstract class HabitList : Iterable<Habit> {
val observable: ModelObservable
@JvmField
protected val filter: HabitMatcher
/**
* Creates a new HabitList.
*
*
* Depending on the implementation, this list can either be empty or be
* populated by some pre-existing habits, for example, from a certain
* database.
@@ -54,7 +54,6 @@ abstract class HabitList : Iterable<Habit> {
/**
* Inserts a new habit in the list.
*
*
* If the id of the habit is null, the list will assign it a new id, which
* is guaranteed to be unique in the scope of the list. If id is not null,
* the caller should make sure that the list does not already contain
@@ -115,7 +114,6 @@ abstract class HabitList : Iterable<Habit> {
/**
* Removes the given habit from the list.
*
*
* If the given habit is not in the list, does nothing.
*
* @param h the habit to be removed.
@@ -151,7 +149,6 @@ abstract class HabitList : Iterable<Habit> {
/**
* Notifies the list that a certain list of habits has been modified.
*
*
* Depending on the implementation, this operation might trigger a write to
* disk, or do nothing at all. To make sure that the habits get persisted,
* this operation must be called.
@@ -163,7 +160,6 @@ abstract class HabitList : Iterable<Habit> {
/**
* Notifies the list that a certain habit has been modified.
*
*
* See [.update] for more details.
*
* @param habit the habit that has been modified.
@@ -212,6 +208,14 @@ abstract class HabitList : Iterable<Habit> {
abstract fun resort()
enum class Order {
BY_NAME_ASC, BY_NAME_DESC, BY_COLOR_ASC, BY_COLOR_DESC, BY_SCORE_ASC, BY_SCORE_DESC, BY_STATUS_ASC, BY_STATUS_DESC, BY_POSITION
BY_NAME_ASC,
BY_NAME_DESC,
BY_COLOR_ASC,
BY_COLOR_DESC,
BY_SCORE_ASC,
BY_SCORE_DESC,
BY_STATUS_ASC,
BY_STATUS_DESC,
BY_POSITION
}
}

View File

@@ -151,16 +151,18 @@ class MemoryHabitList : HabitList {
}
val statusComparatorAsc =
Comparator { h1: Habit, h2: Habit -> statusComparatorDesc.compare(h2, h1) }
if (order === Order.BY_POSITION) return positionComparator
if (order === Order.BY_NAME_ASC) return nameComparatorAsc
if (order === Order.BY_NAME_DESC) return nameComparatorDesc
if (order === Order.BY_COLOR_ASC) return colorComparatorAsc
if (order === Order.BY_COLOR_DESC) return colorComparatorDesc
if (order === Order.BY_SCORE_DESC) return scoreComparatorDesc
if (order === Order.BY_SCORE_ASC) return scoreComparatorAsc
if (order === Order.BY_STATUS_DESC) return statusComparatorDesc
if (order === Order.BY_STATUS_ASC) return statusComparatorAsc
throw IllegalStateException()
return when {
order === Order.BY_POSITION -> positionComparator
order === Order.BY_NAME_ASC -> nameComparatorAsc
order === Order.BY_NAME_DESC -> nameComparatorDesc
order === Order.BY_COLOR_ASC -> colorComparatorAsc
order === Order.BY_COLOR_DESC -> colorComparatorDesc
order === Order.BY_SCORE_DESC -> scoreComparatorDesc
order === Order.BY_SCORE_ASC -> scoreComparatorAsc
order === Order.BY_STATUS_DESC -> statusComparatorDesc
order === Order.BY_STATUS_ASC -> statusComparatorAsc
else -> throw IllegalStateException()
}
}
@Synchronized

View File

@@ -1,23 +0,0 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* 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/>.
*/
/**
* Provides in-memory implementation of core models.
*/
package org.isoron.uhabits.core.models.memory;

View File

@@ -1,23 +0,0 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* 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/>.
*/
/**
* Provides SQLite implementations of the core models.
*/
package org.isoron.uhabits.core.models.sqlite;

View File

@@ -29,7 +29,7 @@ import org.isoron.uhabits.core.models.sqlite.SQLiteEntryList
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
class HabitFixtures(private val modelFactory: ModelFactory, private val habitList: HabitList) {
var NON_DAILY_HABIT_CHECKS = booleanArrayOf(
private var NON_DAILY_HABIT_CHECKS = booleanArrayOf(
true, false, false, true, true, true, false, false, true, true
)

View File

@@ -123,7 +123,14 @@ open class ListHabitsBehavior @Inject constructor(
}
enum class Message {
COULD_NOT_EXPORT, IMPORT_SUCCESSFUL, IMPORT_FAILED, DATABASE_REPAIRED, COULD_NOT_GENERATE_BUG_REPORT, FILE_NOT_RECOGNIZED, SYNC_ENABLED, SYNC_KEY_ALREADY_INSTALLED
COULD_NOT_EXPORT,
IMPORT_SUCCESSFUL,
IMPORT_FAILED,
DATABASE_REPAIRED,
COULD_NOT_GENERATE_BUG_REPORT,
FILE_NOT_RECOGNIZED,
SYNC_ENABLED,
SYNC_KEY_ALREADY_INSTALLED
}
interface BugReporter {

View File

@@ -37,7 +37,7 @@ class ListHabitsSelectionMenuBehavior @Inject constructor(
var commandRunner: CommandRunner
) {
fun canArchive(): Boolean {
for ((_, _, _, _, isArchived) in adapter.selected) if (isArchived) return false
for (habit in adapter.selected) if (habit.isArchived) return false
return true
}
@@ -46,7 +46,7 @@ class ListHabitsSelectionMenuBehavior @Inject constructor(
}
fun canUnarchive(): Boolean {
for ((_, _, _, _, isArchived) in adapter.selected) if (!isArchived) return false
for (habit in adapter.selected) if (!habit.isArchived) return false
return true
}
@@ -56,23 +56,21 @@ class ListHabitsSelectionMenuBehavior @Inject constructor(
}
fun onChangeColor() {
val selected = adapter.selected
val (color) = selected[0]
val (color) = adapter.selected[0]
screen.showColorPicker(color) { selectedColor: PaletteColor ->
commandRunner.run(ChangeHabitColorCommand(habitList, selected, selectedColor))
commandRunner.run(ChangeHabitColorCommand(habitList, adapter.selected, selectedColor))
adapter.clearSelection()
}
}
fun onDeleteHabits() {
val selected = adapter.selected
screen.showDeleteConfirmationScreen(
{
adapter.performRemove(selected)
commandRunner.run(DeleteHabitsCommand(habitList, selected))
adapter.performRemove(adapter.selected)
commandRunner.run(DeleteHabitsCommand(habitList, adapter.selected))
adapter.clearSelection()
},
selected.size
adapter.selected.size
)
}

View File

@@ -18,19 +18,20 @@
*/
package org.isoron.uhabits.core.database
import junit.framework.Assert.assertNull
import org.apache.commons.lang3.builder.EqualsBuilder
import org.apache.commons.lang3.builder.HashCodeBuilder
import org.apache.commons.lang3.builder.ToStringBuilder
import org.hamcrest.MatcherAssert
import org.hamcrest.core.IsEqual
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.core.IsEqual.equalTo
import org.isoron.uhabits.core.BaseUnitTest
import org.junit.Assert
import org.junit.Before
import org.junit.Test
class RepositoryTest : BaseUnitTest() {
private lateinit var repository: Repository<ThingRecord>
private lateinit var db: Database
@Before
@Throws(Exception::class)
override fun setUp() {
@@ -54,11 +55,10 @@ class RepositoryTest : BaseUnitTest() {
"values (10, 20, 'hello', 8.0)"
)
val record = repository.find(10L)
Assert.assertNotNull(record)
MatcherAssert.assertThat(record!!.id, IsEqual.equalTo(10L))
MatcherAssert.assertThat(record.color, IsEqual.equalTo(20))
MatcherAssert.assertThat(record.name, IsEqual.equalTo("hello"))
MatcherAssert.assertThat(record.score, IsEqual.equalTo(8.0))
assertThat(record!!.id, equalTo(10L))
assertThat(record.color, equalTo(20))
assertThat(record.name, equalTo("hello"))
assertThat(record.score, equalTo(8.0))
}
@Test
@@ -71,11 +71,11 @@ class RepositoryTest : BaseUnitTest() {
score = 5.0
}
repository.save(record)
MatcherAssert.assertThat(record, IsEqual.equalTo(repository.find(50L)))
assertThat(record, equalTo(repository.find(50L)))
record.name = "world"
record.score = 128.0
repository.save(record)
MatcherAssert.assertThat(record, IsEqual.equalTo(repository.find(50L)))
assertThat(record, equalTo(repository.find(50L)))
}
@Test
@@ -88,9 +88,8 @@ class RepositoryTest : BaseUnitTest() {
}
repository.save(record)
val retrieved = repository.find(record.id!!)
Assert.assertNotNull(retrieved)
Assert.assertNull(retrieved!!.name)
MatcherAssert.assertThat(record, IsEqual.equalTo(retrieved))
assertNull(retrieved!!.name)
assertThat(record, equalTo(retrieved))
}
@Test
@@ -108,8 +107,8 @@ class RepositoryTest : BaseUnitTest() {
score = 2.0
}
repository.save(r2)
MatcherAssert.assertThat(r1.id, IsEqual.equalTo(1L))
MatcherAssert.assertThat(r2.id, IsEqual.equalTo(2L))
assertThat(r1.id, equalTo(1L))
assertThat(r2.id, equalTo(2L))
}
@Test
@@ -128,14 +127,14 @@ class RepositoryTest : BaseUnitTest() {
}
repository.save(rec2)
val id = rec1.id!!
MatcherAssert.assertThat(rec1, IsEqual.equalTo(repository.find(id)))
MatcherAssert.assertThat(rec2, IsEqual.equalTo(repository.find(rec2.id!!)))
assertThat(rec1, equalTo(repository.find(id)))
assertThat(rec2, 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!!)))
assertThat(rec1.id, equalTo(null))
assertNull(repository.find(id))
assertThat(rec2, equalTo(repository.find(rec2.id!!)))
repository.remove(rec1) // should have no effect
Assert.assertNull(repository.find(id))
assertNull(repository.find(id))
}
@Table(name = "tests")