Skip to content

Commit

Permalink
change edit record ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Apr 21, 2024
1 parent 4d21855 commit 38fa985
Show file tree
Hide file tree
Showing 31 changed files with 883 additions and 565 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Throttler : LifecycleOwner {
}

fun <T, U> throttle(
destinationFunction: (T, U) -> Unit,
destinationFunction: (T, U) -> Any,
): (T, U) -> Unit = { param1, param2 ->
throttler { destinationFunction(param1, param2) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.util.simpletimetracker.core.extension

import com.example.util.simpletimetracker.core.mapper.TimeMapper
import com.example.util.simpletimetracker.domain.model.Range
import com.example.util.simpletimetracker.domain.model.RecordsFilter
import com.example.util.simpletimetracker.feature_base_adapter.runningRecord.GoalTimeViewData
import com.example.util.simpletimetracker.feature_views.viewData.RecordTypeIcon
import com.example.util.simpletimetracker.navigation.params.screen.ChangeRecordParams
import com.example.util.simpletimetracker.navigation.params.screen.ChangeRunningRecordParams
import com.example.util.simpletimetracker.navigation.params.screen.RangeParams
import com.example.util.simpletimetracker.navigation.params.screen.RecordTypeIconParams
Expand Down Expand Up @@ -51,6 +53,34 @@ fun GoalTimeViewData.toParams(): ChangeRunningRecordParams.Preview.GoalTimeParam
)
}

fun ChangeRecordParams.Preview.DateTime.toViewData(): TimeMapper.DateTime {
return TimeMapper.DateTime(
date = date,
time = time,
)
}

fun TimeMapper.DateTime.toRecordParams(): ChangeRecordParams.Preview.DateTime {
return ChangeRecordParams.Preview.DateTime(
date = date,
time = time
)
}

fun ChangeRunningRecordParams.Preview.DateTime.toViewData(): TimeMapper.DateTime {
return TimeMapper.DateTime(
date = date,
time = time,
)
}

fun TimeMapper.DateTime.toRunningRecordParams(): ChangeRunningRecordParams.Preview.DateTime {
return ChangeRunningRecordParams.Preview.DateTime(
date = date,
time = time
)
}

