Merge pull request #1908 from Hafizzle/Fiz/reminder-notification-permission

Request notification permission after user creates reminder
This commit is contained in:
Phillip Thelen 2023-01-27 13:05:28 +01:00 committed by GitHub
commit 63216f6f5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View file

@ -8,6 +8,7 @@ import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.provider.Settings
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
@ -67,8 +68,7 @@ import com.habitrpg.shared.habitica.models.tasks.HabitResetOption
import com.habitrpg.shared.habitica.models.tasks.TaskDifficulty
import com.habitrpg.shared.habitica.models.tasks.TaskType
import io.realm.RealmList
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import java.util.Date
import javax.inject.Inject
@ -107,6 +107,29 @@ class TaskFormActivity : BaseActivity() {
private var challenge: Challenge? = null
private val notificationPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { granted ->
if (granted) {
pushNotificationManager.addPushDeviceUsingStoredToken()
} else {
//If user denies notification settings originally - they must manually enable it through notification settings.
val alert = HabiticaAlertDialog(this)
alert.setTitle(R.string.push_notification_system_settings_title)
alert.setMessage(R.string.push_notification_system_settings_description)
alert.addButton(R.string.settings, true, false) { _, _ ->
val notifSettingIntent: Intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Settings.EXTRA_APP_PACKAGE, applicationContext?.packageName)
startActivity(notifSettingIntent)
}
alert.addButton(R.string.cancel, false) { _, _ ->
alert.dismiss()
}
alert.show()
}
}
private var isCreating = true
private var isChallengeTask = false
private var usesTaskAttributeStats = false
@ -753,7 +776,14 @@ class TaskFormActivity : BaseActivity() {
private fun checkIfShowNotifLayout() {
if (!pushNotificationManager.notificationPermissionEnabled() && Build.VERSION.SDK_INT >= 33) {
binding.notificationsDisabledLayout.visibility = View.VISIBLE
binding.remindersContainer.shouldShowNotifPermission = true
binding.remindersContainer.showNotifPermission = { show ->
if (show) {
notificationPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
}
}
} else {
binding.remindersContainer.shouldShowNotifPermission = false
binding.notificationsDisabledLayout.visibility = View.GONE
}
}

View file

@ -62,6 +62,9 @@ class ReminderContainer @JvmOverloads constructor(
field = value
}
var showNotifPermission: ((Boolean) -> Unit)? = null
var shouldShowNotifPermission = false
init {
orientation = VERTICAL
@ -81,6 +84,7 @@ class ReminderContainer @JvmOverloads constructor(
addReminderViewAt(-1)
view.animDuration = 300
view.isAddButton = false
if (shouldShowNotifPermission) { showNotifPermission?.invoke(true) }
}
}
val indexToUse = if (index < 0) {