Skip to content

Commit

Permalink
Merge pull request #325 from joeloewi7178/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
joeloewi7178 authored Jan 11, 2024
2 parents c99e039 + fba827d commit 30ef100
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {

defaultConfig {
applicationId = "com.joeloewi.croissant"
versionCode = 47
versionCode = 48
versionName = "1.2.2"
targetSdk = 34

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.core.content.getSystemService
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.LifecycleResumeEffect
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionState
import com.google.accompanist.permissions.PermissionStatus
Expand Down Expand Up @@ -65,26 +63,16 @@ fun rememberSpecialPermissionState(
onPermissionResult: (Boolean) -> Unit = {}
): SpecialPermissionState {
val context = LocalContext.current
val lifecycle = LocalLifecycleOwner.current.lifecycle
val permissionState = remember(specialPermission) {
SpecialPermissionState(specialPermission.name, context)
}
// Refresh the permission status when the lifecycle is resumed
val permissionCheckerObserver = remember(permissionState) {
LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME) {
// If the permission is revoked, check again.
// We don't check if the permission was denied as that triggers a process restart.
if (permissionState.status != PermissionStatus.Granted) {
permissionState.refreshPermissionStatus()
}
}
LifecycleResumeEffect(permissionState) {
if (permissionState.status != PermissionStatus.Granted) {
permissionState.refreshPermissionStatus()
}
}

DisposableEffect(lifecycle, permissionCheckerObserver) {
lifecycle.addObserver(permissionCheckerObserver)
onDispose { lifecycle.removeObserver(permissionCheckerObserver) }
onPauseOrDispose { }
}

val launcher =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.Instant
import java.time.ZoneId
import java.time.ZonedDateTime
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -166,11 +167,6 @@ class AttendanceDetailViewModel @Inject constructor(

workManager.cancelUniqueWork(attendance.attendCheckInEventWorkerName.toString())

alarmScheduler.scheduleCheckInAlarm(
attendance = attendance,
scheduleForTomorrow = false
)

val periodicCheckSessionWork = PeriodicWorkRequest.Builder(
CheckSessionWorker::class.java,
6L,
Expand All @@ -190,19 +186,24 @@ class AttendanceDetailViewModel @Inject constructor(
periodicCheckSessionWork
)

updateAttendanceUseCase(
attendance.copy(
modifiedAt = System.currentTimeMillis(),
cookie = _cookie.value,
nickname = _nickname.value,
uid = _uid.value,
hourOfDay = _hourOfDay.value,
minute = _minute.value,
timezoneId = ZoneId.systemDefault().id,
checkSessionWorkerId = periodicCheckSessionWork.id
)
val newAttendance = attendance.copy(
modifiedAt = Instant.now().toEpochMilli(),
cookie = _cookie.value,
nickname = _nickname.value,
uid = _uid.value,
hourOfDay = _hourOfDay.value,
minute = _minute.value,
timezoneId = ZoneId.systemDefault().id,
checkSessionWorkerId = periodicCheckSessionWork.id
)

alarmScheduler.scheduleCheckInAlarm(
attendance = newAttendance,
scheduleForTomorrow = false
)

updateAttendanceUseCase(newAttendance)

val games = attendanceWithGames.games
val originalGames = arrayListOf<Game>()
val newGames = arrayListOf<Game>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ class AttendanceLogsCalendarViewModel @Inject constructor(
val deleteAllState = _deleteAllState.asStateFlow()
val attendanceId = savedStateHandle.get<Long>(_attendanceIdKey) ?: Long.MIN_VALUE
val resultCounts = loggableWorker.flatMapLatest {
getAllResultCountUseCase(it)
getAllResultCountUseCase(attendanceId, it)
}.map {
it.toImmutableList()
}.flowOn(Dispatchers.IO).stateIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
initialValue = persistentListOf()
)
val startToEnd = loggableWorker.flatMapLatest {
getStartToEnd(it).map {
val startToEnd = loggableWorker.flatMapLatest { loggableWorker ->
getStartToEnd(attendanceId, loggableWorker).map {
ZonedDateTime.ofInstant(
Instant.ofEpochMilli(it.start),
ZoneId.systemDefault()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class ResultCountRepositoryImpl @Inject constructor(
private val resultCountDataSource: ResultCountDataSource
) : ResultCountRepository {

override fun getAll(loggableWorker: LoggableWorker): Flow<List<ResultCount>> =
resultCountDataSource.getAll(loggableWorker).flowOn(Dispatchers.IO)
override fun getAll(
attendanceId: Long,
loggableWorker: LoggableWorker
): Flow<List<ResultCount>> =
resultCountDataSource.getAll(attendanceId, loggableWorker).flowOn(Dispatchers.IO)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import javax.inject.Inject
class ResultRangeRepositoryImpl @Inject constructor(
private val resultRangeDataSource: ResultRangeDataSource
) : ResultRangeRepository {
override fun getStartToEnd(loggableWorker: LoggableWorker): Flow<ResultRange> =
resultRangeDataSource.getStartToEnd(loggableWorker)
override fun getStartToEnd(
attendanceId: Long,
loggableWorker: LoggableWorker
): Flow<ResultRange> = resultRangeDataSource.getStartToEnd(attendanceId, loggableWorker)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.Flow
interface ResultCountDataSource {

fun getAll(
attendanceId: Long,
loggableWorker: LoggableWorker
): Flow<List<ResultCount>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ import kotlinx.coroutines.flow.Flow

interface ResultRangeDataSource {

fun getStartToEnd(loggableWorker: LoggableWorker): Flow<ResultRange>
fun getStartToEnd(
attendanceId: Long,
loggableWorker: LoggableWorker
): Flow<ResultRange>
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class ResultCountDataSourceImpl @Inject constructor(
private val resultCountDao: ResultCountDao
) : ResultCountDataSource {
override fun getAll(
attendanceId: Long,
loggableWorker: LoggableWorker
): Flow<List<ResultCount>> {
val query = """
SELECT
DATE(createdAt / 1000, 'unixepoch', 'localtime') as date,
COUNT(CASE state WHEN "${WorkerExecutionLogState.SUCCESS.name}" THEN 1 ELSE NULL END) as successCount,
COUNT(CASE state WHEN "${WorkerExecutionLogState.FAILURE.name}" THEN 1 ELSE NULL END) as failureCount
COUNT(CASE state WHEN '${WorkerExecutionLogState.SUCCESS.name}' THEN 1 ELSE NULL END) as successCount,
COUNT(CASE state WHEN '${WorkerExecutionLogState.FAILURE.name}' THEN 1 ELSE NULL END) as failureCount
FROM (SELECT * FROM WorkerExecutionLogEntity ORDER BY attendanceId)
WHERE loggableWorker = "${loggableWorker.name}"
WHERE loggableWorker = '${loggableWorker.name}'
AND attendanceId = '${attendanceId}'
GROUP BY DATE((createdAt + 0.00) / 1000, 'unixepoch', 'localtime')
""".trimIndent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import javax.inject.Inject
class ResultRangeDataSourceImpl @Inject constructor(
private val resultRangeDao: ResultRangeDao
) : ResultRangeDataSource {
override fun getStartToEnd(loggableWorker: LoggableWorker): Flow<ResultRange> {
override fun getStartToEnd(
attendanceId: Long,
loggableWorker: LoggableWorker
): Flow<ResultRange> {
val query = """
SELECT
MIN(
Expand All @@ -33,7 +36,8 @@ class ResultRangeDataSourceImpl @Inject constructor(
FROM (
SELECT *
FROM WorkerExecutionLogEntity
WHERE loggableWorker = "${loggableWorker.name}"
WHERE loggableWorker = '${loggableWorker.name}'
AND attendanceId = '${attendanceId}'
)
""".trimIndent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ import kotlinx.coroutines.flow.Flow

interface ResultCountRepository {

fun getAll(loggableWorker: LoggableWorker): Flow<List<ResultCount>>
fun getAll(
attendanceId: Long,
loggableWorker: LoggableWorker
): Flow<List<ResultCount>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ import kotlinx.coroutines.flow.Flow

interface ResultRangeRepository {

fun getStartToEnd(loggableWorker: LoggableWorker): Flow<ResultRange>
fun getStartToEnd(
attendanceId: Long,
loggableWorker: LoggableWorker
): Flow<ResultRange>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ sealed class ResultCountUseCase {
private val resultCountRepository: ResultCountRepository
) : ResultCountUseCase() {

operator fun invoke(loggableWorker: LoggableWorker) =
resultCountRepository.getAll(loggableWorker)
operator fun invoke(
attendanceId: Long,
loggableWorker: LoggableWorker
) = resultCountRepository.getAll(attendanceId, loggableWorker)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ sealed class ResultRangeUseCase {
class GetStartToEnd @Inject constructor(
private val resultRangeRepository: ResultRangeRepository
) : ResultRangeUseCase() {
operator fun invoke(loggableWorker: LoggableWorker) =
resultRangeRepository.getStartToEnd(loggableWorker)
operator fun invoke(
attendanceId: Long,
loggableWorker: LoggableWorker
) = resultRangeRepository.getStartToEnd(attendanceId, loggableWorker)
}
}

0 comments on commit 30ef100

Please sign in to comment.