fun RecordsFilterParam.toModel(): RecordsFilter {
return when (this) {
is RecordsFilterParam.Untracked -> RecordsFilter.Untracked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,78 +24,87 @@ class RecordTagViewDataInteractor @Inject constructor(
showAddButton: Boolean,
showArchived: Boolean,
showUntaggedButton: Boolean,
): List<ViewHolderType> {
): Result {
val isDarkTheme = prefsInteractor.getDarkMode()
val recordTags = getSelectableTagsInteractor.execute(typeId)
val types = recordTypeInteractor.getAll().associateBy { it.id }

return recordTags
val data = recordTags
.let { tags -> if (showArchived) tags else tags.filterNot { it.archived } }
.takeUnless { it.isEmpty() }
?.let { tags ->
val selected = tags.filter { it.id in selectedTags }
val available = tags.filter { it.id !in selectedTags }
selected to available
}
?.let { (selected, available) ->
val viewData = mutableListOf<ViewHolderType>()

listOf(
categoryViewDataMapper.mapToRecordTagHint(),
DividerViewData(1),
).takeIf { showAddButton }?.let(viewData::addAll)
return if (data.isNotEmpty()) {
val selected = data.filter { it.id in selectedTags }
val available = data.filter { it.id !in selectedTags }

categoryViewDataMapper.mapSelectedCategoriesHint(
isEmpty = selected.isEmpty(),
).takeIf { multipleChoiceAvailable }?.let(viewData::add)
val viewData = mutableListOf<ViewHolderType>()

selected.map {
categoryViewDataMapper.mapRecordTag(
tag = it,
type = types[it.iconColorSource],
isDarkTheme = isDarkTheme,
)
}.let(viewData::addAll)
listOf(
categoryViewDataMapper.mapToRecordTagHint(),
DividerViewData(1),
).takeIf { showAddButton }?.let(viewData::addAll)

DividerViewData(2)
.takeUnless { available.isEmpty() }
.takeIf { multipleChoiceAvailable }
?.let(viewData::add)
categoryViewDataMapper.mapSelectedCategoriesHint(
isEmpty = selected.isEmpty(),
).takeIf { multipleChoiceAvailable }?.let(viewData::add)

available.map {
categoryViewDataMapper.mapRecordTag(
tag = it,
type = types[it.iconColorSource],
isDarkTheme = isDarkTheme,
)
}.let(viewData::addAll)
selected.map {
categoryViewDataMapper.mapRecordTag(
tag = it,
type = types[it.iconColorSource],
isDarkTheme = isDarkTheme,
)
}.let(viewData::addAll)

if (showUntaggedButton) {
if (selected.isNotEmpty() || available.isNotEmpty()) {
DividerViewData(3)
.takeIf { multipleChoiceAvailable }
?.let(viewData::add)
categoryViewDataMapper.mapToUntaggedItem(
isDarkTheme = isDarkTheme,
isFiltered = false,
).let(viewData::add)
}
}
DividerViewData(2)
.takeUnless { available.isEmpty() }
.takeIf { multipleChoiceAvailable }
?.let(viewData::add)

categoryViewDataMapper.mapToRecordTagAddItem(isDarkTheme)
.takeIf { showAddButton }
?.let(viewData::add)
available.map {
categoryViewDataMapper.mapRecordTag(
tag = it,
type = types[it.iconColorSource],
isDarkTheme = isDarkTheme,
)
}.let(viewData::addAll)

viewData
if (showUntaggedButton) {
if (selected.isNotEmpty() || available.isNotEmpty()) {
DividerViewData(3)
.takeIf { multipleChoiceAvailable }
?.let(viewData::add)
categoryViewDataMapper.mapToUntaggedItem(
isDarkTheme = isDarkTheme,
isFiltered = false,
).let(viewData::add)
}
}
?: listOfNotNull(
if (showAddButton && recordTagInteractor.isEmpty()) {
categoryViewDataMapper.mapToTagsFirstHint()
} else {
categoryViewDataMapper.mapToRecordTagsEmpty()
},
categoryViewDataMapper.mapToRecordTagAddItem(isDarkTheme)
.takeIf { showAddButton },

categoryViewDataMapper.mapToRecordTagAddItem(isDarkTheme)
.takeIf { showAddButton }
?.let(viewData::add)

Result(
selectedCount = selected.size,
data = viewData,
)
} else {
Result(
selectedCount = 0,
data = listOfNotNull(
if (showAddButton && recordTagInteractor.isEmpty()) {
categoryViewDataMapper.mapToTagsFirstHint()
} else {
categoryViewDataMapper.mapToRecordTagsEmpty()
},
categoryViewDataMapper.mapToRecordTagAddItem(isDarkTheme)
.takeIf { showAddButton },
),
)
}
}

data class Result(
val selectedCount: Int,
val data: List<ViewHolderType>,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class RunningRecordViewDataMapper @Inject constructor(
useMilitaryTime = useMilitaryTime,
showSeconds = showSeconds,
),
timeStartedTimestamp = runningRecord.timeStarted,
timer = timeMapper.formatInterval(
interval = currentDuration,
forceSeconds = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class TimeMapper @Inject constructor(
private val timeFormatMilitary by lazy { SimpleDateFormat("HH:mm", locale) }
private val timeFormatMilitaryWithSeconds by lazy { SimpleDateFormat("HH:mm:ss", locale) }

private val dateFormat by lazy { SimpleDateFormat("MMM d", locale) }
private val dateFormatWithSeconds by lazy { SimpleDateFormat("MMM d", locale) }
private val dateFormatMilitary by lazy { SimpleDateFormat("MMM d", locale) }
private val dateFormatMilitaryWithSeconds by lazy { SimpleDateFormat("MMM d", locale) }

private val dateTimeFormat by lazy { SimpleDateFormat("MMM d h:mm a", locale) }
private val dateTimeFormatWithSeconds by lazy { SimpleDateFormat("MMM d h:mm:ss a", locale) }
private val dateTimeFormatMilitary by lazy { SimpleDateFormat("MMM d HH:mm", locale) }
Expand Down Expand Up @@ -63,6 +68,19 @@ class TimeMapper @Inject constructor(
}.format(time)
}

// Mar 11
fun formatDate(
time: Long,
useMilitaryTime: Boolean,
showSeconds: Boolean,
): String = synchronized(lock) {
return if (useMilitaryTime) {
if (showSeconds) dateFormatMilitaryWithSeconds else dateFormatMilitary
} else {
if (showSeconds) dateFormatWithSeconds else dateFormat
}.format(time)
}

// Mar 11 12:21
fun formatDateTime(
time: Long,
Expand Down Expand Up @@ -632,11 +650,35 @@ class TimeMapper @Inject constructor(
.let(::toDayOfWeek)
}

fun getFormattedDateTime(
time: Long,
useMilitaryTime: Boolean,
showSeconds: Boolean,
): DateTime {
return DateTime(
date = formatDate(
time = time,
useMilitaryTime = useMilitaryTime,
showSeconds = showSeconds,
),
time = formatTime(
time = time,
useMilitaryTime = useMilitaryTime,
showSeconds = showSeconds,
),
)
}

private fun isFirstWeekOfNextYear(calendar: Calendar): Boolean {
return calendar.get(Calendar.WEEK_OF_YEAR) == 1 &&
calendar.get(Calendar.MONTH) == calendar.getActualMaximum(Calendar.MONTH)
}

data class DateTime(
val date: String,
val time: String,
)

companion object {
private const val MINUTES_IN_MILLIS = 60_000
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class RemoveRecordViewModel @Inject constructor(
) : ViewModel() {

val deleteButtonEnabled: LiveData<Boolean> = MutableLiveData()
val deleteIconVisibility: LiveData<Boolean> = MutableLiveData()
val message: LiveData<SnackBarParams?> = MutableLiveData()
val needUpdate: LiveData<Boolean> = MutableLiveData()

Expand All @@ -35,7 +34,6 @@ class RemoveRecordViewModel @Inject constructor(
fun prepare(id: Long) {
recordId = id
(deleteButtonEnabled as MutableLiveData).value = true
(deleteIconVisibility as MutableLiveData).value = id != 0L
}

fun onDeleteClick(from: ChangeRecordParams.From?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class TimeAdjustmentView @JvmOverloads constructor(
private fun initEditMode() {
if (isInEditMode) {
List(6) {
ViewData.Adjust(text = "Button $it", value = it.toLong())
ViewData.Adjust(text = "Btn $it", value = it.toLong())
}.let(adapter::replace)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sealed class RecordViewData : ViewHolderType {

data class Tracked(
val id: Long,
override val timeStartedTimestamp: Long, // TODO remove?
override val timeStartedTimestamp: Long,
override val timeEndedTimestamp: Long,
override val name: String,
override val tagName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ data class RunningRecordViewData(
val name: String,
val tagName: String,
val timeStarted: String,
val timeStartedTimestamp: Long,
val timer: String,
val timerTotal: String,
val goalTime: GoalTimeViewData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package com.example.util.simpletimetracker.feature_change_record.mapper
import com.example.util.simpletimetracker.core.mapper.ColorMapper
import com.example.util.simpletimetracker.core.mapper.IconMapper
import com.example.util.simpletimetracker.core.mapper.TimeMapper
import com.example.util.simpletimetracker.core.repo.ResourceRepo
import com.example.util.simpletimetracker.domain.extension.getFullName
import com.example.util.simpletimetracker.domain.model.Record
import com.example.util.simpletimetracker.domain.model.RecordTag
import com.example.util.simpletimetracker.domain.model.RecordType
import com.example.util.simpletimetracker.feature_change_record.R
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordViewData
import javax.inject.Inject

class ChangeRecordViewDataMapper @Inject constructor(
private val iconMapper: IconMapper,
private val colorMapper: ColorMapper,
private val timeMapper: TimeMapper,
private val resourceRepo: ResourceRepo,
) {

fun map(
Expand All @@ -26,7 +29,8 @@ class ChangeRecordViewDataMapper @Inject constructor(
showSeconds: Boolean,
): ChangeRecordViewData {
return ChangeRecordViewData(
name = recordType?.name.orEmpty(),
name = recordType?.name
?: resourceRepo.getString(R.string.untracked_time_name),
tagName = recordTags
.getFullName(),
timeStarted = timeMapper.formatTime(
Expand All @@ -39,12 +43,12 @@ class ChangeRecordViewDataMapper @Inject constructor(
useMilitaryTime = useMilitaryTime,
showSeconds = showSeconds,
),
dateTimeStarted = timeMapper.formatDateTime(
dateTimeStarted = timeMapper.getFormattedDateTime(
time = record.timeStarted,
useMilitaryTime = useMilitaryTime,
showSeconds = showSeconds,
showSeconds = showSeconds
),
dateTimeFinished = timeMapper.formatDateTime(
dateTimeFinished = timeMapper.getFormattedDateTime(
time = record.timeEnded,
useMilitaryTime = useMilitaryTime,
showSeconds = showSeconds,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.util.simpletimetracker.feature_change_record.model

// TODO remove?
enum class TimeAdjustmentState {
HIDDEN,
TIME_STARTED,
Expand Down
Loading

0 comments on commit 38fa985

Please sign in to comment.