Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
Fix history headers showing wrong count
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkitSuda committed Nov 25, 2022
1 parent eb77b05 commit 0309ec2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.ankitsuda.base.utils.generateId
import com.ankitsuda.rebound.domain.LogSetType
import com.ankitsuda.rebound.domain.entities.*
import kotlinx.coroutines.flow.Flow
import java.time.LocalDate
import java.time.LocalDateTime

@Dao
Expand Down Expand Up @@ -160,7 +159,7 @@ date(start_at / 1000,'unixepoch') >= date(:dateStart / 1000,'unixepoch') AND
date(start_at / 1000,'unixepoch') <= date(:dateEnd / 1000,'unixepoch')
AND is_hidden = 0 AND in_progress = 0 GROUP BY start_at)
""")
fun getWorkoutsCountOnMonthOnDateRangeAlt(dateStart: Long, dateEnd: Long): Flow<Long>
fun getWorkoutsCountOnDateRangeAlt(dateStart: Long, dateEnd: Long): Flow<Long>

@Query("""
SELECT SUM(count) FROM (SELECT COUNT(*) as count FROM workouts WHERE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.map
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery
import com.ankitsuda.base.util.NONE_WORKOUT_ID
import com.ankitsuda.base.utils.generateId
import com.ankitsuda.base.utils.toEpochMillis
Expand All @@ -32,10 +31,8 @@ import com.ankitsuda.rebound.domain.addIfNot
import com.ankitsuda.rebound.domain.entities.*
import kotlinx.coroutines.flow.*
import timber.log.Timber
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
import javax.inject.Inject

Expand Down Expand Up @@ -326,8 +323,8 @@ class WorkoutsRepository @Inject constructor(
dateEnd = dateEnd.toEpochMillis()
)

fun getWorkoutsCountOnMonthOnDateRangeAlt(dateStart: LocalDate, dateEnd: LocalDate) =
workoutsDao.getWorkoutsCountOnMonthOnDateRangeAlt(
fun getWorkoutsCountOnDateRangeAlt(dateStart: LocalDate, dateEnd: LocalDate) =
workoutsDao.getWorkoutsCountOnDateRangeAlt(
dateStart = dateStart.toEpochMillis(),
dateEnd = dateEnd.toEpochMillis()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,37 @@ class HistoryScreenViewModel @Inject constructor(
private fun mapData(pagingData: PagingData<WorkoutWithExtraInfo>) =
pagingData.insertSeparators { before, after ->
if (after != null) {
val afterDate = after.workout?.completedAt?.toLocalDate()
val afterDate = after.workout?.startAt?.toLocalDate()
?.with(TemporalAdjusters.firstDayOfMonth())

val beforeDate = before?.workout?.completedAt?.toLocalDate()
val beforeDate = before?.workout?.startAt?.toLocalDate()
?.with(TemporalAdjusters.firstDayOfMonth())

if (after.workout?.completedAt != null && afterDate != null && beforeDate != afterDate) {
if (after.workout?.startAt != null && afterDate != null && beforeDate != afterDate) {

if (
dateRangeType != WorkoutsDateRangeType.YEAR &&
dateRangeType != WorkoutsDateRangeType.ALL &&
dateStart != null && dateEnd != null
) {
workoutsRepository.getWorkoutsCountOnMonthOnDateRangeAlt(
workoutsRepository.getWorkoutsCountOnDateRangeAlt(
dateStart,
dateEnd
).firstOrNull() ?: 0L
} else {
val year = after.workout!!.startAt!!.year
val month = after.workout!!.startAt!!.month
CountWithDate(
date = afterDate.toEpochMillis(),
count = workoutsRepository.getWorkoutsCountOnMonth(
date = after.workout!!.completedAt!!.toEpochMillis()
count = workoutsRepository.getWorkoutsCountOnDateRangeAlt(
dateStart = YearMonth.of(
year,
month
).atDay(1),
dateEnd = YearMonth.of(
year,
month
).atEndOfMonth()
).firstOrNull() ?: 0
)
}
Expand Down

0 comments on commit 0309ec2

Please sign in to comment.