Skip to content

Commit

Permalink
fix(scheduler): sửa lỗi đặt lịch nhắc nhở học tập
Browse files Browse the repository at this point in the history
- lỗi do format local date time
- sửa thành repeat thay vì setExactAndAllowWhileIdle
  • Loading branch information
nqmgaming committed Dec 17, 2024
1 parent 47f719f commit c82ec0f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.pwhs.quickmem.core.data.alarm

import androidx.annotation.StringRes
import java.time.LocalDateTime

data class StudyAlarm(
val time: LocalDateTime,
val message: String
@StringRes val message: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import android.content.Intent
import androidx.core.app.NotificationCompat
import com.pwhs.quickmem.MainActivity
import com.pwhs.quickmem.R
import com.pwhs.quickmem.core.data.alarm.StudyAlarm
import timber.log.Timber
import java.time.LocalDateTime

class AlarmReceiver : BroadcastReceiver() {

Expand All @@ -27,7 +25,7 @@ class AlarmReceiver : BroadcastReceiver() {
}

Timber.d("Alarm received")
val message = intent.getStringExtra("EXTRA_MESSAGE") ?: return
val message = intent.getIntExtra("EXTRA_MESSAGE", R.string.txt_it_s_time_to_study)
Timber.d("Message: $message")

val notificationManager =
Expand All @@ -36,7 +34,7 @@ class AlarmReceiver : BroadcastReceiver() {

val channel = NotificationChannel(
channelId,
"Daily Notifications",
context.getString(R.string.txt_daily_notifications),
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(channel)
Expand All @@ -53,21 +51,13 @@ class AlarmReceiver : BroadcastReceiver() {
)

val notification = NotificationCompat.Builder(context, channelId)
.setContentTitle("Daily Reminder")
.setContentText(message)
.setContentTitle(context.getString(R.string.txt_daily_reminder))
.setContentText(context.getString(message))
.setSmallIcon(R.drawable.ic_bear)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.build()

notificationManager.notify(1, notification)

val alarmScheduler = AndroidAlarmScheduler(context)
val scheduleTime = LocalDateTime.now().plusDays(1)
val studyAlarm = StudyAlarm(
message = "Daily reminder",
time = scheduleTime
)
alarmScheduler.schedule(studyAlarm)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ class AndroidAlarmScheduler @Inject constructor(

val timeInMillis = item.time.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000

alarmManager.setExactAndAllowWhileIdle(
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
timeInMillis,
AlarmManager.INTERVAL_DAY,
pendingIntent
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pwhs.quickmem.presentation.app.settings

import com.pwhs.quickmem.R
import com.pwhs.quickmem.core.data.alarm.StudyAlarm
import com.revenuecat.purchases.CustomerInfo
import java.time.LocalDateTime
Expand All @@ -22,7 +23,7 @@ data class SettingUiState(
val customerInfo: CustomerInfo? = null,
val studyAlarm: StudyAlarm = StudyAlarm(
time = LocalDateTime.now(),
message = "Study time"
message = R.string.txt_it_s_time_to_study
),
val isStudyAlarmEnabled: Boolean = false,
val timeStudyAlarm: String = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pwhs.quickmem.presentation.app.settings
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.pwhs.quickmem.R
import com.pwhs.quickmem.core.data.alarm.StudyAlarm
import com.pwhs.quickmem.core.datastore.AppManager
import com.pwhs.quickmem.core.datastore.TokenManager
Expand Down Expand Up @@ -114,13 +115,14 @@ class SettingsViewModel @Inject constructor(

is SettingUiAction.OnChangeTimeStudyAlarm -> {
viewModelScope.launch {
scheduler.cancel(_uiState.value.studyAlarm)
_uiState.update {
it.copy(
timeStudyAlarm = event.timeStudyAlarm,
studyAlarm = StudyAlarm(
time = event.timeStudyAlarm.toLocalDateTime()
?: LocalDateTime.now(),
message = "It's time to study!"
message = R.string.txt_it_s_time_to_study
)
)
}
Expand Down Expand Up @@ -148,12 +150,12 @@ class SettingsViewModel @Inject constructor(
val username = appManager.username.firstOrNull() ?: ""
val role = appManager.userRole.firstOrNull() ?: ""
val email = appManager.userEmail.firstOrNull() ?: ""
val isPushNotificationsEnabled = appManager.pushNotifications.firstOrNull() ?: false
val isPushNotificationsEnabled = appManager.pushNotifications.firstOrNull() == true
val isAppPushNotificationsEnabled =
appManager.appPushNotifications.firstOrNull() ?: false
val enabledStudySchedule = appManager.enabledStudySchedule.firstOrNull() ?: false
appManager.appPushNotifications.firstOrNull() == true
val enabledStudySchedule = appManager.enabledStudySchedule.firstOrNull() == true
val timeStudySchedule = appManager.timeStudySchedule.firstOrNull() ?: ""
val isPlaySound = appManager.isPlaySound.firstOrNull() ?: false
val isPlaySound = appManager.isPlaySound.firstOrNull() == true
_uiState.update {
it.copy(
userId = userId,
Expand Down Expand Up @@ -205,7 +207,7 @@ class SettingsViewModel @Inject constructor(
is Resources.Success -> {
_uiState.update {
it.copy(
canChangeInfo = resource.data?.success ?: false,
canChangeInfo = resource.data?.success == true,
isLoading = false,
password = "",
errorMessage = ""
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/com/pwhs/quickmem/util/StringExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.pwhs.quickmem.util

import androidx.compose.ui.graphics.Color
import com.wajahatkarim3.easyvalidation.core.view_ktx.validEmail
import timber.log.Timber
import java.text.SimpleDateFormat
import java.time.Duration
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
Expand Down Expand Up @@ -70,15 +72,26 @@ fun String.calculateTimeAgo(): String {
else -> "${duration.toDays()}d"
}
} catch (e: Exception) {
Timber.e(e)
"N/A"
}
}


fun String.toLocalDateTime(): LocalDateTime? {
return try {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
LocalDateTime.parse(this, formatter)
val time = if (this.contains("T")) {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
LocalDateTime.parse(this, formatter)
} else {
val formatter = DateTimeFormatter.ofPattern("HH:mm")
val localTime = LocalTime.parse(this, formatter)
LocalDateTime.now().withHour(localTime.hour).withMinute(localTime.minute).withSecond(0).withNano(0)
}
Timber.d("Time: $time")
time
} catch (e: Exception) {
Timber.e(e)
null
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,7 @@
<string name="txt_user_not_found">Không tìm thấy người dùng</string>
<string name="txt_this_user_is_already_joined">Người dùng này đã tham gia lớp học</string>
<string name="folder_edited">Chỉnh sửa thư mục xong</string>
<string name="txt_daily_reminder">Thông báo nhắc nhở học hàng ngày</string>
<string name="txt_it_s_time_to_study">Đã đến thời gian học rồi!</string>
<string name="txt_daily_notifications">Thông báo hằng ngày</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,7 @@
<string name="txt_user_not_found">User not found</string>
<string name="txt_this_user_is_already_joined">This user is already joined</string>
<string name="folder_edited">Folder edited</string>
<string name="txt_daily_reminder">Daily Reminder</string>
<string name="txt_daily_notifications">Daily Notifications</string>
<string name="txt_it_s_time_to_study">It\'s time to study!</string>
</resources>

0 comments on commit c82ec0f

Please sign in to comment.