Start instant delivery notification
This commit is contained in:
parent
7e9da28704
commit
7dbbf12c99
4 changed files with 20 additions and 9 deletions
|
@ -36,6 +36,12 @@ class Repository(private val sharedPrefs: SharedPreferences, private val subscri
|
|||
return toSubscriptionList(subscriptionDao.list())
|
||||
}
|
||||
|
||||
fun getSubscriptionIdsWithInstantStatus(): Set<Pair<Long, Boolean>> {
|
||||
return subscriptionDao
|
||||
.list()
|
||||
.map { Pair(it.id, it.instant) }.toSet()
|
||||
}
|
||||
|
||||
@Suppress("RedundantSuspendModifier")
|
||||
@WorkerThread
|
||||
suspend fun getSubscription(subscriptionId: Long): Subscription? {
|
||||
|
|
|
@ -103,7 +103,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc
|
|||
}
|
||||
}
|
||||
|
||||
// React to changes in fast delivery setting
|
||||
// React to changes in instant delivery setting
|
||||
viewModel.listIdsWithInstantStatus().observe(this) {
|
||||
subscriberManager?.refreshService(it)
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package io.heckel.ntfy.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
|
@ -7,16 +8,17 @@ import androidx.activity.ComponentActivity
|
|||
import androidx.lifecycle.lifecycleScope
|
||||
import io.heckel.ntfy.msg.SubscriberService
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* This class only manages the SubscriberService, i.e. it starts or stops it.
|
||||
* It's used in multiple activities.
|
||||
*/
|
||||
class SubscriberManager(private val activity: ComponentActivity) {
|
||||
fun refreshService(subscriptionIdsWithInstantStatus: Set<Pair<Long, Boolean>>) {
|
||||
class SubscriberManager(private val context: Context) {
|
||||
fun refreshService(subscriptionIdsWithInstantStatus: Set<Pair<Long, Boolean>>) { // Set<SubscriptionId -> IsInstant>
|
||||
Log.d(MainActivity.TAG, "Triggering subscriber service refresh")
|
||||
activity.lifecycleScope.launch(Dispatchers.IO) {
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
val instantSubscriptions = subscriptionIdsWithInstantStatus.toList().filter { (_, instant) -> instant }.size
|
||||
if (instantSubscriptions == 0) {
|
||||
performActionOnSubscriberService(SubscriberService.Actions.STOP)
|
||||
|
@ -27,18 +29,18 @@ class SubscriberManager(private val activity: ComponentActivity) {
|
|||
}
|
||||
|
||||
private fun performActionOnSubscriberService(action: SubscriberService.Actions) {
|
||||
val serviceState = SubscriberService.readServiceState(activity)
|
||||
val serviceState = SubscriberService.readServiceState(context)
|
||||
if (serviceState == SubscriberService.ServiceState.STOPPED && action == SubscriberService.Actions.STOP) {
|
||||
return
|
||||
}
|
||||
val intent = Intent(activity, SubscriberService::class.java)
|
||||
val intent = Intent(context, SubscriberService::class.java)
|
||||
intent.action = action.name
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
Log.d(MainActivity.TAG, "Performing SubscriberService action: ${action.name} (as foreground service, API >= 26)")
|
||||
activity.startForegroundService(intent)
|
||||
context.startForegroundService(intent)
|
||||
} else {
|
||||
Log.d(MainActivity.TAG, "Performing SubscriberService action: ${action.name} (as background service, API >= 26)")
|
||||
activity.startService(intent)
|
||||
context.startService(intent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import android.util.Log
|
|||
import io.heckel.ntfy.R
|
||||
import io.heckel.ntfy.app.Application
|
||||
import io.heckel.ntfy.data.Subscription
|
||||
import io.heckel.ntfy.ui.SubscriberManager
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -43,8 +44,10 @@ class BroadcastReceiver : android.content.BroadcastReceiver() {
|
|||
)
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
repository.addSubscription(subscription)
|
||||
val subscriptionIdsWithInstantStatus = repository.getSubscriptionIdsWithInstantStatus()
|
||||
val subscriberManager = SubscriberManager(context!!)
|
||||
subscriberManager.refreshService(subscriptionIdsWithInstantStatus)
|
||||
}
|
||||
|
||||
distributor.sendEndpoint(appId, connectorToken)
|
||||
// XXXXXXXXX
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue