Skip to content

Commit 17ceae0

Browse files
committed
detailed statistics for tag values from prefs
1 parent 0dc3652 commit 17ceae0

File tree

22 files changed

+193
-110
lines changed

22 files changed

+193
-110
lines changed

app/src/androidTest/java/com/example/util/simpletimetracker/StatisticsDetailTagValueTest.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,11 @@ class StatisticsDetailTagValueTest : BaseUiTest() {
237237

238238
private fun clickOnChartMode(withTextId: Int) {
239239
clickOnTagValuesSettings()
240-
clickOnSettingsSelectorBesideText(R.string.statistics_detail_tag_values_hint)
241-
clickOnViewWithText(withTextId)
240+
try {
241+
checkViewIsDisplayed(withText(withTextId))
242+
} catch (_: Exception) {
243+
clickOnSettingsSelectorBesideText(R.string.statistics_detail_tag_values_hint)
244+
}
242245
pressBack()
243246
}
244247

data_local/src/main/java/com/example/util/simpletimetracker/data_local/backup/BackupPrefsRepo.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import com.example.util.simpletimetracker.data_local.prefs.PrefsRepoImpl.Compani
7676
import com.example.util.simpletimetracker.data_local.prefs.PrefsRepoImpl.Companion.KEY_STATISTICS_DETAIL_RANGE_CUSTOM_END
7777
import com.example.util.simpletimetracker.data_local.prefs.PrefsRepoImpl.Companion.KEY_STATISTICS_DETAIL_RANGE_CUSTOM_START
7878
import com.example.util.simpletimetracker.data_local.prefs.PrefsRepoImpl.Companion.KEY_STATISTICS_DETAIL_RANGE_LAST_DAYS
79+
import com.example.util.simpletimetracker.data_local.prefs.PrefsRepoImpl.Companion.KEY_STATISTICS_DETAIL_TAG_VALUE_SETTINGS
7980
import com.example.util.simpletimetracker.data_local.prefs.PrefsRepoImpl.Companion.KEY_STATISTICS_DETAIL_STREAK_TYPE
8081
import com.example.util.simpletimetracker.data_local.prefs.PrefsRepoImpl.Companion.KEY_STATISTICS_RANGE
8182
import com.example.util.simpletimetracker.data_local.prefs.PrefsRepoImpl.Companion.KEY_STATISTICS_RANGE_CUSTOM_END
@@ -172,6 +173,7 @@ class BackupPrefsRepo @Inject constructor(
172173
PrefsProcessor(KEY_STATISTICS_DETAIL_RANGE_CUSTOM_START, ::statisticsDetailRangeCustomStart),
173174
PrefsProcessor(KEY_STATISTICS_DETAIL_RANGE_CUSTOM_END, ::statisticsDetailRangeCustomEnd),
174175
PrefsProcessor(KEY_STATISTICS_DETAIL_RANGE_LAST_DAYS, ::statisticsDetailRangeLastDays),
176+
PrefsProcessor(KEY_STATISTICS_DETAIL_TAG_VALUE_SETTINGS, ::statisticsDetailTagValueSettings),
175177
PrefsProcessor(KEY_FILE_EXPORT_RANGE, ::fileExportRange),
176178
PrefsProcessor(KEY_FILE_EXPORT_RANGE_CUSTOM_START, ::fileExportRangeCustomStart),
177179
PrefsProcessor(KEY_FILE_EXPORT_RANGE_CUSTOM_END, ::fileExportRangeCustomEnd),

data_local/src/main/java/com/example/util/simpletimetracker/data_local/prefs/PrefsRepoImpl.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ class PrefsRepoImpl @Inject constructor(
8383
KEY_TAG_ORDER_MANUAL, emptySet(),
8484
)
8585

86+
override var statisticsDetailTagValueSettings: Set<String> by prefs.delegate(
87+
KEY_STATISTICS_DETAIL_TAG_VALUE_SETTINGS, emptySet(),
88+
)
89+
8690
override var statisticsRange: Int by prefs.delegate(
8791
KEY_STATISTICS_RANGE, 0,
8892
)
@@ -723,6 +727,7 @@ class PrefsRepoImpl @Inject constructor(
723727
const val KEY_STATISTICS_DETAIL_RANGE_CUSTOM_START = "statisticsDetailRangeCustomStart"
724728
const val KEY_STATISTICS_DETAIL_RANGE_CUSTOM_END = "statisticsDetailRangeCustomEnd"
725729
const val KEY_STATISTICS_DETAIL_RANGE_LAST_DAYS = "statisticsDetailRangeLastDays"
730+
const val KEY_STATISTICS_DETAIL_TAG_VALUE_SETTINGS = "statisticsDetailTagValueSettings"
726731
const val KEY_FILE_EXPORT_RANGE = "fileExportRange"
727732
const val KEY_FILE_EXPORT_RANGE_CUSTOM_START = "fileExportRangeCustomStart"
728733
const val KEY_FILE_EXPORT_RANGE_CUSTOM_END = "fileExportRangeCustomEnd"

domain/src/main/java/com/example/util/simpletimetracker/domain/prefs/interactor/PrefsInteractor.kt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import com.example.util.simpletimetracker.domain.record.model.RepeatButtonType
1616
import com.example.util.simpletimetracker.domain.recordTag.model.CardTagOrder
1717
import com.example.util.simpletimetracker.domain.recordType.model.CardOrder
1818
import com.example.util.simpletimetracker.domain.statistics.model.ChartFilterType
19+
import com.example.util.simpletimetracker.domain.statistics.model.ChartValueMode
1920
import com.example.util.simpletimetracker.domain.statistics.model.RangeLength
21+
import com.example.util.simpletimetracker.domain.statistics.model.StatisticsDetailTagValueSettings
2022
import com.example.util.simpletimetracker.domain.statistics.model.StatisticsStreaksType
2123
import com.example.util.simpletimetracker.domain.widget.model.GridWidgetData
2224
import com.example.util.simpletimetracker.domain.widget.model.StatisticsWidgetData
@@ -201,6 +203,24 @@ class PrefsInteractor @Inject constructor(
201203
prefsRepo.statisticsDetailRangeLastDays
202204
}
203205

206+
suspend fun getStatisticsDetailTagValueSettings(
207+
tagId: Long,
208+
): StatisticsDetailTagValueSettings = withContext(Dispatchers.IO) {
209+
prefsRepo.statisticsDetailTagValueSettings
210+
.let(::mapStatisticsDetailTagValueSettings)[tagId]
211+
?: StatisticsDetailTagValueSettings.getDefault(tagId)
212+
}
213+
214+
suspend fun setStatisticsDetailTagValueSettings(
215+
value: StatisticsDetailTagValueSettings,
216+
) = withContext(Dispatchers.IO) {
217+
val settings = prefsRepo.statisticsDetailTagValueSettings
218+
.let(::mapStatisticsDetailTagValueSettings)
219+
.toMutableMap().apply { put(value.tagId, value) }
220+
.let { serializeStatisticsDetailTagValueSettings(it.values) }
221+
prefsRepo.statisticsDetailTagValueSettings = settings
222+
}
223+
204224
suspend fun getFileExportRange(): RangeLength = withContext(Dispatchers.IO) {
205225
mapToRange(
206226
value = prefsRepo.fileExportRange,
@@ -1262,7 +1282,50 @@ class PrefsInteractor @Inject constructor(
12621282
?: emptyMap()
12631283
}
12641284

1285+
private fun mapStatisticsDetailTagValueSettings(
1286+
set: Set<String>?,
1287+
): Map<Long, StatisticsDetailTagValueSettings> {
1288+
return set?.mapNotNull { string ->
1289+
string.split(STATISTICS_DETAIL_TAG_VALUE_DELIMITER).let { parts ->
1290+
val tagId = parts.getOrNull(0)?.toLongOrNull() ?: return@mapNotNull null
1291+
val modeInt = parts.getOrNull(1)?.toIntOrNull().orZero()
1292+
val multiplyInt = parts.getOrNull(2)?.toIntOrNull().orZero()
1293+
val chartValueMode = when (modeInt) {
1294+
0 -> ChartValueMode.TOTAL
1295+
1 -> ChartValueMode.AVERAGE
1296+
else -> ChartValueMode.TOTAL
1297+
}
1298+
val multiplyDuration = multiplyInt == 1
1299+
tagId to StatisticsDetailTagValueSettings(
1300+
tagId = tagId,
1301+
chartValueMode = chartValueMode,
1302+
multiplyDuration = multiplyDuration,
1303+
)
1304+
}
1305+
}?.toMap().orEmpty()
1306+
}
1307+
1308+
private fun serializeStatisticsDetailTagValueSettings(
1309+
values: Collection<StatisticsDetailTagValueSettings>,
1310+
): Set<String> {
1311+
return values.map { settings ->
1312+
buildString {
1313+
append(settings.tagId)
1314+
append(STATISTICS_DETAIL_TAG_VALUE_DELIMITER)
1315+
append(
1316+
when (settings.chartValueMode) {
1317+
ChartValueMode.TOTAL -> 0
1318+
ChartValueMode.AVERAGE -> 1
1319+
},
1320+
)
1321+
append(STATISTICS_DETAIL_TAG_VALUE_DELIMITER)
1322+
append(if (settings.multiplyDuration) 1 else 0)
1323+
}
1324+
}.toSet()
1325+
}
1326+
12651327
companion object {
12661328
private const val CARDS_ORDER_DELIMITER = "_"
1329+
private const val STATISTICS_DETAIL_TAG_VALUE_DELIMITER = "_"
12671330
}
12681331
}

domain/src/main/java/com/example/util/simpletimetracker/domain/prefs/repo/PrefsRepo.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ interface PrefsRepo {
3535
var statisticsDetailRangeCustomEnd: Long
3636
var statisticsDetailRangeLastDays: Int
3737

38+
var statisticsDetailTagValueSettings: Set<String>
39+
3840
var fileExportRange: Int
3941
var fileExportRangeCustomStart: Long
4042
var fileExportRangeCustomEnd: Long
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.util.simpletimetracker.domain.statistics.model
2+
3+
enum class ChartValueMode {
4+
TOTAL,
5+
AVERAGE,
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example.util.simpletimetracker.domain.statistics.model
2+
3+
data class StatisticsDetailTagValueSettings(
4+
val tagId: Long,
5+
val chartValueMode: ChartValueMode,
6+
val multiplyDuration: Boolean,
7+
) {
8+
9+
companion object {
10+
fun getDefault(tagId: Long = -1L): StatisticsDetailTagValueSettings {
11+
return StatisticsDetailTagValueSettings(
12+
tagId = tagId,
13+
chartValueMode = ChartValueMode.TOTAL,
14+
multiplyDuration = false,
15+
)
16+
}
17+
}
18+
}

features/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/interactor/StatisticsDetailChartInteractor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartG
2828
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartLength
2929
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartMode
3030
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartSplitSortMode
31-
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartValueMode
31+
import com.example.util.simpletimetracker.domain.statistics.model.ChartValueMode
3232
import com.example.util.simpletimetracker.feature_statistics_detail.viewData.StatisticsDetailChartCompositeViewData
3333
import kotlinx.coroutines.Dispatchers
3434
import kotlinx.coroutines.withContext

features/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/interactor/StatisticsDetailGoalsInteractor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import com.example.util.simpletimetracker.feature_statistics_detail.mapper.Stati
1818
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartGrouping
1919
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartLength
2020
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartSplitSortMode
21-
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartValueMode
21+
import com.example.util.simpletimetracker.domain.statistics.model.ChartValueMode
2222
import com.example.util.simpletimetracker.feature_statistics_detail.viewData.StatisticsDetailGoalsCompositeViewData
2323
import kotlinx.coroutines.Dispatchers
2424
import kotlinx.coroutines.withContext

features/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/interactor/StatisticsDetailTagValueInteractor.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartG
1616
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartLength
1717
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartMode
1818
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartSplitSortMode
19-
import com.example.util.simpletimetracker.feature_statistics_detail.model.ChartValueMode
19+
import com.example.util.simpletimetracker.domain.statistics.model.ChartValueMode
2020
import com.example.util.simpletimetracker.feature_statistics_detail.viewData.StatisticsDetailTagValuesCompositeViewData
2121
import kotlinx.coroutines.Dispatchers
2222
import kotlinx.coroutines.withContext
@@ -33,23 +33,15 @@ class StatisticsDetailTagValueInteractor @Inject constructor(
3333

3434
// TODO compare?
3535
suspend fun getViewData(
36+
valuedTag: RecordTag,
3637
records: List<RecordBase>,
37-
filter: List<RecordsFilter>,
3838
currentChartGrouping: ChartGrouping,
3939
currentChartLength: ChartLength,
4040
currentChartValueMode: ChartValueMode,
4141
multiplyDuration: Boolean,
4242
rangeLength: RangeLength,
4343
rangePosition: Int,
4444
): StatisticsDetailTagValuesCompositeViewData = withContext(Dispatchers.Default) {
45-
val tags = recordTagInteractor.getAll()
46-
val valuedTag = getSingleSelectedTagWithValue(filter, tags)
47-
?: return@withContext StatisticsDetailTagValuesCompositeViewData(
48-
viewData = emptyList(),
49-
appliedChartGrouping = currentChartGrouping,
50-
appliedChartLength = currentChartLength,
51-
)
52-
5345
val firstDayOfWeek = prefsInteractor.getFirstDayOfWeek()
5446
val startOfDayShift = prefsInteractor.getStartOfDayShift()
5547
val durationFormat = prefsInteractor.getDurationFormat()
@@ -130,6 +122,13 @@ class StatisticsDetailTagValueInteractor @Inject constructor(
130122
)
131123
}
132124

125+
suspend fun getSelectedTagWithValueId(
126+
filter: List<RecordsFilter>,
127+
): RecordTag? = withContext(Dispatchers.Default) {
128+
val tags = recordTagInteractor.getAll()
129+
getSingleSelectedTagWithValue(filter, tags)
130+
}
131+
133132
private fun getSingleSelectedTagWithValue(
134133
filter: List<RecordsFilter>,
135134
tags: List<RecordTag>,

0 commit comments

Comments
 (0)