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