Skip to content

Commit

Permalink
Merge pull request #135 from TeamHY2/Fix/#132-timer-stop
Browse files Browse the repository at this point in the history
[Fix] 타이머가 doze 모드에서 멈추는 현상 해결
  • Loading branch information
librarywon authored Nov 19, 2024
2 parents d1b7fcb + 6032c99 commit 9f0af63
Show file tree
Hide file tree
Showing 42 changed files with 378 additions and 155 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ android {
}

dependencies {
implementation(projects.app.notification)

implementation(projects.core.notification)
implementation(projects.core.designsystem)
implementation(projects.core.remote)
implementation(projects.core.auth)
Expand Down

This file was deleted.

This file was deleted.

14 changes: 11 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />


<application
android:name=".app.HongikYeolgong2Application"
android:name=".app.HongikYeolgong2Application"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -17,6 +20,11 @@
android:theme="@style/Theme.HongikYeolgong2"
tools:targetApi="31">

<service
android:name=".timer.presentation.TimerForegroundService"
android:exported="false"
android:foregroundServiceType="dataSync"/>

</application>

</manifest>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.teamhy2.hongikyeolgong2.notification

import android.app.Notification

interface NotificationHandler {
fun buildServiceNotification(): Notification

fun buildGeneralNotification(contentText: String): Notification

fun showSimpleNotification(pushText: PushText)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.teamhy2.hongikyeolgong2.notification

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object NotificationModule {
@Provides
@Singleton
fun providesCoroutineScope(): CoroutineScope {
return CoroutineScope(SupervisorJob())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.teamhy2.hongikyeolgong2.notification

import androidx.annotation.StringRes

enum class PushText(
@StringRes val id: Int,
) {
THIRTY_MINUTES(R.string.notification_content_thirty_minutes_remain),
TEN_MINUTES(R.string.notification_content_ten_minutes_remain),
ZERO_MINUTES(R.string.notification_content_zero_minutes_remain),
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="notification_title">홍익열공이 열람실 연장 알림!</string>
<string name="notification_content_thirty_minutes_remain">열람실 시간 종료 30분 전이에요.\n지금부터 열람실 연장이 가능해요!</string>
<string name="notification_content_ten_minutes_remain">열람실 시간 종료 10분 전이에요.\n열람실 연장이 필요하다면 서둘러주세요!</string>
<string name="notification_content_zero_minutes_remain">열람실 이용이 종료되었어요!</string>
</resources>
2 changes: 1 addition & 1 deletion main-presentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ android {
}

dependencies {
implementation(projects.app.notification)

implementation(projects.mainDomain)

implementation(projects.core.notification)
implementation(projects.core.remote)

implementation(projects.calendarPresentation)
Expand Down
1 change: 1 addition & 0 deletions main-presentation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<activity
android:name="com.teamhy2.feature.main.MainActivity"
android:launchMode="singleInstance"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ import com.teamhy2.feature.home.component.RunningTimerComponent
import com.teamhy2.feature.home.component.WeeklyStudyCalendar
import com.teamhy2.feature.home.model.HomeUiState
import com.teamhy2.hongikyeolgong2.main.presentation.R
import com.teamhy2.hongikyeolgong2.notification.PushText
import com.teamhy2.hongikyeolgong2.timer.model.Timer
import com.teamhy2.hongikyeolgong2.timer.prsentation.TimerViewModel
import com.teamhy2.hongikyeolgong2.timer.prsentation.model.TimerUiModel
import com.teamhy2.hongikyeolgong2.timer.presentation.TimerViewModel
import com.teamhy2.hongikyeolgong2.timer.presentation.model.TimerUiModel
import com.teamhy2.main.domain.model.WeeklyStudyDay
import com.teamhy2.main.domain.model.WiseSaying
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -50,7 +49,6 @@ import java.time.temporal.ChronoUnit
@Composable
fun HomeRoute(
seatingChartUrl: String,
onSendNotification: (PushText) -> Unit,
modifier: Modifier = Modifier,
homeViewModel: HomeViewModel = hiltViewModel(),
) {
Expand Down Expand Up @@ -92,17 +90,23 @@ fun HomeRoute(

is HomeUiState.Success -> {
val uiState = homeUiState as HomeUiState.Success

if (uiState.isTimePickerVisible) {
HY2TimePicker(
title = stringResource(R.string.main_study_room_use_start_time),
onSelected = { selectedTime ->
val updatedSelectedTime =
selectedTime.plusSeconds(LocalDateTime.now().second.toLong())

homeViewModel.run {
updateSelectedTime(selectedTime)
updateSelectedTime(updatedSelectedTime)
updateTimePickerVisibility(false)
updateTimerRunning(true)
}
startTimer(selectedTime, homeViewModel, timerViewModel, onSendNotification)
startTimer(updatedSelectedTime, homeViewModel, timerViewModel)
homeViewModel.startTimerService(
startTime = updatedSelectedTime,
duration = timerViewModel.durationHour.value,
)
},
onCancelled = {
homeViewModel.updateTimePickerVisibility(false)
Expand Down Expand Up @@ -132,7 +136,11 @@ fun HomeRoute(
LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES),
homeViewModel,
timerViewModel,
onSendNotification,
)
homeViewModel.stopTimerService()
homeViewModel.startTimerService(
startTime = LocalDateTime.now(),
duration = timerViewModel.durationHour.value,
)
},
onDismiss = {
Expand All @@ -153,6 +161,7 @@ fun HomeRoute(
homeViewModel.updateStudyRoomEndDialogVisibility(false)
homeViewModel.updateTimerRunning(false)
homeViewModel.saveStudyDay(false)
homeViewModel.stopTimerService()
},
onDismiss = {
homeViewModel.updateStudyRoomEndDialogVisibility(false)
Expand Down Expand Up @@ -206,19 +215,12 @@ private fun startTimer(
startTime: LocalDateTime,
homeViewModel: HomeViewModel,
timerViewModel: TimerViewModel,
onSendNotification: (PushText) -> Unit,
) {
timerViewModel.setTimer(
startTime = startTime,
events =
mapOf(
Timer.THIRTY_MINUTES_SECONDS to {
onSendNotification(PushText.THIRTY_MINUTES)
},
Timer.TEN_MINUTES_SECONDS to {
onSendNotification(PushText.TEN_MINUTES)
},
Timer.TIME_OVER_SECONDS to {
Timer.TIME_OVER to {
homeViewModel.updateTimerRunning(false)
homeViewModel.saveStudyDay(false)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.teamhy2.feature.home
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.teamhy2.feature.home.model.HomeUiState
import com.teamhy2.hongikyeolgong2.timer.prsentation.model.TimerUiModel
import com.teamhy2.hongikyeolgong2.timer.model.TimerService
import com.teamhy2.hongikyeolgong2.timer.presentation.model.TimerUiModel
import com.teamhy2.main.domain.model.WeeklyStudyDay
import com.teamhy2.main.domain.model.WiseSaying
import com.teamhy2.main.domain.repository.StudyDayRepository
Expand All @@ -19,6 +20,7 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.time.Duration
import java.time.LocalDateTime
import java.time.LocalTime
import javax.inject.Inject
Expand All @@ -29,6 +31,7 @@ class HomeViewModel
constructor(
private val wiseSayingRepository: WiseSayingRepository,
private val studyDayRepository: StudyDayRepository,
private val timerService: TimerService,
) : ViewModel() {
private val _homeUiState = MutableStateFlow<HomeUiState>(HomeUiState.Loading)
val homeUiState: StateFlow<HomeUiState> = _homeUiState.asStateFlow()
Expand Down Expand Up @@ -63,6 +66,17 @@ class HomeViewModel
}
}

fun startTimerService(
startTime: LocalDateTime,
duration: Duration,
) {
timerService.startService(startTime, duration)
}

fun stopTimerService() {
timerService.stopService()
}

fun saveStudyDay(isExtend: Boolean) {
val currentState = _homeUiState.value
if (currentState is HomeUiState.Success) {
Expand Down
Loading

0 comments on commit 9f0af63

Please sign in to comment.