Skip to content

Commit 541cd0a

Browse files
committed
add selected day title to wear statistics
1 parent 9151a62 commit 541cd0a

File tree

64 files changed

+661
-353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+661
-353
lines changed

app/src/main/java/com/example/util/simpletimetracker/di/AppModuleBinds.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.example.util.simpletimetracker.di
22

3-
import com.example.util.simpletimetracker.core.common.repo.BaseResourceRepo
3+
import com.example.util.simpletimetracker.core.repo.BaseResourceRepo
44
import com.example.util.simpletimetracker.core.interactor.GetCurrentDayInteractorImpl
55
import com.example.util.simpletimetracker.core.interactor.IsSystemInDarkModeInteractorImpl
66
import com.example.util.simpletimetracker.core.interactor.GetUntrackedRecordsInteractorImpl

core/common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ android {
1414
}
1515

1616
dependencies {
17+
api(project(":domain:common"))
1718
implementation(project(":resources"))
1819

1920
implementation(libs.google.dagger)

core/common/src/main/java/com/example/util/simpletimetracker/core/common/mapper/CommonTimeMapper.kt

Lines changed: 0 additions & 73 deletions
This file was deleted.

core/common/src/main/java/com/example/util/simpletimetracker/core/common/repo/BaseResourceRepo.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.util.simpletimetracker.core.extension
2+
3+
import java.util.Calendar
4+
5+
fun Calendar.setToStartOfDay() {
6+
set(Calendar.HOUR_OF_DAY, 0)
7+
set(Calendar.MINUTE, 0)
8+
set(Calendar.SECOND, 0)
9+
set(Calendar.MILLISECOND, 0)
10+
}
11+
12+
fun Calendar.setWeekToFirstDay() {
13+
val another = Calendar.getInstance()
14+
another.timeInMillis = timeInMillis
15+
16+
val currentTime = another.timeInMillis
17+
// Setting DAY_OF_WEEK have a weird behaviour so as if after that another field is set -
18+
// it would reset to current day. Use another calendar to manipulate day of week and get its time.
19+
another.set(Calendar.DAY_OF_WEEK, firstDayOfWeek)
20+
// If went to future - go back a week
21+
if (another.timeInMillis > currentTime) {
22+
another.add(Calendar.DATE, -7)
23+
}
24+
25+
timeInMillis = another.timeInMillis
26+
}
27+
28+
fun Calendar.shift(shift: Long): Calendar {
29+
// Example
30+
// shift 6h
31+
// before 2023-03-26T00:00+01:00[Europe/Amsterdam] DST_OFFSET = 0
32+
// after 2023-03-26T07:00+02:00[Europe/Amsterdam] DST_OFFSET = 3600000
33+
// need to compensate one hour.
34+
val dstOffsetBefore = get(Calendar.DST_OFFSET)
35+
timeInMillis += shift
36+
val dstOffsetAfter = get(Calendar.DST_OFFSET)
37+
timeInMillis += (dstOffsetBefore - dstOffsetAfter)
38+
return this
39+
}
40+
41+
fun Calendar.shiftTimeStamp(timestamp: Long, shift: Long): Long {
42+
timeInMillis = timestamp
43+
shift(shift)
44+
return timeInMillis
45+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.example.util.simpletimetracker.core.mapper
2+
3+
import com.example.util.simpletimetracker.core.common.R
4+
import com.example.util.simpletimetracker.core.repo.BaseResourceRepo
5+
import com.example.util.simpletimetracker.domain.daysOfWeek.model.DayOfWeek
6+
import com.example.util.simpletimetracker.domain.record.model.Range
7+
import com.example.util.simpletimetracker.domain.statistics.model.RangeLength
8+
import javax.inject.Inject
9+
10+
class RangeTitleMapper @Inject constructor(
11+
private val resourceRepo: BaseResourceRepo,
12+
private val timeMapper: TimeMapper,
13+
) {
14+
15+
fun mapToTitle(
16+
rangeLength: RangeLength,
17+
position: Int,
18+
startOfDayShift: Long,
19+
firstDayOfWeek: DayOfWeek,
20+
useShortCustomRange: Boolean = false,
21+
): String {
22+
return when (rangeLength) {
23+
is RangeLength.Day -> timeMapper.toDayTitle(position, startOfDayShift)
24+
is RangeLength.Week -> timeMapper.toWeekTitle(position, startOfDayShift, firstDayOfWeek)
25+
is RangeLength.Month -> timeMapper.toMonthTitle(position, startOfDayShift)
26+
is RangeLength.Year -> timeMapper.toYearTitle(position, startOfDayShift)
27+
is RangeLength.All -> resourceRepo.getString(R.string.range_overall)
28+
is RangeLength.Last -> mapToLastDaysTitle(rangeLength.days, position, startOfDayShift, firstDayOfWeek)
29+
is RangeLength.Custom -> if (useShortCustomRange) {
30+
mapToSelectRangeName()
31+
} else {
32+
mapToCustomRangeTitle(rangeLength.range, position, startOfDayShift, firstDayOfWeek)
33+
}
34+
}
35+
}
36+
37+
fun mapToSelectRangeName(): String {
38+
return resourceRepo.getString(R.string.range_custom)
39+
}
40+
41+
private fun mapToCustomRangeTitle(range: Range): String {
42+
// Time ended is the end of selected day, meaning the beginning on the next day.
43+
return timeMapper.formatDate(range.timeStarted) +
44+
" - " +
45+
timeMapper.formatDate(range.timeEnded - 1)
46+
}
47+
48+
private fun mapToCustomRangeTitle(
49+
range: Range,
50+
position: Int,
51+
startOfDayShift: Long,
52+
firstDayOfWeek: DayOfWeek,
53+
): String {
54+
val shiftedRange = timeMapper.getRangeStartAndEnd(
55+
rangeLength = RangeLength.Custom(range),
56+
shift = position,
57+
firstDayOfWeek = firstDayOfWeek,
58+
startOfDayShift = startOfDayShift,
59+
)
60+
return mapToCustomRangeTitle(shiftedRange)
61+
}
62+
63+
fun mapToLastDaysTitle(days: Int): String {
64+
return resourceRepo.getQuantityString(R.plurals.range_last, days, days)
65+
}
66+
67+
private fun mapToLastDaysTitle(
68+
days: Int,
69+
position: Int,
70+
startOfDayShift: Long,
71+
firstDayOfWeek: DayOfWeek,
72+
): String {
73+
return if (position == 0) {
74+
mapToLastDaysTitle(days)
75+
} else {
76+
timeMapper.getRangeStartAndEnd(
77+
rangeLength = RangeLength.Last(days),
78+
shift = position,
79+
firstDayOfWeek = firstDayOfWeek,
80+
startOfDayShift = startOfDayShift,
81+
).let(::mapToCustomRangeTitle)
82+
}
83+
}
84+
}

core/common/src/main/java/com/example/util/simpletimetracker/core/common/mapper/StatisticsMapper.kt renamed to core/common/src/main/java/com/example/util/simpletimetracker/core/mapper/StatisticsMapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.util.simpletimetracker.core.common.mapper
1+
package com.example.util.simpletimetracker.core.mapper
22

33
import javax.inject.Inject
44
import kotlin.math.roundToLong

core/src/main/java/com/example/util/simpletimetracker/core/mapper/TimeMapper.kt renamed to core/common/src/main/java/com/example/util/simpletimetracker/core/mapper/TimeMapper.kt

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.example.util.simpletimetracker.core.mapper
22

3-
import com.example.util.simpletimetracker.core.R
4-
import com.example.util.simpletimetracker.core.common.mapper.CommonTimeMapper
3+
import com.example.util.simpletimetracker.core.common.R
54
import com.example.util.simpletimetracker.core.extension.setToStartOfDay
65
import com.example.util.simpletimetracker.core.extension.setWeekToFirstDay
76
import com.example.util.simpletimetracker.core.extension.shift
87
import com.example.util.simpletimetracker.core.provider.LocaleProvider
9-
import com.example.util.simpletimetracker.core.repo.ResourceRepo
8+
import com.example.util.simpletimetracker.core.repo.BaseResourceRepo
109
import com.example.util.simpletimetracker.domain.base.CurrentTimestampProvider
1110
import com.example.util.simpletimetracker.domain.daysOfWeek.model.DayOfWeek
1211
import com.example.util.simpletimetracker.domain.extension.padDuration
@@ -17,12 +16,12 @@ import java.util.Calendar
1716
import java.util.Locale
1817
import java.util.concurrent.TimeUnit
1918
import javax.inject.Inject
19+
import kotlin.math.abs
2020

2121
class TimeMapper @Inject constructor(
2222
localeProvider: LocaleProvider,
23-
private val resourceRepo: ResourceRepo,
23+
private val resourceRepo: BaseResourceRepo,
2424
private val currentTimestampProvider: CurrentTimestampProvider,
25-
private val commonTimeMapper: CommonTimeMapper,
2625
) {
2726

2827
private val locale: Locale by lazy { localeProvider.get() }
@@ -468,16 +467,56 @@ class TimeMapper @Inject constructor(
468467
.getActualMaximum(field)
469468
}
470469

470+
/**
471+
* @param forceSeconds - true 1h 7m 21s, false 1h 7m
472+
* @param useProportionalMinutes - true 1.25h
473+
*/
471474
fun formatInterval(
472475
interval: Long,
473476
forceSeconds: Boolean,
474477
useProportionalMinutes: Boolean,
475478
): String {
476-
return commonTimeMapper.formatInterval(
477-
interval = interval,
478-
forceSeconds = forceSeconds,
479-
useProportionalMinutes = useProportionalMinutes,
479+
val hourString = resourceRepo.getString(R.string.time_hour)
480+
val minuteString = resourceRepo.getString(R.string.time_minute)
481+
val secondString = resourceRepo.getString(R.string.time_second)
482+
483+
val hr: Long = TimeUnit.MILLISECONDS.toHours(
484+
abs(interval),
485+
)
486+
val min: Long = TimeUnit.MILLISECONDS.toMinutes(
487+
abs(interval) - TimeUnit.HOURS.toMillis(hr),
488+
)
489+
val sec: Long = TimeUnit.MILLISECONDS.toSeconds(
490+
abs(interval) - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min),
480491
)
492+
493+
if (useProportionalMinutes) {
494+
return formatIntervalProportional(hr, min)
495+
}
496+
497+
val willShowHours: Boolean
498+
val willShowMinutes: Boolean
499+
val willShowSeconds: Boolean
500+
501+
if (forceSeconds) {
502+
willShowHours = hr != 0L
503+
willShowMinutes = willShowHours || min != 0L
504+
willShowSeconds = true
505+
} else {
506+
willShowHours = hr != 0L
507+
willShowMinutes = true
508+
willShowSeconds = false
509+
}
510+
511+
var res = ""
512+
if (willShowHours) res += "$hr$hourString "
513+
if (willShowMinutes) res += "$min$minuteString"
514+
if (willShowMinutes && willShowSeconds) res += " "
515+
if (willShowSeconds) res += "$sec$secondString"
516+
517+
res = if (interval < 0) "-$res" else res
518+
519+
return res
481520
}
482521

483522
@Suppress("unused")
@@ -516,6 +555,15 @@ class TimeMapper @Inject constructor(
516555
}
517556
}
518557

558+
private fun formatIntervalProportional(hr: Long, min: Long): String {
559+
val hourString = resourceRepo.getString(R.string.time_hour)
560+
val minutesProportion = min / 60f
561+
val proportional = hr + minutesProportion
562+
val proportionalString = "%.2f".format(proportional)
563+
564+
return "$proportionalString$hourString"
565+
}
566+
519567
fun toDayDateTitle(
520568
daysFromToday: Int,
521569
startOfDayShift: Long,

core/src/main/java/com/example/util/simpletimetracker/core/provider/ContextProvider.kt renamed to core/common/src/main/java/com/example/util/simpletimetracker/core/provider/ContextProvider.kt

File renamed without changes.

core/src/main/java/com/example/util/simpletimetracker/core/provider/LocaleProvider.kt renamed to core/common/src/main/java/com/example/util/simpletimetracker/core/provider/LocaleProvider.kt

File renamed without changes.

0 commit comments

Comments
 (0)