Add Koin with api, db and app modules

This commit is contained in:
Shinokuni 2020-10-13 22:11:13 +02:00
parent 9b5dddeff4
commit add729f5e2
7 changed files with 235 additions and 63 deletions

View File

@ -0,0 +1,98 @@
package com.readrops.api
import com.readrops.api.localfeed.LocalRSSDataSource
import com.readrops.api.services.freshrss.FreshRSSDataSource
import com.readrops.api.services.freshrss.FreshRSSService
import com.readrops.api.services.freshrss.adapters.FreshRSSFeedsAdapter
import com.readrops.api.services.freshrss.adapters.FreshRSSFoldersAdapter
import com.readrops.api.services.freshrss.adapters.FreshRSSItemsAdapter
import com.readrops.api.services.nextcloudnews.NextNewsDataSource
import com.readrops.api.services.nextcloudnews.NextNewsService
import com.readrops.api.services.nextcloudnews.adapters.NextNewsFeedsAdapter
import com.readrops.api.services.nextcloudnews.adapters.NextNewsFoldersAdapter
import com.readrops.api.services.nextcloudnews.adapters.NextNewsItemsAdapter
import com.readrops.api.utils.HttpManager
import com.readrops.db.entities.Item
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import okhttp3.OkHttpClient
import org.koin.core.qualifier.named
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.moshi.MoshiConverterFactory
import java.util.concurrent.TimeUnit
val apiModule = module {
single(createdAtStart = true) {
OkHttpClient.Builder()
.callTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.HOURS)
.addInterceptor(HttpManager.getInstance().AuthInterceptor())
.build()
}
single { LocalRSSDataSource(get()) }
//region freshrss
single {
FreshRSSDataSource(get<FreshRSSService>())
}
single {
get<Retrofit>(named("freshrssRetrofit"))
.create(FreshRSSService::class.java)
}
single(named("freshrssRetrofit")) {
get<Retrofit.Builder>()
.addConverterFactory(MoshiConverterFactory.create(get<Moshi>(named("freshrssMoshi"))))
.build()
}
single(named("freshrssMoshi")) {
Moshi.Builder()
.add(Types.newParameterizedType(List::class.java, Item::class.java), FreshRSSItemsAdapter())
.add(FreshRSSFeedsAdapter())
.add(FreshRSSFoldersAdapter())
.build()
}
//endregion freshrss
//region nextcloud news
single {
NextNewsDataSource(get<NextNewsService>())
}
single {
get<Retrofit>(named("nextcloudNewsRetrofit"))
.create(NextNewsService::class.java)
}
single(named("nextcloudNewsRetrofit")) {
get<Retrofit.Builder>()
.addConverterFactory(MoshiConverterFactory.create(get<Moshi>(named("nextcloudNewsMoshi"))))
.build()
}
single(named("nextcloudNewsMoshi")) {
Moshi.Builder()
.add(NextNewsFeedsAdapter())
.add(NextNewsFoldersAdapter())
.add(Types.newParameterizedType(List::class.java, Item::class.java), NextNewsItemsAdapter())
.build()
}
//endregion nextcloud news
single {
Retrofit.Builder() // url will be set dynamically in an interceptor
.baseUrl("https://baseurl.com")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(get<OkHttpClient>())
}
}

View File

@ -0,0 +1,45 @@
package com.readrops.app
import com.readrops.app.repositories.FreshRSSRepository
import com.readrops.app.repositories.LocalFeedRepository
import com.readrops.app.repositories.NextNewsRepository
import com.readrops.app.viewmodels.*
import com.readrops.db.entities.account.Account
import com.readrops.db.entities.account.AccountType
import org.koin.android.ext.koin.androidApplication
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
val appModule = module {
factory { (account: Account) ->
when (account.accountType) {
AccountType.LOCAL -> LocalFeedRepository(get(), get(), androidContext(), account)
AccountType.NEXTCLOUD_NEWS -> NextNewsRepository(get(), get(), androidContext(), account)
AccountType.FRESHRSS -> FreshRSSRepository(get(), get(), androidContext(), account)
else -> throw IllegalArgumentException("Account type not supported")
}
}
viewModel {
MainViewModel(androidApplication())
}
viewModel {
AddFeedsViewModel(androidApplication())
}
viewModel {
ItemViewModel(androidApplication())
}
viewModel {
ManageFeedsFoldersViewModel(androidApplication())
}
viewModel {
NotificationPermissionViewModel(androidApplication())
}
}

View File

