Skip to content

Commit

Permalink
Merge pull request #56 from galihif/improve-notif
Browse files Browse the repository at this point in the history
Limit daily reminders of take programs
  • Loading branch information
ariefzuhri authored Nov 5, 2023
2 parents 2bcbe35 + 8b3b4d7 commit b89c9cc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 33 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.giftech.terbit.domain.usecase

import com.giftech.terbit.data.repository.ProgramRepository
import com.giftech.terbit.domain.enums.NotificationType
import com.giftech.terbit.domain.enums.ProgramTag
import com.giftech.terbit.domain.model.Notification
import com.giftech.terbit.domain.repository.INotificationRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.mapLatest
import javax.inject.Inject

class GetEligibleDailyNotificationListUseCase @Inject constructor(
private val notificationRepository: INotificationRepository,
private val programRepository: ProgramRepository,
) {

@OptIn(ExperimentalCoroutinesApi::class)
suspend operator fun invoke(): Flow<List<Notification>> {
return programRepository.getAll()
.flatMapLatest { programList ->
notificationRepository.getAll()
.mapLatest { notificationList ->
val preTestProgramList = programList.filter {
it.tag == ProgramTag.PRE_TEST
}
val weeklyProgramList = programList.filter {
it.tag == ProgramTag.WEEKLY_PROGRAM
}
val isPreTestDone = preTestProgramList.all { it.isCompleted }
val isWeeklyProgramDone = weeklyProgramList.all { it.isCompleted }

notificationList.filter {
when (it.id) {
6000 -> isPreTestDone && !isWeeklyProgramDone
else -> it.type == NotificationType.DAILY_TIPS
}
}

}
.flowOn(Dispatchers.IO)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.lifecycle.viewModelScope
import com.giftech.terbit.domain.model.Notification
import com.giftech.terbit.domain.model.UserNotification
import com.giftech.terbit.domain.usecase.GetAllUserNotificationListUseCase
import com.giftech.terbit.domain.usecase.GetDailyNotificationListUseCase
import com.giftech.terbit.domain.usecase.GetEligibleDailyNotificationListUseCase
import com.giftech.terbit.domain.usecase.MonitorNotificationUseCase
import com.giftech.terbit.domain.usecase.UpdateSchedulingStatusUserNotificationUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -20,7 +20,7 @@ class MainViewModel @Inject constructor(
private val getAllUserNotificationUseCase: GetAllUserNotificationListUseCase,
private val monitorNotificationUseCase: MonitorNotificationUseCase,
private val updateSchedulingStatusUserNotificationUseCase: UpdateSchedulingStatusUserNotificationUseCase,
private val getDailyNotificationListUseCase: GetDailyNotificationListUseCase,
private val getEligibleDailyNotificationListUseCase: GetEligibleDailyNotificationListUseCase,
): ViewModel(){

init {
Expand Down Expand Up @@ -50,7 +50,7 @@ class MainViewModel @Inject constructor(

fun getDailyNotificationList(): LiveData<List<Notification>> {
return liveData(context = viewModelScope.coroutineContext) {
getDailyNotificationListUseCase()
getEligibleDailyNotificationListUseCase()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Service
import android.content.Intent
import android.os.IBinder
import com.giftech.terbit.R
import com.giftech.terbit.domain.usecase.GetDailyNotificationListUseCase
import com.giftech.terbit.domain.usecase.GetEligibleDailyNotificationListUseCase
import com.giftech.terbit.domain.usecase.GetUnshownUserNotificationListUseCase
import com.giftech.terbit.presentation.util.RemindersManager.startReminder
import com.giftech.terbit.presentation.util.buildHiddenNotification
Expand All @@ -26,7 +26,7 @@ class NotificationRestartService : Service() {
lateinit var getUnshownUserNotificationListUseCase: GetUnshownUserNotificationListUseCase

@Inject
lateinit var getDailyNotificationListUseCase: GetDailyNotificationListUseCase
lateinit var getEligibleDailyNotificationListUseCase: GetEligibleDailyNotificationListUseCase

override fun onCreate() {
super.onCreate()
Expand Down Expand Up @@ -74,7 +74,7 @@ class NotificationRestartService : Service() {

private fun restartDailyTips() {
scope.launch {
getDailyNotificationListUseCase().collect { notificationList ->
getEligibleDailyNotificationListUseCase().collect { notificationList ->
notificationList.forEach {
startReminder(
context = this@NotificationRestartService,
Expand Down

0 comments on commit b89c9cc

Please sign in to comment.