Skip to content

Commit

Permalink
fix: sync messages require notification permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ViscousPot committed Sep 15, 2024
1 parent e675914 commit edad3b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
32 changes: 21 additions & 11 deletions app/src/main/java/com/viscouspot/gitsync/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class MainActivity : AppCompatActivity() {

window.statusBarColor = getColor(R.color.app_bg)

checkAndRequestStoragePermission { }
checkAndRequestStoragePermission()

settingsManager = SettingsManager(this)
gitManager = GitManager(this, this)
Expand Down Expand Up @@ -238,11 +238,16 @@ class MainActivity : AppCompatActivity() {
}

syncMessageButton.setOnClickListener {
settingsManager.toggleSyncMessageEnabled()
if (settingsManager.getSyncMessageEnabled()) {
syncMessageButton.setIconResource(R.drawable.notify)
syncMessageButton.setIconTintResource(R.color.auth_green)
val syncMessageEnabled = settingsManager.getSyncMessageEnabled()

if (!syncMessageEnabled) {
checkAndRequestNotificationPermission {
settingsManager.setSyncMessageEnabled(true)
syncMessageButton.setIconResource(R.drawable.notify)
syncMessageButton.setIconTintResource(R.color.auth_green)
}
} else {
settingsManager.setSyncMessageEnabled(false)
syncMessageButton.setIconResource(R.drawable.notify_off)
syncMessageButton.setIconTintResource(R.color.textPrimary)
}
Expand Down Expand Up @@ -377,8 +382,12 @@ class MainActivity : AppCompatActivity() {
gitDirPath.setText(settingsManager.getGitDirPath())

if (settingsManager.getSyncMessageEnabled()) {
syncMessageButton.setIconResource(R.drawable.notify)
syncMessageButton.setIconTintResource(R.color.auth_green)
settingsManager.setSyncMessageEnabled(false)
checkAndRequestNotificationPermission {
settingsManager.setSyncMessageEnabled(true)
syncMessageButton.setIconResource(R.drawable.notify)
syncMessageButton.setIconTintResource(R.color.auth_green)
}
} else {
syncMessageButton.setIconResource(R.drawable.notify_off)
syncMessageButton.setIconTintResource(R.color.textPrimary)
Expand Down Expand Up @@ -568,11 +577,12 @@ class MainActivity : AppCompatActivity() {
}
}

private fun checkNotificationPermission(): Boolean {
return NotificationManagerCompat.from(this).areNotificationsEnabled()
}
private fun checkAndRequestNotificationPermission(onGranted: (() -> Unit)? = null) {
if (NotificationManagerCompat.from(this).areNotificationsEnabled()) {
onGranted?.invoke()
return
}

private fun requestNotificationPermission() {
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ class SettingsManager internal constructor(context: Context) {
return settingsSharedPref.getBoolean("syncMessageEnabled", true)
}

fun toggleSyncMessageEnabled() {
val prevValue = getSyncMessageEnabled()
fun setSyncMessageEnabled(enabled: Boolean) {
with(settingsSharedPref.edit()) {
putBoolean("syncMessageEnabled", !prevValue)
putBoolean("syncMessageEnabled", enabled)
apply()
}
}
Expand Down

0 comments on commit edad3b4

Please sign in to comment.