forked from github/wulkanowy-mirror
Add debug notification for worker (#271)
This commit is contained in:
parent
ca23f92096
commit
f21feabc49
@ -50,7 +50,7 @@ internal class RepositoryModule {
|
||||
@Provides
|
||||
fun provideChuckCollector(context: Context, prefRepository: PreferencesRepository): ChuckCollector {
|
||||
return ChuckCollector(context)
|
||||
.showNotification(prefRepository.isShowChuckerNotification)
|
||||
.showNotification(prefRepository.isDebugNotificationEnable)
|
||||
.retentionManager(RetentionManager(context, ChuckCollector.Period.ONE_HOUR))
|
||||
}
|
||||
|
||||
|
@ -48,7 +48,7 @@ class PreferencesRepository @Inject constructor(
|
||||
val isNotificationsEnable: Boolean
|
||||
get() = sharedPref.getBoolean(context.getString(R.string.pref_key_notifications_enable), true)
|
||||
|
||||
val isShowChuckerNotificationKey: String = context.getString(R.string.pref_key_debug_chucker_notification)
|
||||
val isShowChuckerNotification: Boolean
|
||||
get() = sharedPref.getBoolean(isShowChuckerNotificationKey, false)
|
||||
val isDebugNotificationEnableKey: String = context.getString(R.string.pref_key_notification_debug)
|
||||
val isDebugNotificationEnable: Boolean
|
||||
get() = sharedPref.getBoolean(isDebugNotificationEnableKey, false)
|
||||
}
|
||||
|
@ -11,23 +11,28 @@ import androidx.work.NetworkType.UNMETERED
|
||||
import androidx.work.PeriodicWorkRequest
|
||||
import androidx.work.WorkManager
|
||||
import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository
|
||||
import io.github.wulkanowy.services.sync.channels.DebugChannel
|
||||
import io.github.wulkanowy.services.sync.channels.NewEntriesChannel
|
||||
import io.github.wulkanowy.utils.isHolidays
|
||||
import org.threeten.bp.LocalDate.now
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.TimeUnit.MINUTES
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class SyncManager @Inject constructor(
|
||||
private val workManager: WorkManager,
|
||||
private val preferencesRepository: PreferencesRepository,
|
||||
newEntriesChannel: NewEntriesChannel
|
||||
newEntriesChannel: NewEntriesChannel,
|
||||
debugChannel: DebugChannel,
|
||||
@Named("isDebug") isDebug: Boolean
|
||||
) {
|
||||
|
||||
init {
|
||||
if (SDK_INT >= O) newEntriesChannel.create()
|
||||
if (SDK_INT >= O && isDebug) debugChannel.create()
|
||||
if (now().isHolidays) stopSyncWorker()
|
||||
Timber.i("SyncManager was initialized")
|
||||
}
|
||||
@ -35,7 +40,7 @@ class SyncManager @Inject constructor(
|
||||
fun startSyncWorker(restart: Boolean = false) {
|
||||
if (preferencesRepository.isServiceEnabled && !now().isHolidays) {
|
||||
workManager.enqueueUniquePeriodicWork(SyncWorker::class.java.simpleName, if (restart) REPLACE else KEEP,
|
||||
PeriodicWorkRequest.Builder(SyncWorker::class.java, preferencesRepository.servicesInterval, MINUTES, 10, MINUTES)
|
||||
PeriodicWorkRequest.Builder(SyncWorker::class.java, preferencesRepository.servicesInterval, MINUTES)
|
||||
.setBackoffCriteria(EXPONENTIAL, 30, MINUTES)
|
||||
.setConstraints(Constraints.Builder()
|
||||
.setRequiredNetworkType(if (preferencesRepository.isServicesOnlyWifi) METERED else UNMETERED)
|
||||
|
@ -1,24 +1,35 @@
|
||||
package io.github.wulkanowy.services.sync
|
||||
|
||||
import android.content.Context
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationCompat.BigTextStyle
|
||||
import androidx.core.app.NotificationCompat.PRIORITY_DEFAULT
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.work.ListenableWorker
|
||||
import androidx.work.RxWorker
|
||||
import androidx.work.WorkerParameters
|
||||
import com.squareup.inject.assisted.Assisted
|
||||
import com.squareup.inject.assisted.AssistedInject
|
||||
import io.github.wulkanowy.R
|
||||
import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository
|
||||
import io.github.wulkanowy.data.repositories.semester.SemesterRepository
|
||||
import io.github.wulkanowy.data.repositories.student.StudentRepository
|
||||
import io.github.wulkanowy.services.sync.channels.DebugChannel
|
||||
import io.github.wulkanowy.services.sync.works.Work
|
||||
import io.github.wulkanowy.utils.getCompatColor
|
||||
import io.reactivex.Completable
|
||||
import io.reactivex.Single
|
||||
import timber.log.Timber
|
||||
import kotlin.random.Random
|
||||
|
||||
class SyncWorker @AssistedInject constructor(
|
||||
@Assisted appContext: Context,
|
||||
@Assisted workerParameters: WorkerParameters,
|
||||
private val studentRepository: StudentRepository,
|
||||
private val semesterRepository: SemesterRepository,
|
||||
private val works: Set<@JvmSuppressWildcards Work>
|
||||
private val works: Set<@JvmSuppressWildcards Work>,
|
||||
private val preferencesRepository: PreferencesRepository,
|
||||
private val notificationManager: NotificationManagerCompat
|
||||
) : RxWorker(appContext, workerParameters) {
|
||||
|
||||
override fun createWork(): Single<Result> {
|
||||
@ -34,6 +45,18 @@ class SyncWorker @AssistedInject constructor(
|
||||
Timber.e(it, "There was an error during synchronization")
|
||||
Result.retry()
|
||||
}
|
||||
.doOnSuccess { if (preferencesRepository.isDebugNotificationEnable) notify(it) }
|
||||
}
|
||||
|
||||
private fun notify(result: Result) {
|
||||
notificationManager.notify(Random.nextInt(Int.MAX_VALUE), NotificationCompat.Builder(applicationContext, DebugChannel.CHANNEL_ID)
|
||||
.setContentTitle("Debug notification")
|
||||
.setSmallIcon(R.drawable.ic_more_settings_24dp)
|
||||
.setAutoCancel(true)
|
||||
.setColor(applicationContext.getCompatColor(R.color.colorPrimary))
|
||||
.setStyle(BigTextStyle().bigText("${SyncWorker::class.java.simpleName} result: $result"))
|
||||
.setPriority(PRIORITY_DEFAULT)
|
||||
.build())
|
||||
}
|
||||
|
||||
@AssistedInject.Factory
|
||||
|
@ -0,0 +1,28 @@
|
||||
package io.github.wulkanowy.services.sync.channels
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.app.Notification.VISIBILITY_PUBLIC
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.NotificationManager.IMPORTANCE_DEFAULT
|
||||
import android.content.Context
|
||||
import io.github.wulkanowy.R
|
||||
import javax.inject.Inject
|
||||
|
||||
@TargetApi(26)
|
||||
class DebugChannel @Inject constructor(
|
||||
private val notificationManager: NotificationManager,
|
||||
private val context: Context
|
||||
) {
|
||||
|
||||
companion object {
|
||||
const val CHANNEL_ID = "debug_channel"
|
||||
}
|
||||
|
||||
fun create() {
|
||||
notificationManager.createNotificationChannel(
|
||||
NotificationChannel(CHANNEL_ID, context.getString(R.string.channel_debug), IMPORTANCE_DEFAULT).apply {
|
||||
lockscreenVisibility = VISIBILITY_PUBLIC
|
||||
})
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import com.takisoft.preferencex.PreferenceFragmentCompat
|
||||
import dagger.android.support.AndroidSupportInjection
|
||||
import io.github.wulkanowy.BuildConfig
|
||||
import io.github.wulkanowy.BuildConfig.DEBUG
|
||||
import io.github.wulkanowy.R
|
||||
import io.github.wulkanowy.ui.base.BaseActivity
|
||||
import io.github.wulkanowy.ui.modules.main.MainView
|
||||
@ -37,7 +37,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
|
||||
|
||||
override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
addPreferencesFromResource(R.xml.scheme_preferences)
|
||||
findPreference(getString(R.string.pref_key_debug_chucker_notification)).isVisible = BuildConfig.DEBUG
|
||||
findPreference(getString(R.string.pref_key_notification_debug)).isVisible = DEBUG
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
|
||||
|
@ -32,7 +32,7 @@ class SettingsPresenter @Inject constructor(
|
||||
serviceEnableKey -> syncManager.run { if (isServiceEnabled) startSyncWorker() else stopSyncWorker() }
|
||||
servicesIntervalKey, servicesOnlyWifiKey -> syncManager.startSyncWorker(true)
|
||||
currentThemeKey -> view?.setTheme(currentTheme)
|
||||
isShowChuckerNotificationKey -> chuckCollector.showNotification(isShowChuckerNotification)
|
||||
isDebugNotificationEnableKey -> chuckCollector.showNotification(isDebugNotificationEnable)
|
||||
}
|
||||
}
|
||||
analytics.logEvent("setting_changed", "name" to key)
|
||||
|
@ -249,7 +249,7 @@
|
||||
|
||||
<string name="pref_notify_header">Powiadomienia</string>
|
||||
<string name="pref_notify_switch">Pokazuj powiadomienia</string>
|
||||
<string name="pref_debug_notify_switch">Pokazuj powiadomienia debugowania</string>
|
||||
<string name="pref_notify_debug_switch">Pokazuj powiadomienia debugowania</string>
|
||||
|
||||
<string name="pref_services_header">Synchronizacja</string>
|
||||
<string name="pref_services_switch">Automatyczna aktualizacja</string>
|
||||
@ -260,6 +260,7 @@
|
||||
|
||||
<!--Notification Channels-->
|
||||
<string name="channel_new_entries">Nowe wpisy w dzienniku</string>
|
||||
<string name="channel_debug">Debugowanie</string>
|
||||
|
||||
|
||||
<!--Colors-->
|
||||
|
@ -11,5 +11,5 @@
|
||||
<string name="pref_key_services_interval">services_interval</string>
|
||||
<string name="pref_key_services_wifi_only">services_disable_wifi_only</string>
|
||||
<string name="pref_key_notifications_enable">notifications_enable</string>
|
||||
<string name="pref_key_debug_chucker_notification">chucker_notification</string>
|
||||
<string name="pref_key_notification_debug">notification_debug</string>
|
||||
</resources>
|
||||
|
@ -234,7 +234,7 @@
|
||||
|
||||
<string name="pref_notify_header">Notifications</string>
|
||||
<string name="pref_notify_switch">Show notifications</string>
|
||||
<string name="pref_debug_notify_switch">Show debug notifications</string>
|
||||
<string name="pref_notify_debug_switch">Show debug notifications</string>
|
||||
|
||||
<string name="pref_services_header">Synchronization</string>
|
||||
<string name="pref_services_switch">Automatic update</string>
|
||||
@ -245,6 +245,7 @@
|
||||
|
||||
<!--Notification Channels-->
|
||||
<string name="channel_new_entries">New entries in register</string>
|
||||
<string name="channel_debug">Debug</string>
|
||||
|
||||
|
||||
<!--Colors-->
|
||||
|
@ -90,8 +90,8 @@
|
||||
app:iconSpaceReserved="false" />
|
||||
<SwitchPreference
|
||||
android:defaultValue="false"
|
||||
android:key="@string/pref_key_debug_chucker_notification"
|
||||
android:title="@string/pref_debug_notify_switch"
|
||||
android:key="@string/pref_key_notification_debug"
|
||||
android:title="@string/pref_notify_debug_switch"
|
||||
app:iconSpaceReserved="false" />
|
||||
</PreferenceCategory>
|
||||
</PreferenceScreen>
|
||||
|
Loading…
x
Reference in New Issue
Block a user