Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ class SettingsBackupTest : BaseUiTest() {
)
}
commentList.first().let {
testUtils.addFavouriteComment(it.comment)
testUtils.addFavouriteComment(it.comment, null)
}
iconsList.first().let {
testUtils.addFavouriteIcon(it.icon)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,7 @@ class SettingsTest : BaseUiTest() {
pressBack()

// Has favourite comments
testUtils.addFavouriteComment(favComment1)
testUtils.addFavouriteComment(favComment1, null)
clickOnViewWithText(nameComments)
checkViewDoesNotExist(withText(coreR.string.change_record_similar_comments_hint))
checkHint(coreR.string.change_record_last_comments_hint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RecordCommentSearchViewDataInteractor @Inject constructor(
val result = mutableListOf<ViewHolderType>()

val similar = getSimilarData(comment)
val favourite = getFavouriteData()
val favourite = getFavouriteData(typeId)
val last = getLastCommentsData(typeId)

val filters = getFilters(
Expand Down Expand Up @@ -121,9 +121,13 @@ class RecordCommentSearchViewDataInteractor @Inject constructor(
}
}

private suspend fun getFavouriteData(): List<ViewHolderType> {
return favouriteCommentInteractor.getAll()
.map { RecordCommentViewData.Favourite(it.comment) }
private suspend fun getFavouriteData(
typeId: Long,
): List<ViewHolderType> {
return favouriteCommentInteractor.getByTypeId(typeId)
.map { it.comment }
.toSet()
.map { RecordCommentViewData.Favourite(it) }
}

private suspend fun getLastCommentsData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,9 @@ class TestUtils @Inject constructor(

fun addFavouriteComment(
text: String,
typeId: Long?,
) = runBlocking {
favouriteCommentInteractor.add(FavouriteComment(comment = text))
favouriteCommentInteractor.add(FavouriteComment(comment = text, typeId = typeId))
}

fun addFavouriteIcon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 32,
"identityHash": "50c6fb0f1aafe1784918f027568edeb4",
"identityHash": "6b692ab798afc87801d928d25cbeaee0",
"entities": [
{
"tableName": "records",
Expand Down Expand Up @@ -522,7 +522,7 @@
},
{
"tableName": "favouriteComments",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `comment` TEXT NOT NULL)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `comment` TEXT NOT NULL, `type_id` INTEGER)",
"fields": [
{
"fieldPath": "id",
Expand All @@ -535,6 +535,12 @@
"columnName": "comment",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "typeId",
"columnName": "type_id",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
Expand Down Expand Up @@ -964,7 +970,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '50c6fb0f1aafe1784918f027568edeb4')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '6b692ab798afc87801d928d25cbeaee0')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,10 @@ class BackupRepoImpl @Inject constructor(

private fun toBackupString(favouriteComment: FavouriteComment): String {
return String.format(
"$ROW_FAVOURITE_COMMENT\t%s\t%s\n",
"$ROW_FAVOURITE_COMMENT\t%s\t%s\t%s\n",
favouriteComment.id.toString(),
favouriteComment.comment.cleanTabs().replaceNewline(),
favouriteComment.typeId?.toString().orEmpty(),
)
}

Expand Down Expand Up @@ -891,6 +892,7 @@ class BackupRepoImpl @Inject constructor(
id = parts.getOrNull(1)?.toLongOrNull().orZero(),
comment = parts.getOrNull(2)?.restoreNewline()
?.takeUnless(String::isEmpty) ?: return null,
typeId = parts.getOrNull(3)?.toLongOrNull(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AppDatabaseMigrations {
migration_29_30,
migration_30_31,
migration_31_32,
migration_32_33,
)

private val migration_1_2 = object : Migration(1, 2) {
Expand Down Expand Up @@ -407,5 +408,13 @@ class AppDatabaseMigrations {
)
}
}

private val migration_32_33 = object : Migration(32, 33) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"ALTER TABLE `favouriteComments` ADD COLUMN type_id INTEGER",
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ data class FavouriteCommentDBO(

@ColumnInfo(name = "comment")
val comment: String,

@ColumnInfo(name = "type_id")
val typeId: Long?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ interface FavouriteCommentDao {
@Query("SELECT * FROM favouriteComments")
suspend fun getAll(): List<FavouriteCommentDBO>

@Query("SELECT * FROM favouriteComments WHERE type_id IS NULL OR type_id = :typeId")
suspend fun getByTypeId(typeId: Long): List<FavouriteCommentDBO>

@Query("SELECT * FROM favouriteComments WHERE id = :id LIMIT 1")
suspend fun get(id: Long): FavouriteCommentDBO?

@Query("SELECT * FROM favouriteComments WHERE comment = :text LIMIT 1")
suspend fun get(text: String): FavouriteCommentDBO?
@Query("""
SELECT * FROM favouriteComments WHERE comment = :text AND
((:typeId IS NULL AND type_id IS NULL) OR (:typeId IS NOT NULL AND type_id = :typeId)) LIMIT 1
""")
suspend fun get(text: String, typeId: Long?): FavouriteCommentDBO?

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(comment: FavouriteCommentDBO): Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class FavouriteCommentDataLocalMapper @Inject constructor() {
return FavouriteComment(
id = dbo.id,
comment = dbo.comment,
typeId = dbo.typeId,
)
}

fun map(domain: FavouriteComment): FavouriteCommentDBO {
return FavouriteCommentDBO(
id = domain.id,
comment = domain.comment,
typeId = domain.typeId,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ class FavouriteCommentRepoImpl @Inject constructor(
dao.getAll().map(mapper::map)
}

override suspend fun getByTypeId(typeId: Long): List<FavouriteComment> = withContext(Dispatchers.IO) {
logDataAccess("get by type")
dao.getAll().map(mapper::map)
}

override suspend fun get(id: Long): FavouriteComment? = withContext(Dispatchers.IO) {
logDataAccess("get id")
dao.get(id)?.let(mapper::map)
}

override suspend fun get(text: String): FavouriteComment? = withContext(Dispatchers.IO) {
override suspend fun get(text: String, typeId: Long?): FavouriteComment? = withContext(Dispatchers.IO) {
logDataAccess("get text")
dao.get(text)?.let(mapper::map)
dao.get(text, typeId)?.let(mapper::map)
}

override suspend fun add(comment: FavouriteComment): Long = withContext(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ class FavouriteCommentInteractor @Inject constructor(
return repo.getAll().let(::sort)
}

suspend fun get(text: String): FavouriteComment? {
return repo.get(text)
suspend fun getByTypeId(typeId: Long): List<FavouriteComment> {
return repo.getByTypeId(typeId).let(::sort)
}

suspend fun get(text: String, typeId: Long?): FavouriteComment? {
return repo.get(text, typeId)
}

suspend fun add(comment: FavouriteComment): Long {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.example.util.simpletimetracker.domain.favourite.model
data class FavouriteComment(
val id: Long = 0,
val comment: String,
val typeId: Long?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ interface FavouriteCommentRepo {

suspend fun getAll(): List<FavouriteComment>

suspend fun getByTypeId(typeId: Long): List<FavouriteComment>

suspend fun get(id: Long): FavouriteComment?

suspend fun get(text: String): FavouriteComment?
suspend fun get(text: String, typeId: Long?): FavouriteComment?

suspend fun add(comment: FavouriteComment): Long

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import androidx.core.widget.doAfterTextChanged
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.createRecyclerBindingAdapterDelegate
import com.example.util.simpletimetracker.feature_views.extension.setOnClick
import com.example.util.simpletimetracker.feature_views.extension.setOnLongClick
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordCommentFieldViewData as ViewData
import com.example.util.simpletimetracker.feature_change_record.databinding.ChangeRecordCommentFieldItemBinding as Binding

fun createChangeRecordCommentFieldAdapterDelegate(
afterTextChange: (String) -> Unit,
onFavouriteClick: () -> Unit,
onFavouriteLongClick: () -> Unit,
) = createRecyclerBindingAdapterDelegate<ViewData, Binding>(
Binding::inflate,
) { binding, item, _ ->
Expand All @@ -32,6 +34,7 @@ fun createChangeRecordCommentFieldAdapterDelegate(
ColorStateList.valueOf(item.iconColor),
)
btnChangeRecordFavouriteComment.setOnClick { onFavouriteClick() }
btnChangeRecordFavouriteComment.setOnLongClick { onFavouriteLongClick() }

etChangeRecordCommentField.removeTextChangedListener(textWatcher)
textWatcher = etChangeRecordCommentField.doAfterTextChanged { afterTextChange(it.toString()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class ChangeRecordViewDataInteractor @Inject constructor(
): List<ViewHolderType> {
val items = mutableListOf<ViewHolderType>()
val isDarkTheme = prefsInteractor.getDarkMode()
val isFavourite = favouriteCommentInteractor.get(comment) != null
val isFavourite = (
favouriteCommentInteractor.get(comment, typeId) != null ||
favouriteCommentInteractor.get(comment, null) != null
)

ChangeRecordCommentFieldViewData(
// Only one at the time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ChangeRecordCore(
createChangeRecordCommentFieldAdapterDelegate(
afterTextChange = viewModel::onCommentChange,
onFavouriteClick = viewModel::onFavouriteCommentClick,
onFavouriteLongClick = viewModel::onFavouriteCommentLongClick,
),
createRecordCommentAdapterDelegate(
onItemClick = viewModel::onCommentClick,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,24 @@ abstract class ChangeRecordBaseViewModel(
if (newComment.isEmpty()) return

viewModelScope.launch {
favouriteCommentInteractor.get(newComment)
favouriteCommentInteractor.get(newComment, newTypeId)
?.let { favouriteCommentInteractor.remove(it.id) }
?: run {
val new = FavouriteComment(comment = newComment)
val new = FavouriteComment(comment = newComment, typeId = newTypeId)
favouriteCommentInteractor.add(new)
}
updateCommentsViewData()
}
}

fun onFavouriteCommentLongClick() {
if (newComment.isEmpty()) return

viewModelScope.launch {
favouriteCommentInteractor.get(newComment, null)
?.let { favouriteCommentInteractor.remove(it.id) }
?: run {
val new = FavouriteComment(comment = newComment, typeId = null)
favouriteCommentInteractor.add(new)
}
updateCommentsViewData()
Expand Down