PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/MainModule.kt

206 lines
5.0 KiB
Kotlin
Raw Normal View History

2020-07-20 19:05:48 +00:00
package code.name.monkey.retromusic
import androidx.room.Room
2020-08-23 08:01:42 +00:00
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import code.name.monkey.retromusic.auto.AutoMusicProvider
2020-08-31 12:30:07 +00:00
import code.name.monkey.retromusic.db.BlackListStoreDao
2020-08-23 08:01:42 +00:00
import code.name.monkey.retromusic.db.BlackListStoreEntity
2020-08-20 09:52:38 +00:00
import code.name.monkey.retromusic.db.PlaylistWithSongs
import code.name.monkey.retromusic.db.RetroDatabase
import code.name.monkey.retromusic.fragments.LibraryViewModel
2020-08-11 18:29:44 +00:00
import code.name.monkey.retromusic.fragments.albums.AlbumDetailsViewModel
import code.name.monkey.retromusic.fragments.artists.ArtistDetailsViewModel
import code.name.monkey.retromusic.fragments.genres.GenreDetailsViewModel
import code.name.monkey.retromusic.fragments.playlists.PlaylistDetailsViewModel
2020-07-20 19:05:48 +00:00
import code.name.monkey.retromusic.model.Genre
2020-09-24 12:09:05 +00:00
import code.name.monkey.retromusic.network.provideDefaultCache
import code.name.monkey.retromusic.network.provideLastFmRest
import code.name.monkey.retromusic.network.provideLastFmRetrofit
import code.name.monkey.retromusic.network.provideOkHttp
import code.name.monkey.retromusic.repository.*
2020-08-23 08:01:42 +00:00
import code.name.monkey.retromusic.util.FilePathUtil
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.bind
2020-07-20 19:05:48 +00:00
import org.koin.dsl.module
2020-09-06 17:56:39 +00:00
val networkModule = module {
factory {
provideDefaultCache()
}
factory {
provideOkHttp(get(), get())
}
single {
provideLastFmRetrofit(get())
}
single {
provideLastFmRest(get())
}
}
private val roomModule = module {
2020-08-23 08:01:42 +00:00
single {
Room.databaseBuilder(androidContext(), RetroDatabase::class.java, "playlist.db")
.allowMainThreadQueries()
2020-08-23 08:01:42 +00:00
.addCallback(object : RoomDatabase.Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
super.onOpen(db)
GlobalScope.launch(IO) {
FilePathUtil.blacklistFilePaths().map {
2020-08-31 12:30:07 +00:00
get<BlackListStoreDao>().insertBlacklistPath(BlackListStoreEntity(it))
2020-08-23 08:01:42 +00:00
}
}
}
})
.fallbackToDestructiveMigration()
.build()
}
2020-09-10 19:22:10 +00:00
factory {
get<RetroDatabase>().lyricsDao()
}
factory {
get<RetroDatabase>().playlistDao()
}
2020-08-31 12:30:07 +00:00
factory {
get<RetroDatabase>().blackListStore()
}
factory {
get<RetroDatabase>().playCountDao()
}
2020-09-06 11:03:24 +00:00
2020-09-06 10:47:06 +00:00
factory {
get<RetroDatabase>().historyDao()
}
2020-08-31 12:30:07 +00:00
single {
2020-09-10 19:22:10 +00:00
RealRoomRepository(get(), get(), get(), get(), get())
2020-08-31 12:30:07 +00:00
} bind RoomRepository::class
}
private val autoModule = module {
single {
AutoMusicProvider(androidContext(),
get(),
get(),
get(),
get(),
get(),
get()
)
}
}
private val mainModule = module {
single {
androidContext().contentResolver
}
}
private val dataModule = module {
single {
2020-09-06 17:56:39 +00:00
RealRepository(
get(),
get(),
get(),
get(),
get(),
get(),
get(),
get(),
get(),
get(),
get(),
2020-09-24 10:53:23 +00:00
get(),
2020-09-06 17:56:39 +00:00
)
} bind Repository::class
single {
RealSongRepository(get())
} bind SongRepository::class
single {
RealGenreRepository(get(), get())
} bind GenreRepository::class
single {
RealAlbumRepository(get())
} bind AlbumRepository::class
single {
RealArtistRepository(get(), get())
} bind ArtistRepository::class
single {
RealPlaylistRepository(get())
} bind PlaylistRepository::class
single {
RealTopPlayedRepository(get(), get(), get(), get())
} bind TopPlayedRepository::class
single {
RealLastAddedRepository(
get(),
get(),
get()
)
} bind LastAddedRepository::class
single {
RealSearchRepository(
get(),
get(),
get(),
get(),
get()
)
}
2020-09-24 10:53:23 +00:00
single {
RealLocalDataRepository(get())
} bind LocalDataRepository::class
}
2020-07-20 19:05:48 +00:00
private val viewModules = module {
2020-07-20 19:05:48 +00:00
viewModel {
LibraryViewModel(get())
}
viewModel { (albumId: Long) ->
2020-08-11 18:29:44 +00:00
AlbumDetailsViewModel(
get(),
albumId
)
2020-07-20 19:05:48 +00:00
}
viewModel { (artistId: Long?, artistName: String?) ->
2020-08-11 18:29:44 +00:00
ArtistDetailsViewModel(
get(),
artistId,
artistName
2020-08-11 18:29:44 +00:00
)
2020-07-20 19:05:48 +00:00
}
2020-08-20 09:52:38 +00:00
viewModel { (playlist: PlaylistWithSongs) ->
2020-08-11 18:29:44 +00:00
PlaylistDetailsViewModel(
get(),
playlist
)
2020-07-20 19:05:48 +00:00
}
viewModel { (genre: Genre) ->
2020-08-11 18:29:44 +00:00
GenreDetailsViewModel(
get(),
genre
)
2020-07-20 19:05:48 +00:00
}
}
val appModules = listOf(mainModule, dataModule, autoModule, viewModules, networkModule, roomModule)