Remove last references to Database singleton

This commit is contained in:
Shinokuni 2020-10-23 18:51:01 +02:00
parent 6294b5556f
commit e32b20bc87
5 changed files with 29 additions and 33 deletions

View File

@ -19,6 +19,8 @@ import com.readrops.app.utils.SyncWorker;
import com.readrops.app.utils.feedscolors.FeedsColorsIntentService;
import com.readrops.db.Database;
import org.koin.java.KoinJavaComponent;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@ -38,7 +40,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
AtomicBoolean serviceStarted = new AtomicBoolean(false);
feedsColorsPreference.setOnPreferenceClickListener(preference -> {
Database database = Database.getInstance(getContext());
Database database = KoinJavaComponent.get(Database.class);
database.feedDao().getAllFeeds().observe(getActivity(), feeds -> {
if (!serviceStarted.get()) {

View File

@ -1,20 +1,22 @@
package com.readrops.app.utils
import android.content.Context
import com.readrops.api.services.SyncResult
import com.readrops.db.Database
import com.readrops.db.entities.Item
import com.readrops.db.entities.account.Account
import com.readrops.db.entities.account.AccountType
import com.readrops.api.services.SyncResult
import org.jetbrains.annotations.TestOnly
import org.koin.core.KoinComponent
import org.koin.core.get
class SyncResultDebugData {
companion object {
companion object : KoinComponent {
@TestOnly
fun oneAccountOneFeedOneItem(context: Context): Map<Account, SyncResult> {
val database = Database.getInstance(context)
fun oneAccountOneFeedOneItem(): Map<Account, SyncResult> {
val database = get<Database>()
val account1 = database.accountDao().select(2)
@ -27,14 +29,14 @@ class SyncResultDebugData {
}
@TestOnly
fun oneAccountOneFeedMultipleItems(context: Context): Map<Account, SyncResult> {
fun oneAccountOneFeedMultipleItems(): Map<Account, SyncResult> {
val account1 = Account().apply {
id = 1
accountType = AccountType.FRESHRSS
isNotificationsEnabled = true
}
val database = Database.getInstance(context)
val database = get<Database>()
val item = database.itemDao().select(5055)
database.feedDao().updateFeedNotificationState(item.feedId, false).subscribe()

View File

@ -7,6 +7,7 @@ import android.content.Intent
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.readrops.api.services.SyncResult
@ -19,13 +20,16 @@ import com.readrops.db.entities.Item
import com.readrops.db.entities.account.Account
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.koin.core.KoinComponent
import org.koin.core.get
import org.koin.core.parameter.parametersOf
class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters) {
class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters), KoinComponent {
private var disposable: Disposable? = null
private val notificationManager = NotificationManagerCompat.from(applicationContext)
private val database = Database.getInstance(applicationContext)
private val database = get<Database>()
override fun doWork(): Result {
var result = Result.success()
@ -39,7 +43,7 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
.setProgress(0, 0, true)
.setSmallIcon(R.drawable.ic_notif)
.setOnlyAlertOnce(true)
accounts.forEach {
notificationBuilder.setContentText(it.accountName)
notificationManager.notify(SYNC_NOTIFICATION_ID, notificationBuilder.build())
@ -47,7 +51,7 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
it.login = SharedPreferencesManager.readString(applicationContext, it.loginKey)
it.password = SharedPreferencesManager.readString(applicationContext, it.passwordKey)
val repository = ARepository.repositoryFactory(it, applicationContext)
val repository = get<ARepository>(parameters = { parametersOf(it) })
disposable = repository.sync(null)
.doOnError { throwable ->
@ -138,12 +142,12 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
.build()
}
class MarkReadReceiver : BroadcastReceiver() {
class MarkReadReceiver : BroadcastReceiver(), KoinComponent {
override fun onReceive(context: Context?, intent: Intent?) {
val itemId = intent?.getIntExtra(ReadropsKeys.ITEM_ID, 0)!!
with(Database.getInstance(context)) {
with(get<Database>()) {
itemDao().setReadState(itemId, true, true)
.subscribeOn(Schedulers.io())
.subscribe()
@ -155,12 +159,12 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
}
}
class ReadLaterReceiver : BroadcastReceiver() {
class ReadLaterReceiver : BroadcastReceiver(), KoinComponent {
override fun onReceive(context: Context?, intent: Intent?) {
val itemId = intent?.getIntExtra(ReadropsKeys.ITEM_ID, 0)!!
with(Database.getInstance(context)) {
with(get<Database>()) {
itemDao().setReadItLater(itemId)
.subscribeOn(Schedulers.io())
.subscribe()

View File

@ -6,15 +6,17 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.readrops.app.R
import com.readrops.app.ReadropsApp
import com.readrops.app.utils.ReadropsKeys.FEEDS
import com.readrops.db.Database
import com.readrops.db.entities.Feed
import com.readrops.app.utils.ReadropsKeys.FEEDS
import org.koin.core.KoinComponent
import org.koin.core.get
class FeedsColorsIntentService : IntentService("FeedsColorsIntentService") {
class FeedsColorsIntentService : IntentService("FeedsColorsIntentService"), KoinComponent {
override fun onHandleIntent(intent: Intent?) {
val feeds: List<Feed> = intent!!.getParcelableArrayListExtra(FEEDS)!!
val database = Database.getInstance(this)
val database = get<Database>()
val notificationBuilder = NotificationCompat.Builder(this, ReadropsApp.FEEDS_COLORS_CHANNEL_ID)
.setContentTitle(getString(R.string.get_feeds_colors))

View File

@ -1,9 +1,6 @@
package com.readrops.db;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
import androidx.room.migration.Migration;
@ -30,18 +27,7 @@ public abstract class Database extends RoomDatabase {
public abstract FolderDao folderDao();
public abstract AccountDao accountDao();
private static Database database;
public static Database getInstance(Context context) {
if (database == null)
database = Room.databaseBuilder(context, Database.class, "readrops-db")
.addMigrations(MIGRATION_1_2)
.build();
return database;
}
public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {