Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Koin 2.0.1 #105

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/src/main/java/com/naman14/timberx/MainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import com.naman14.timberx.playback.MediaSessionConnection
import com.naman14.timberx.playback.RealMediaSessionConnection
import com.naman14.timberx.playback.TimberMusicService
import io.reactivex.android.schedulers.AndroidSchedulers
import org.koin.dsl.module.module
import org.koin.core.qualifier.named
import org.koin.dsl.bind
import org.koin.dsl.module

const val MAIN = "main"

Expand All @@ -36,7 +38,7 @@ val mainModule = module {
RealMediaSessionConnection(get(), component)
} bind MediaSessionConnection::class

factory(name = MAIN) {
factory(named(MAIN)) {
AndroidSchedulers.mainThread()
}
}
15 changes: 8 additions & 7 deletions app/src/main/java/com/naman14/timberx/PrefsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import com.naman14.timberx.constants.SongSortOrder
import com.naman14.timberx.constants.SongSortOrder.SONG_A_Z
import com.naman14.timberx.constants.StartPage
import com.naman14.timberx.constants.StartPage.SONGS
import org.koin.dsl.module.module
import org.koin.core.qualifier.named
import org.koin.dsl.module

const val PREF_APP_THEME = "theme_preference"
const val PREF_SONG_SORT_ORDER = "song_sort_order"
Expand All @@ -38,27 +39,27 @@ const val PREF_LAST_FOLDER = "last_folder"
val prefsModule = module {
single { rxkPrefs(get<Application>()) }

factory(name = PREF_SONG_SORT_ORDER) {
factory(named(PREF_SONG_SORT_ORDER)) {
get<RxkPrefs>().enum(PREF_SONG_SORT_ORDER, SONG_A_Z,
SongSortOrder.Companion::fromString, SongSortOrder.Companion::toString)
}

factory(name = PREF_ALBUM_SORT_ORDER) {
factory(named(PREF_ALBUM_SORT_ORDER)) {
get<RxkPrefs>().enum(PREF_ALBUM_SORT_ORDER, ALBUM_A_Z,
AlbumSortOrder.Companion::fromString, AlbumSortOrder.Companion::toString)
}

factory(name = PREF_APP_THEME) {
factory(named(PREF_APP_THEME)) {
get<RxkPrefs>().enum(PREF_APP_THEME, LIGHT,
AppThemes.Companion::fromString, AppThemes.Companion::toString)
}

factory(name = PREF_START_PAGE) {
factory(named(PREF_START_PAGE)) {
get<RxkPrefs>().enum(PREF_START_PAGE, SONGS,
StartPage.Companion::fromString, StartPage.Companion::toString)
}

factory(name = PREF_LAST_FOLDER) {
factory(named(PREF_LAST_FOLDER)) {
val defaultFolder = getExternalStoragePublicDirectory(DIRECTORY_MUSIC).path
get<RxkPrefs>().string(PREF_LAST_FOLDER, defaultFolder)
}
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/com/naman14/timberx/TimberXApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import com.naman14.timberx.permissions.permissionsModule
import com.naman14.timberx.playback.mediaModule
import com.naman14.timberx.repository.repositoriesModule
import com.naman14.timberx.ui.viewmodels.viewModelsModule
import org.koin.android.ext.android.startKoin
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import timber.log.Timber

class TimberXApp : Application() {
Expand All @@ -55,9 +57,10 @@ class TimberXApp : Application() {
lyricsModule,
lastFmModule
)
startKoin(
androidContext = this,
modules = modules
)
startKoin {
androidLogger()
androidContext(this@TimberXApp)
modules(modules)
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/naman14/timberx/db/RoomModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ package com.naman14.timberx.db

import android.app.Application
import androidx.room.Room
import org.koin.dsl.module.module
import org.koin.dsl.bind
import org.koin.dsl.module

val roomModule = module {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.naman14.timberx.network
import com.google.gson.Gson
import com.naman14.timberx.network.api.LastFmRestService
import okhttp3.OkHttpClient
import org.koin.dsl.module.module
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.naman14.timberx.network
import com.naman14.timberx.network.api.LyricsRestService
import com.naman14.timberx.network.conversion.LyricsConverterFactory
import okhttp3.OkHttpClient
import org.koin.dsl.module.module
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES
import com.google.gson.GsonBuilder
import okhttp3.Cache
import okhttp3.OkHttpClient
import org.koin.dsl.module.module
import org.koin.dsl.module

private const val CACHE_MAX_AGE = 60 * 60 * 24 * 7
private const val CACHE_MAX_STALE = 31536000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import android.app.Application
import android.app.NotificationManager
import android.content.Context
import com.naman14.timberx.extensions.systemService
import org.koin.dsl.module.module
import org.koin.dsl.bind
import org.koin.dsl.module

val notificationModule = module {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import com.naman14.timberx.playback.players.RealMusicPlayer
import com.naman14.timberx.playback.players.RealQueue
import com.naman14.timberx.playback.players.RealSongPlayer
import com.naman14.timberx.playback.players.SongPlayer
import org.koin.dsl.module.module
import org.koin.dsl.bind
import org.koin.dsl.module

val mediaModule = module {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.android.ext.android.get
import org.koin.android.ext.android.inject
import org.koin.standalone.KoinComponent
import org.koin.core.KoinComponent
import timber.log.Timber.d as log

// TODO pull out media logic to separate class to make this more readable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ package com.naman14.timberx.repository

import com.naman14.timberx.PREF_ALBUM_SORT_ORDER
import com.naman14.timberx.PREF_SONG_SORT_ORDER
import org.koin.dsl.module.module
import org.koin.core.qualifier.named
import org.koin.dsl.bind
import org.koin.dsl.module

val repositoriesModule = module {

factory {
RealSongsRepository(get(), get(name = PREF_SONG_SORT_ORDER))
RealSongsRepository(get(), get(named(PREF_SONG_SORT_ORDER)))
} bind SongsRepository::class

factory {
RealAlbumRepository(get(), get(name = PREF_ALBUM_SORT_ORDER))
RealAlbumRepository(get(), get(named(PREF_ALBUM_SORT_ORDER)))
} bind AlbumRepository::class

factory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.naman14.timberx.network.models.ArtworkSize
import com.naman14.timberx.network.models.ArtworkSize.MEGA
import com.naman14.timberx.network.models.ofSize
import com.naman14.timberx.util.Utils.getAlbumArtUri
import org.koin.standalone.StandAloneContext
import org.koin.core.context.GlobalContext
import timber.log.Timber

// Matches keys in preferences.xml
Expand Down Expand Up @@ -157,8 +157,7 @@ private fun fetchArtistImage(
artworkSize: ArtworkSize,
callback: (url: String) -> Unit
) {
val lastFmService = StandAloneContext.getKoin()
.koinContext.get<LastFmRestService>()
val lastFmService = GlobalContext.get().koin.get<LastFmRestService>()
lastFmService.getArtistInfo(artistName)
.ioToMain()
.subscribeForOutcome { outcome ->
Expand All @@ -184,8 +183,7 @@ private fun fetchAlbumImage(
artworkSize: ArtworkSize,
callback: (url: String) -> Unit
) {
val lastFmService = StandAloneContext.getKoin()
.koinContext.get<LastFmRestService>()
val lastFmService = GlobalContext.get().koin.get<LastFmRestService>()
lastFmService.getAlbumInfo(artistName, albumName)
.ioToMain()
.subscribeForOutcome { outcome ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ import com.naman14.timberx.ui.adapters.FolderAdapter
import com.naman14.timberx.ui.fragments.base.MediaItemFragment
import kotlinx.android.synthetic.main.layout_recyclerview_padding.recyclerView
import org.koin.android.ext.android.inject
import org.koin.core.qualifier.named

class FolderFragment : MediaItemFragment() {
private lateinit var folderAdapter: FolderAdapter

private val songsRepository by inject<SongsRepository>()
private val foldersRepository by inject<FoldersRepository>()
private val lastFolderPref by inject<Pref<String>>(name = PREF_LAST_FOLDER)
private val lastFolderPref by inject<Pref<String>>(named(PREF_LAST_FOLDER))

override fun onCreateView(
inflater: LayoutInflater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ import kotlinx.android.synthetic.main.toolbar.btnSearch
import kotlinx.android.synthetic.main.toolbar.mediaRouteButton
import kotlinx.android.synthetic.main.toolbar.toolbar
import org.koin.android.ext.android.inject
import org.koin.core.qualifier.named

class MainFragment : Fragment() {
private val startPagePref by inject<Pref<StartPage>>(name = PREF_START_PAGE)
private val startPagePref by inject<Pref<StartPage>>(named(PREF_START_PAGE))

override fun onCreateView(
inflater: LayoutInflater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ import com.naman14.timberx.ui.listeners.SortMenuListener
import com.naman14.timberx.util.SpacesItemDecoration
import kotlinx.android.synthetic.main.layout_recyclerview_padding.recyclerView
import org.koin.android.ext.android.inject
import org.koin.core.qualifier.named

class AlbumsFragment : MediaItemFragment() {
private lateinit var albumAdapter: AlbumAdapter
private val sortOrderPref by inject<Pref<AlbumSortOrder>>(name = PREF_ALBUM_SORT_ORDER)
private val sortOrderPref by inject<Pref<AlbumSortOrder>>(named(PREF_ALBUM_SORT_ORDER))

override fun onCreateView(
inflater: LayoutInflater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ import com.naman14.timberx.ui.fragments.base.MediaItemFragment
import com.naman14.timberx.ui.listeners.SortMenuListener
import kotlinx.android.synthetic.main.layout_recyclerview.recyclerView
import org.koin.android.ext.android.inject
import org.koin.core.qualifier.named

class SongsFragment : MediaItemFragment() {
private lateinit var songsAdapter: SongsAdapter
private val sortOrderPref by inject<Pref<SongSortOrder>>(name = PREF_SONG_SORT_ORDER)
private val sortOrderPref by inject<Pref<SongSortOrder>>(named(PREF_SONG_SORT_ORDER))

override fun onCreateView(
inflater: LayoutInflater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package com.naman14.timberx.ui.viewmodels

import com.naman14.timberx.models.MediaID
import org.koin.androidx.viewmodel.ext.koin.viewModel
import org.koin.dsl.module.module
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module

val viewModelsModule = module {
viewModel {
Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ext.versions = [
retrofit : "2.4.0",
retrofitGson : "2.3.0",
nanoHttpd : "2.3.1",
koin : "1.0.2",
koin : "2.0.1",

// Debug
timber : "4.7.1",
Expand Down