@ -1,61 +0,0 @@
package com.readrops.app;
import android.annotation.SuppressLint;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceManager;
import com.readrops.app.utils.SharedPreferencesManager;
import io.reactivex.plugins.RxJavaPlugins;
@SuppressLint("Registered")
public class ReadropsApp extends Application {
public static final String FEEDS_COLORS_CHANNEL_ID = "feedsColorsChannel";
public static final String OPML_EXPORT_CHANNEL_ID = "opmlExportChannel";
public static final String SYNC_CHANNEL_ID = "syncChannel";
@Override
public void onCreate() {
super.onCreate();
RxJavaPlugins.setErrorHandler(e -> {
});
createNotificationChannels();
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
if (Boolean.valueOf(SharedPreferencesManager.readString(this, SharedPreferencesManager.SharedPrefKey.DARK_THEME)))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
private void createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel feedsColorsChannel = new NotificationChannel(FEEDS_COLORS_CHANNEL_ID,
getString(R.string.feeds_colors), NotificationManager.IMPORTANCE_DEFAULT);
feedsColorsChannel.setDescription(getString(R.string.get_feeds_colors));
NotificationChannel opmlExportChannel = new NotificationChannel(OPML_EXPORT_CHANNEL_ID,
getString(R.string.opml_export), NotificationManager.IMPORTANCE_DEFAULT);
opmlExportChannel.setDescription(getString(R.string.opml_export_description));
NotificationChannel syncChannel = new NotificationChannel(SYNC_CHANNEL_ID,
getString(R.string.auto_synchro), NotificationManager.IMPORTANCE_LOW);
syncChannel.setDescription(getString(R.string.account_synchro));
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(feedsColorsChannel);
manager.createNotificationChannel(opmlExportChannel);
manager.createNotificationChannel(syncChannel);
}
}
}

View File

@ -0,0 +1,68 @@
package com.readrops.app
import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager
import com.readrops.api.apiModule
import com.readrops.app.utils.SharedPreferencesManager
import com.readrops.db.dbModule
import io.reactivex.plugins.RxJavaPlugins
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
open class ReadropsApp : Application() {
override fun onCreate() {
super.onCreate()
RxJavaPlugins.setErrorHandler { e: Throwable? -> }
createNotificationChannels()
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
if (SharedPreferencesManager.readString(this, SharedPreferencesManager.SharedPrefKey.DARK_THEME).toBoolean())
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
startKoin {
androidLogger(Level.ERROR)
androidContext(this@ReadropsApp)
modules(apiModule, dbModule, appModule)
}
}
private fun createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val feedsColorsChannel = NotificationChannel(FEEDS_COLORS_CHANNEL_ID,
getString(R.string.feeds_colors), NotificationManager.IMPORTANCE_DEFAULT)
feedsColorsChannel.description = getString(R.string.get_feeds_colors)
val opmlExportChannel = NotificationChannel(OPML_EXPORT_CHANNEL_ID,
getString(R.string.opml_export), NotificationManager.IMPORTANCE_DEFAULT)
opmlExportChannel.description = getString(R.string.opml_export_description)
val syncChannel = NotificationChannel(SYNC_CHANNEL_ID,
getString(R.string.auto_synchro), NotificationManager.IMPORTANCE_LOW)
syncChannel.description = getString(R.string.account_synchro)
val manager = getSystemService(NotificationManager::class.java)!!
manager.createNotificationChannel(feedsColorsChannel)
manager.createNotificationChannel(opmlExportChannel)
manager.createNotificationChannel(syncChannel)
}
}
companion object {
const val FEEDS_COLORS_CHANNEL_ID = "feedsColorsChannel"
const val OPML_EXPORT_CHANNEL_ID = "opmlExportChannel"
const val SYNC_CHANNEL_ID = "syncChannel"
}
}

View File

@ -9,6 +9,7 @@ import androidx.lifecycle.MediatorLiveData;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;
import com.readrops.app.repositories.ARepository;
import com.readrops.db.Database;
import com.readrops.db.ItemsListQueryBuilder;
import com.readrops.db.RoomFactoryWrapper;
@ -18,7 +19,9 @@ import com.readrops.db.entities.account.Account;
import com.readrops.db.filters.FilterType;
import com.readrops.db.filters.ListSortType;
import com.readrops.db.pojo.ItemWithFeed;
import com.readrops.app.repositories.ARepository;
import org.koin.core.parameter.DefinitionParametersKt;
import org.koin.java.KoinJavaComponent;
import java.util.ArrayList;
import java.util.Arrays;
@ -59,7 +62,8 @@ public class MainViewModel extends AndroidViewModel {
private void setRepository() {
try {
repository = ARepository.repositoryFactory(currentAccount, getApplication());
repository = KoinJavaComponent.get(ARepository.class, null,
() -> DefinitionParametersKt.parametersOf(currentAccount));
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -69,4 +69,9 @@ dependencies {
api 'androidx.paging:paging-common:2.1.2'
api 'joda-time:joda-time:2.10.5'
def koin_version = "2.1.6"
api "org.koin:koin-android:$koin_version"
api "org.koin:koin-androidx-scope:$koin_version"
api "org.koin:koin-androidx-viewmodel:$koin_version"
}

View File

@ -0,0 +1,13 @@
package com.readrops.db
import androidx.room.Room
import org.koin.dsl.module
val dbModule = module {
single(createdAtStart = true) {
Room.databaseBuilder(get(), Database::class.java, "readrops-db")
.addMigrations(Database.MIGRATION_1_2)
.build()
}
}