Migrate Koin to 1.0.0-beta3 version.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2018-08-03 21:50:21 +02:00
parent ea9289b5db
commit 48acc64ee3
10 changed files with 65 additions and 49 deletions

View File

@ -20,7 +20,7 @@ ext.versions = [
okhttp : "3.10.0",
semver : "1.0.0",
twitterSerial : "0.1.6",
koin : "0.9.3",
koin : "1.0.0-beta-3",
picasso : "2.71828",
junit : "4.12",
@ -57,6 +57,7 @@ ext.other = [
semver : "net.swiftzer.semver:semver:$versions.semver",
twitterSerial : "com.twitter.serial:serial:$versions.twitterSerial",
koinCore : "org.koin:koin-core:$versions.koin",
koinJava : "org.koin:koin-java:$versions.koin",
koinAndroid : "org.koin:koin-android:$versions.koin",
picasso : "com.squareup.picasso:picasso:$versions.picasso",
]
@ -73,4 +74,5 @@ ext.testing = [
apacheCodecs : "commons-codec:commons-codec:$versions.apacheCodecs",
testRunner : "com.android.support.test:runner:$versions.testRunner",
robolectric : "org.robolectric:robolectric:$versions.robolectric",
koinTest : "org.koin:koin-test:$versions.koin"
]

View File

@ -1,10 +1,10 @@
package org.moire.ultrasonic.api.subsonic.di
import org.koin.dsl.context.Context
import org.koin.dsl.context.ModuleDefinition
import org.moire.ultrasonic.api.subsonic.SubsonicAPIClient
const val SUBSONIC_API_CLIENT_CONTEXT = "SubsonicApiClientContext"
fun Context.subsonicApiModule() = context(SUBSONIC_API_CLIENT_CONTEXT) {
bean { return@bean SubsonicAPIClient(get(), get()) }
fun ModuleDefinition.subsonicApiModule() = module(SUBSONIC_API_CLIENT_CONTEXT) {
single { SubsonicAPIClient(get(), get()) }
}

View File

@ -3,25 +3,31 @@ package org.moire.ultrasonic.app
import android.app.Application
import org.koin.android.ext.android.get
import org.koin.android.ext.android.startKoin
import org.moire.ultrasonic.di.DiProperties
import org.moire.ultrasonic.di.appPermanentStorage
import org.moire.ultrasonic.di.baseNetworkModule
import org.moire.ultrasonic.di.directoriesModule
import org.moire.ultrasonic.di.featureFlagsModule
import org.moire.ultrasonic.di.musicServiceModule
import org.moire.ultrasonic.featureflags.FeatureStorage
import org.moire.ultrasonic.subsonic.loader.image.SubsonicImageLoader
import org.moire.ultrasonic.util.Util
class UApp : Application() {
override fun onCreate() {
super.onCreate()
val sharedPreferences = Util.getPreferences(this)
startKoin(this, listOf(
directoriesModule,
baseNetworkModule,
featureFlagsModule(this),
musicServiceModule(sharedPreferences, this)
))
startKoin(this,
listOf(
directoriesModule,
appPermanentStorage,
baseNetworkModule,
featureFlagsModule,
musicServiceModule
),
extraProperties = mapOf(
DiProperties.APP_CONTEXT to applicationContext
)
)
}
/**

View File

@ -0,0 +1,10 @@
package org.moire.ultrasonic.di
import org.koin.dsl.module.module
import org.moire.ultrasonic.util.Util
const val SP_NAME = "Default_SP"
val appPermanentStorage = module {
single(name = SP_NAME) { Util.getPreferences(getProperty(DiProperties.APP_CONTEXT)) }
}

View File

@ -1,11 +1,11 @@
package org.moire.ultrasonic.di
import okhttp3.OkHttpClient
import org.koin.dsl.module.applicationContext
import org.koin.dsl.module.module
/**
* Provides base network dependencies.
*/
val baseNetworkModule = applicationContext {
bean { OkHttpClient.Builder().build() }
val baseNetworkModule = module {
single { OkHttpClient.Builder().build() }
}

View File

@ -0,0 +1,5 @@
package org.moire.ultrasonic.di
object DiProperties {
const val APP_CONTEXT = "app_context"
}

View File

@ -1,9 +1,9 @@
package org.moire.ultrasonic.di
import org.koin.dsl.module.applicationContext
import org.koin.dsl.module.module
import org.moire.ultrasonic.cache.AndroidDirectories
import org.moire.ultrasonic.cache.Directories
val directoriesModule = applicationContext {
bean { AndroidDirectories(get()) } bind Directories::class
val directoriesModule = module {
single { AndroidDirectories(get()) } bind Directories::class
}

View File

@ -1,11 +1,8 @@
package org.moire.ultrasonic.di
import android.content.Context
import org.koin.dsl.module.applicationContext
import org.koin.dsl.module.module
import org.moire.ultrasonic.featureflags.FeatureStorage
fun featureFlagsModule(
context: Context
) = applicationContext {
factory { FeatureStorage(context) }
val featureFlagsModule = module {
factory { FeatureStorage(getProperty(DiProperties.APP_CONTEXT)) }
}

View File

@ -1,10 +1,9 @@
@file:JvmName("MusicServiceModule")
package org.moire.ultrasonic.di
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import org.koin.dsl.module.applicationContext
import org.koin.dsl.module.module
import org.moire.ultrasonic.BuildConfig
import org.moire.ultrasonic.api.subsonic.SubsonicAPIClient
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions
@ -26,40 +25,38 @@ private const val DEFAULT_SERVER_INSTANCE = 1
private const val UNKNOWN_SERVER_URL = "not-exists"
private const val LOG_TAG = "MusicServiceModule"
fun musicServiceModule(
sp: SharedPreferences,
context: Context
) = applicationContext {
context(MUSIC_SERVICE_CONTEXT) {
val musicServiceModule = module(MUSIC_SERVICE_CONTEXT) {
subsonicApiModule()
bean(name = "ServerInstance") {
return@bean sp.getInt(
single(name = "ServerInstance") {
return@single get<SharedPreferences>(SP_NAME).getInt(
Constants.PREFERENCES_KEY_SERVER_INSTANCE,
DEFAULT_SERVER_INSTANCE
)
}
bean(name = "ServerID") {
single(name = "ServerID") {
val serverInstance = get<Int>(name = "ServerInstance")
val sp: SharedPreferences = get(SP_NAME)
val serverUrl = sp.getString(
Constants.PREFERENCES_KEY_SERVER_URL + serverInstance,
null
)
return@bean if (serverUrl == null) {
return@single if (serverUrl == null) {
UNKNOWN_SERVER_URL
} else {
abs("$serverUrl$serverInstance".hashCode()).toString()
}
}
bean {
single {
val serverId = get<String>(name = "ServerID")
return@bean PermanentFileStorage(get(), serverId, BuildConfig.DEBUG)
return@single PermanentFileStorage(get(), serverId, BuildConfig.DEBUG)
}
bean {
single {
val instance = get<Int>(name = "ServerInstance")
val sp: SharedPreferences = get(SP_NAME)
val serverUrl = sp.getString(Constants.PREFERENCES_KEY_SERVER_URL + instance, null)
val username = sp.getString(Constants.PREFERENCES_KEY_USERNAME + instance, null)
val password = sp.getString(Constants.PREFERENCES_KEY_PASSWORD + instance, null)
@ -77,7 +74,7 @@ fun musicServiceModule(
password == null
) {
Log.i(LOG_TAG, "Server credentials is not available")
return@bean SubsonicClientConfiguration(
return@single SubsonicClientConfiguration(
baseUrl = "http://localhost",
username = "",
password = "",
@ -90,7 +87,7 @@ fun musicServiceModule(
debug = BuildConfig.DEBUG
)
} else {
return@bean SubsonicClientConfiguration(
return@single SubsonicClientConfiguration(
baseUrl = serverUrl,
username = username,
password = password,
@ -105,16 +102,15 @@ fun musicServiceModule(
}
}
bean { return@bean SubsonicAPIClient(get()) }
single { SubsonicAPIClient(get()) }
bean<MusicService>(name = ONLINE_MUSIC_SERVICE) {
return@bean CachedMusicService(RESTMusicService(get(), get()))
single<MusicService>(name = ONLINE_MUSIC_SERVICE) {
CachedMusicService(RESTMusicService(get(), get()))
}
bean<MusicService>(name = OFFLINE_MUSIC_SERVICE) {
return@bean OfflineMusicService(get(), get())
single<MusicService>(name = OFFLINE_MUSIC_SERVICE) {
OfflineMusicService(get(), get())
}
bean { return@bean SubsonicImageLoader(context, get()) }
single { SubsonicImageLoader(getProperty(DiProperties.APP_CONTEXT), get()) }
}
}

View File

@ -21,7 +21,7 @@ package org.moire.ultrasonic.service
import android.content.Context
import org.koin.standalone.KoinComponent
import org.koin.standalone.get
import org.koin.standalone.releaseContext
import org.koin.standalone.release
import org.moire.ultrasonic.cache.Directories
import org.moire.ultrasonic.di.MUSIC_SERVICE_CONTEXT
import org.moire.ultrasonic.di.OFFLINE_MUSIC_SERVICE
@ -45,7 +45,7 @@ object MusicServiceFactory : KoinComponent {
*/
@JvmStatic
fun resetMusicService() {
releaseContext(MUSIC_SERVICE_CONTEXT)
release(MUSIC_SERVICE_CONTEXT)
}
@JvmStatic