mirror of
https://github.com/afollestad/nock-nock.git
synced 2025-08-05 07:38:38 +00:00
Show success notification if validation passes after previously not passing. Resolves #4.
This commit is contained in:
parent
e3820fd7d3
commit
cbac2796aa
6 changed files with 66 additions and 13 deletions
|
@ -19,8 +19,8 @@ import android.app.job.JobParameters
|
||||||
import android.app.job.JobService
|
import android.app.job.JobService
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.afollestad.nocknock.data.AppDatabase
|
import com.afollestad.nocknock.data.AppDatabase
|
||||||
import com.afollestad.nocknock.data.model.RetryPolicy
|
|
||||||
import com.afollestad.nocknock.data.getSite
|
import com.afollestad.nocknock.data.getSite
|
||||||
|
import com.afollestad.nocknock.data.model.RetryPolicy
|
||||||
import com.afollestad.nocknock.data.model.Site
|
import com.afollestad.nocknock.data.model.Site
|
||||||
import com.afollestad.nocknock.data.model.Status
|
import com.afollestad.nocknock.data.model.Status
|
||||||
import com.afollestad.nocknock.data.model.Status.CHECKING
|
import com.afollestad.nocknock.data.model.Status.CHECKING
|
||||||
|
@ -80,6 +80,10 @@ class ValidationJob : JobService() {
|
||||||
sendBroadcast(Intent(ACTION_JOB_RUNNING).apply { putExtra(KEY_SITE_ID, site.id) })
|
sendBroadcast(Intent(ACTION_JOB_RUNNING).apply { putExtra(KEY_SITE_ID, site.id) })
|
||||||
|
|
||||||
log("Checking ${site.name} (${site.url})...")
|
log("Checking ${site.name} (${site.url})...")
|
||||||
|
val lastResult = site.lastResult
|
||||||
|
if (lastResult != null) {
|
||||||
|
log("Result of previous attempt: ${lastResult.status}")
|
||||||
|
}
|
||||||
|
|
||||||
val jobResult = async(IO) {
|
val jobResult = async(IO) {
|
||||||
updateStatus(site, CHECKING)
|
updateStatus(site, CHECKING)
|
||||||
|
@ -139,6 +143,9 @@ class ValidationJob : JobService() {
|
||||||
|
|
||||||
if (jobResult.lastResult!!.status == OK) {
|
if (jobResult.lastResult!!.status == OK) {
|
||||||
notificationManager.cancelStatusNotification(jobResult)
|
notificationManager.cancelStatusNotification(jobResult)
|
||||||
|
if (lastResult != null && lastResult.status != OK) {
|
||||||
|
notificationManager.postValidationSuccessNotification(jobResult)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
val retryPolicy = site.retryPolicy
|
val retryPolicy = site.retryPolicy
|
||||||
if (retryPolicy != null) {
|
if (retryPolicy != null) {
|
||||||
|
@ -167,7 +174,7 @@ class ValidationJob : JobService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notificationManager.postStatusNotification(jobResult)
|
notificationManager.postValidationErrorNotification(jobResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
validationManager.scheduleValidation(
|
validationManager.scheduleValidation(
|
||||||
|
|
|
@ -24,7 +24,13 @@ enum class Channel(
|
||||||
val description: Int,
|
val description: Int,
|
||||||
val importance: Int
|
val importance: Int
|
||||||
) {
|
) {
|
||||||
CheckFailures(
|
ValidationSuccess(
|
||||||
|
id = "check_success",
|
||||||
|
title = R.string.channel_server_check_success_title,
|
||||||
|
description = R.string.channel_server_check_success_description,
|
||||||
|
importance = IMPORTANCE_DEFAULT
|
||||||
|
),
|
||||||
|
ValidationErrors(
|
||||||
id = "check_failures",
|
id = "check_failures",
|
||||||
title = R.string.channel_server_check_failures_title,
|
title = R.string.channel_server_check_failures_title,
|
||||||
description = R.string.channel_server_check_failures_description,
|
description = R.string.channel_server_check_failures_description,
|
||||||
|
|
|
@ -18,7 +18,7 @@ package com.afollestad.nocknock.notifications
|
||||||
import android.annotation.TargetApi
|
import android.annotation.TargetApi
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
import android.os.Build.VERSION_CODES
|
import android.os.Build.VERSION_CODES
|
||||||
import com.afollestad.nocknock.notifications.Channel.CheckFailures
|
import com.afollestad.nocknock.notifications.Channel.ValidationErrors
|
||||||
import com.afollestad.nocknock.utilities.providers.CanNotifyModel
|
import com.afollestad.nocknock.utilities.providers.CanNotifyModel
|
||||||
import com.afollestad.nocknock.utilities.providers.IntentProvider
|
import com.afollestad.nocknock.utilities.providers.IntentProvider
|
||||||
import com.afollestad.nocknock.utilities.providers.NotificationChannelProvider
|
import com.afollestad.nocknock.utilities.providers.NotificationChannelProvider
|
||||||
|
@ -34,7 +34,9 @@ interface NockNotificationManager {
|
||||||
|
|
||||||
fun createChannels()
|
fun createChannels()
|
||||||
|
|
||||||
fun postStatusNotification(model: CanNotifyModel)
|
fun postValidationErrorNotification(model: CanNotifyModel)
|
||||||
|
|
||||||
|
fun postValidationSuccessNotification(model: CanNotifyModel)
|
||||||
|
|
||||||
fun cancelStatusNotification(model: CanNotifyModel)
|
fun cancelStatusNotification(model: CanNotifyModel)
|
||||||
|
|
||||||
|
@ -60,26 +62,48 @@ class RealNockNotificationManager(
|
||||||
override fun createChannels() =
|
override fun createChannels() =
|
||||||
Channel.values().forEach(this::createChannel)
|
Channel.values().forEach(this::createChannel)
|
||||||
|
|
||||||
override fun postStatusNotification(model: CanNotifyModel) {
|
override fun postValidationErrorNotification(model: CanNotifyModel) {
|
||||||
if (isAppOpen) {
|
if (isAppOpen) {
|
||||||
// Don't show notifications while the app is open
|
// Don't show notifications while the app is open
|
||||||
log("App is open, status notification for site ${model.notifyId()} won't be posted.")
|
log("App is open, validation error notification for site ${model.notifyId()} won't be posted.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log("Posting status notification for site ${model.notifyId()}...")
|
log("Posting validation error notification for site ${model.notifyId()}...")
|
||||||
val intent = intentProvider.getPendingIntentForViewSite(model)
|
val intent = intentProvider.getPendingIntentForViewSite(model)
|
||||||
|
|
||||||
val newNotification = notificationProvider.create(
|
val newNotification = notificationProvider.create(
|
||||||
channelId = CheckFailures.id,
|
channelId = ValidationErrors.id,
|
||||||
title = model.notifyName(),
|
title = model.notifyName(),
|
||||||
content = model.notifyDescription() ?: stringProvider.get(R.string.something_wrong),
|
content = model.notifyDescription() ?: stringProvider.get(R.string.something_wrong),
|
||||||
intent = intent,
|
intent = intent,
|
||||||
smallIcon = R.drawable.ic_notification
|
smallIcon = R.drawable.ic_notification_error
|
||||||
)
|
)
|
||||||
|
|
||||||
stockManager.notify(model.notifyTag(), model.notificationId(), newNotification)
|
stockManager.notify(model.notifyTag(), model.notificationId(), newNotification)
|
||||||
log("Posted status notification for site ${model.notificationId()}.")
|
log("Posted validation error notification for site ${model.notificationId()}.")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun postValidationSuccessNotification(model: CanNotifyModel) {
|
||||||
|
if (isAppOpen) {
|
||||||
|
// Don't show notifications while the app is open
|
||||||
|
log("App is open, validation success notification for site ${model.notifyId()} won't be posted.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log("Posting validation success notification for site ${model.notifyId()}...")
|
||||||
|
val intent = intentProvider.getPendingIntentForViewSite(model)
|
||||||
|
|
||||||
|
val newNotification = notificationProvider.create(
|
||||||
|
channelId = ValidationErrors.id,
|
||||||
|
title = model.notifyName(),
|
||||||
|
content = stringProvider.get(R.string.validation_passed),
|
||||||
|
intent = intent,
|
||||||
|
smallIcon = R.drawable.ic_notification_success
|
||||||
|
)
|
||||||
|
|
||||||
|
stockManager.notify(model.notifyTag(), model.notificationId(), newNotification)
|
||||||
|
log("Posted validation success notification for site ${model.notificationId()}.")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cancelStatusNotification(model: CanNotifyModel) {
|
override fun cancelStatusNotification(model: CanNotifyModel) {
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
<vector
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportHeight="24.0"
|
||||||
|
android:viewportWidth="24.0"
|
||||||
|
android:width="24dp">
|
||||||
|
<path
|
||||||
|
android:fillColor="#FFFFFF"
|
||||||
|
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z"/>
|
||||||
|
</vector>
|
|
@ -1,12 +1,18 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
<string name="channel_server_check_failures_title">Server Check Failures</string>
|
<string name="channel_server_check_failures_title">Site Validation Failures</string>
|
||||||
<string name="channel_server_check_failures_description">
|
<string name="channel_server_check_failures_description">
|
||||||
Notifications for Nock Nock status checks failing for your sites. Something has gone
|
Notifications for Nock Nock validations failing for your sites. Something has gone
|
||||||
wrong if you see one of these.
|
wrong if you see one of these.
|
||||||
</string>
|
</string>
|
||||||
|
|
||||||
|
<string name="channel_server_check_success_title">Site Validation Success</string>
|
||||||
|
<string name="channel_server_check_success_description">
|
||||||
|
Notifications for Nock Nock when a site validation passes when it previously had not.
|
||||||
|
</string>
|
||||||
|
|
||||||
<string name="something_wrong">Something\'s wrong! Tap for details.</string>
|
<string name="something_wrong">Something\'s wrong! Tap for details.</string>
|
||||||
|
<string name="validation_passed">Yay! No longer in trouble! Validation passed.</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue