Skip to content

Commit

Permalink
Merge pull request #45 from omer358/dev
Browse files Browse the repository at this point in the history
fix #44
  • Loading branch information
omer358 authored Aug 14, 2024
2 parents 4442a6f + 11c8909 commit 718287b
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions app/src/main/java/com/example/rememberme/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,26 @@ import java.util.concurrent.TimeUnit
class MainActivity : ComponentActivity() {

private val settingsViewModel: SettingsViewModel by viewModels()

private var lastRepetition: RemindersRepetition? = null

private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
lifecycleScope.launch {
settingsViewModel.uiState.collect { uiState ->
if (isGranted) {
if (isGranted && uiState.remindersRepetition != lastRepetition) {
// Permission is granted. Schedule the notification work with the correct repetition.
Log.i(TAG, "Permission granted")
scheduleNotificationWork(uiState.remindersRepetition)
lastRepetition = uiState.remindersRepetition
} else {
Log.i(TAG, "Permission denied")
// Permission is denied. Handle the case.
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Expand All @@ -56,24 +58,28 @@ class MainActivity : ComponentActivity() {

lifecycleScope.launch {
settingsViewModel.uiState.collect { uiState ->
Log.i(TAG, "Settings UI state: $uiState")
val remindersRepetition = uiState.remindersRepetition

// Check for notification permission only if the SDK version is 33 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(
this@MainActivity,
POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
) {
// Permission is already granted
scheduleNotificationWork(remindersRepetition)
if (remindersRepetition != lastRepetition) {
// Check for notification permission only if the SDK version is 33 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(
this@MainActivity,
POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
) {
// Permission is already granted
scheduleNotificationWork(remindersRepetition)
} else {
// Request permission
requestPermissionLauncher.launch(POST_NOTIFICATIONS)
}
} else {
// Request permission
requestPermissionLauncher.launch(POST_NOTIFICATIONS)
// SDK version is lower than 33, no need to request POST_NOTIFICATIONS permission
scheduleNotificationWork(remindersRepetition)
}
} else {
// SDK version is lower than 33, no need to request POST_NOTIFICATIONS permission
scheduleNotificationWork(remindersRepetition)
lastRepetition = remindersRepetition
}
}
}
Expand All @@ -83,7 +89,7 @@ class MainActivity : ComponentActivity() {
Log.i(TAG, "Scheduling notification work with repetition: $repetition")
val repeatInterval = when (repetition) {
RemindersRepetition.OnceADay -> 24
RemindersRepetition.ThreeADay -> 24 /3
RemindersRepetition.ThreeADay -> 24 / 3
RemindersRepetition.FiveADay -> 24 / 5
}

Expand All @@ -92,17 +98,17 @@ class MainActivity : ComponentActivity() {
repeatInterval.toLong(),
TimeUnit.HOURS)
.addTag("notificationWorkRequest")
.setInitialDelay(repeatInterval.toLong(), TimeUnit.HOURS)
.build()

WorkManager.getInstance(this).enqueueUniquePeriodicWork(
"notificationWork",
ExistingPeriodicWorkPolicy.KEEP, // Keeps the existing work if it exists
ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
notificationWorkRequest
)
}

companion object {
private const val TAG = "MainActivity"
}

}
}

0 comments on commit 718287b

Please sign in to comment.