mirror of https://github.com/tuskyapp/Tusky.git
Browse Source
- Extend what was `NotificationWorkerFactory` to `WorkerFactory`. This can construct arbitrary Workers as long as they provide their own Factory for construction. The per-Worker factory contains any injected components just for that worker type, keeping `WorkerFactory` clean. - Move `NotificationWorkerFactory` to the new model. - Implement `PruneCacheWorker`, and remove the code from `CachedTimelineViewModel`. - Create the periodic worker in `TuskyApplication`, ensuring that the database is only pruned when the device is idle.pull/3652/head^2
11 changed files with 227 additions and 71 deletions
@ -1,51 +0,0 @@
|
||||
/* Copyright 2020 Tusky Contributors |
||||
* |
||||
* This file is part of Tusky. |
||||
* |
||||
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU |
||||
* Lesser General Public License as published by the Free Software Foundation, either version 3 of |
||||
* the License, or (at your option) any later version. |
||||
* |
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
||||
* General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public License along with Tusky. If |
||||
* not, see <http://www.gnu.org/licenses/>. */ |
||||
|
||||
package com.keylesspalace.tusky.components.notifications |
||||
|
||||
import android.content.Context |
||||
import androidx.work.ListenableWorker |
||||
import androidx.work.Worker |
||||
import androidx.work.WorkerFactory |
||||
import androidx.work.WorkerParameters |
||||
import javax.inject.Inject |
||||
|
||||
class NotificationWorker( |
||||
context: Context, |
||||
params: WorkerParameters, |
||||
private val notificationsFetcher: NotificationFetcher |
||||
) : Worker(context, params) { |
||||
|
||||
override fun doWork(): Result { |
||||
notificationsFetcher.fetchAndShow() |
||||
return Result.success() |
||||
} |
||||
} |
||||
|
||||
class NotificationWorkerFactory @Inject constructor( |
||||
private val notificationsFetcher: NotificationFetcher |
||||
) : WorkerFactory() { |
||||
|
||||
override fun createWorker( |
||||
appContext: Context, |
||||
workerClassName: String, |
||||
workerParameters: WorkerParameters |
||||
): ListenableWorker? { |
||||
if (workerClassName == NotificationWorker::class.java.name) { |
||||
return NotificationWorker(appContext, workerParameters, notificationsFetcher) |
||||
} |
||||
return null |
||||
} |
||||
} |
||||
@ -0,0 +1,45 @@
|
||||
/* |
||||
* Copyright 2023 Tusky Contributors |
||||
* |
||||
* This file is a part of Tusky. |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the |
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||
* Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not, |
||||
* see <http://www.gnu.org/licenses>. |
||||
*/ |
||||
|
||||
package com.keylesspalace.tusky.di |
||||
|
||||
import androidx.work.ListenableWorker |
||||
import com.keylesspalace.tusky.worker.ChildWorkerFactory |
||||
import com.keylesspalace.tusky.worker.NotificationWorker |
||||
import com.keylesspalace.tusky.worker.PruneCacheWorker |
||||
import dagger.Binds |
||||
import dagger.MapKey |
||||
import dagger.Module |
||||
import dagger.multibindings.IntoMap |
||||
import kotlin.reflect.KClass |
||||
|
||||
@Retention(AnnotationRetention.RUNTIME) |
||||
@MapKey |
||||
annotation class WorkerKey(val value: KClass<out ListenableWorker>) |
||||
|
||||
@Module |
||||
abstract class WorkerModule { |
||||
@Binds |
||||
@IntoMap |
||||
@WorkerKey(NotificationWorker::class) |
||||
internal abstract fun bindNotificationWorkerFactory(worker: NotificationWorker.Factory): ChildWorkerFactory |
||||
|
||||
@Binds |
||||
@IntoMap |
||||
@WorkerKey(PruneCacheWorker::class) |
||||
internal abstract fun bindPruneCacheWorkerFactory(worker: PruneCacheWorker.Factory): ChildWorkerFactory |
||||
} |
||||
@ -0,0 +1,44 @@
|
||||
/* |
||||
* Copyright 2023 Tusky Contributors |
||||
* |
||||
* This file is a part of Tusky. |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the |
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||
* Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not, |
||||
* see <http://www.gnu.org/licenses>. |
||||
*/ |
||||
|
||||
package com.keylesspalace.tusky.worker |
||||
|
||||
import android.content.Context |
||||
import androidx.work.Worker |
||||
import androidx.work.WorkerParameters |
||||
import com.keylesspalace.tusky.components.notifications.NotificationFetcher |
||||
import javax.inject.Inject |
||||
|
||||
/** Fetch and show new notifications. */ |
||||
class NotificationWorker( |
||||
appContext: Context, |
||||
params: WorkerParameters, |
||||
private val notificationsFetcher: NotificationFetcher |
||||
) : Worker(appContext, params) { |
||||
override fun doWork(): Result { |
||||
notificationsFetcher.fetchAndShow() |
||||
return Result.success() |
||||
} |
||||
|
||||
class Factory @Inject constructor( |
||||
private val notificationsFetcher: NotificationFetcher |
||||
) : ChildWorkerFactory { |
||||
override fun createWorker(appContext: Context, params: WorkerParameters): Worker { |
||||
return NotificationWorker(appContext, params, notificationsFetcher) |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,58 @@
|
||||
/* |
||||
* Copyright 2023 Tusky Contributors |
||||
* |
||||
* This file is a part of Tusky. |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the |
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||
* Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not, |
||||
* see <http://www.gnu.org/licenses>. |
||||
*/ |
||||
|
||||
package com.keylesspalace.tusky.worker |
||||
|
||||
import android.content.Context |
||||
import android.util.Log |
||||
import androidx.work.CoroutineWorker |
||||
import androidx.work.ListenableWorker |
||||
import androidx.work.WorkerParameters |
||||
import com.keylesspalace.tusky.db.AccountManager |
||||
import com.keylesspalace.tusky.db.AppDatabase |
||||
import javax.inject.Inject |
||||
|
||||
/** Prune the database cache of old statuses. */ |
||||
class PruneCacheWorker( |
||||
appContext: Context, |
||||
workerParams: WorkerParameters, |
||||
private val appDatabase: AppDatabase, |
||||
private val accountManager: AccountManager |
||||
) : CoroutineWorker(appContext, workerParams) { |
||||
override suspend fun doWork(): Result { |
||||
for (account in accountManager.accounts) { |
||||
Log.d(TAG, "Pruning database using account ID: ${account.id}") |
||||
appDatabase.timelineDao().cleanup(account.id, MAX_STATUSES_IN_CACHE) |
||||
} |
||||
return Result.success() |
||||
} |
||||
|
||||
companion object { |
||||
private const val TAG = "PruneCacheWorker" |
||||
private const val MAX_STATUSES_IN_CACHE = 1000 |
||||
const val PERIODIC_WORK_TAG = "PruneCacheWorker_periodic" |
||||
} |
||||
|
||||
class Factory @Inject constructor( |
||||
private val appDatabase: AppDatabase, |
||||
private val accountManager: AccountManager |
||||
) : ChildWorkerFactory { |
||||
override fun createWorker(appContext: Context, params: WorkerParameters): ListenableWorker { |
||||
return PruneCacheWorker(appContext, params, appDatabase, accountManager) |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,56 @@
|
||||
/* |
||||
* Copyright 2023 Tusky Contributors |
||||
* |
||||
* This file is a part of Tusky. |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the |
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||
* Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not, |
||||
* see <http://www.gnu.org/licenses>. |
||||
*/ |
||||
|
||||
package com.keylesspalace.tusky.worker |
||||
|
||||
import android.content.Context |
||||
import androidx.work.ListenableWorker |
||||
import androidx.work.WorkerFactory |
||||
import androidx.work.WorkerParameters |
||||
import javax.inject.Inject |
||||
import javax.inject.Provider |
||||
|
||||
/** |
||||
* Workers implement this and are added to the map in [com.keylesspalace.tusky.di.WorkerModule] |
||||
* so they can be created by [WorkerFactory.createWorker]. |
||||
*/ |
||||
interface ChildWorkerFactory { |
||||
/** Create a new instance of the given worker. */ |
||||
fun createWorker(appContext: Context, params: WorkerParameters): ListenableWorker |
||||
} |
||||
|
||||
/** |
||||
* Creates workers, delegating to each worker's [ChildWorkerFactory.createWorker] to do the |
||||
* creation. |
||||
* |
||||
* @see [com.keylesspalace.tusky.components.notifications.NotificationWorker] |
||||
*/ |
||||
class WorkerFactory @Inject constructor( |
||||
val workerFactories: Map<Class<out ListenableWorker>, @JvmSuppressWildcards Provider<ChildWorkerFactory>> |
||||
) : WorkerFactory() { |
||||
override fun createWorker( |
||||
appContext: Context, |
||||
workerClassName: String, |
||||
workerParameters: WorkerParameters |
||||
): ListenableWorker? { |
||||
val key = Class.forName(workerClassName) |
||||
workerFactories[key]?.let { |
||||
return it.get().createWorker(appContext, workerParameters) |
||||
} |
||||
return null |
||||
} |
||||
} |
||||
Loading…
Reference in new issue