-
Notifications
You must be signed in to change notification settings - Fork 7
26-27 lesson homework #39
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.github.krottv.tmstemp | ||
|
|
||
| import android.app.Application | ||
| import com.github.krottv.tmstemp.data.ITunesRemoteDataSourceRetrofit | ||
| import com.github.krottv.tmstemp.data.LibraryRemoteDataSourceRetrofit | ||
| import com.github.krottv.tmstemp.data.MusicApi | ||
| import com.github.krottv.tmstemp.presentation.AlbumsViewModel | ||
| import com.github.krottv.tmstemp.presentation.TracksViewModel | ||
| import org.koin.android.ext.koin.androidContext | ||
| import org.koin.androidx.viewmodel.dsl.viewModel | ||
| import org.koin.core.context.startKoin | ||
| import org.koin.core.module.Module | ||
| import org.koin.core.module.dsl.factoryOf | ||
| import org.koin.dsl.module | ||
|
|
||
| class MyApp : Application() { | ||
|
|
||
| private val itunesModule: Module | ||
| get() = module { | ||
| factory<MusicApi> { ITunesRemoteDataSourceRetrofit() } | ||
| } | ||
| private val libraryModule: Module | ||
| get() = module { | ||
| factory<MusicApi> { LibraryRemoteDataSourceRetrofit() } | ||
| } | ||
| private val viewModelModule: Module | ||
| get() = module { | ||
| viewModel { AlbumsViewModel(get()) } | ||
| viewModel { TracksViewModel(get()) } | ||
| } | ||
|
|
||
| override fun onCreate() { | ||
| super.onCreate() | ||
| startKoin { | ||
| androidContext(this@MyApp) | ||
| modules(libraryModule, itunesModule, viewModelModule) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.github.krottv.tmstemp.data | ||
|
|
||
| import com.github.krottv.tmstemp.domain.AlbumModel | ||
|
|
||
| class DataSourceFake { | ||
|
|
||
| fun getITunesAlbums(): List<AlbumModel>{ | ||
| val model = AlbumModel(0, | ||
| "https://inspiry-2ee60.web.app/music/images/itunes/hip_hop.jpg", | ||
| "Some Text", | ||
| 10) | ||
|
|
||
| val mutableListOf = ArrayList<AlbumModel>(10) | ||
| for (i in 0..10){ | ||
| mutableListOf.add(model.copy(name = "Some Text $i")) | ||
| } | ||
|
|
||
| return mutableListOf | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.github.krottv.tmstemp.data | ||
|
|
||
| import android.util.Log | ||
| import com.github.krottv.tmstemp.domain.AlbumModel | ||
| import com.github.krottv.tmstemp.domain.Tracks | ||
| import com.github.krottv.tmstemp.domain.TracksModel | ||
| import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
| import kotlinx.serialization.json.Json | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import retrofit2.Retrofit | ||
| import retrofit2.create | ||
|
|
||
| class ITunesRemoteDataSourceRetrofit : MusicApi { | ||
|
|
||
| override suspend fun getAlbums(): List<AlbumModel> { | ||
| val retrofit = Retrofit.Builder() | ||
| .baseUrl("https://us-central1-inspiry-2ee60.cloudfunctions.net/getItunesAlbums/") | ||
| .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
| .build() | ||
|
|
||
| val musicApi: MusicApi = retrofit.create() | ||
| return musicApi.getAlbums() | ||
| } | ||
|
|
||
| override suspend fun getTracks(albumId: Long): TracksModel { | ||
| val retrofit = Retrofit.Builder() | ||
| .baseUrl("https://us-central1-inspiry-2ee60.cloudfunctions.net/getItunesTracks/") | ||
| .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
| .build() | ||
|
|
||
| val musicApi: MusicApi = retrofit.create() | ||
|
|
||
| return musicApi.getTracks(1) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.github.krottv.tmstemp.data | ||
|
|
||
| import com.github.krottv.tmstemp.domain.AlbumModel | ||
| import com.github.krottv.tmstemp.domain.TracksModel | ||
| import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
| import kotlinx.serialization.json.Json | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import retrofit2.Retrofit | ||
| import retrofit2.create | ||
|
|
||
| class LibraryRemoteDataSourceRetrofit : MusicApi { | ||
|
|
||
| override suspend fun getAlbums(): List<AlbumModel> { | ||
| val retrofit = Retrofit.Builder() | ||
| .baseUrl("https://us-central1-inspiry-2ee60.cloudfunctions.net/getLibraryAlbums/") | ||
| .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
| .build() | ||
|
|
||
| val musicApi: MusicApi = retrofit.create() | ||
|
|
||
| return musicApi.getAlbums() | ||
| } | ||
|
|
||
| override suspend fun getTracks(albumId: Long): TracksModel { | ||
| val retrofit = Retrofit.Builder() | ||
| .baseUrl("https://us-central1-inspiry-2ee60.cloudfunctions.net/getLibraryTracks/") | ||
| .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) | ||
| .build() | ||
|
|
||
| val musicApi: MusicApi = retrofit.create() | ||
|
|
||
| return musicApi.getTracks(1) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.github.krottv.tmstemp.data | ||
|
|
||
| import com.github.krottv.tmstemp.domain.AlbumModel | ||
| import com.github.krottv.tmstemp.domain.TracksModel | ||
| import retrofit2.http.GET | ||
| import retrofit2.http.Query | ||
|
|
||
| interface MusicApi { | ||
| @GET("getAlbums") | ||
| suspend fun getAlbums(): List<AlbumModel> | ||
|
|
||
| @GET("getTrack") | ||
| suspend fun getTracks(@Query("albumId") albumId: Long): TracksModel | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.github.krottv.tmstemp.domain | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class Album( | ||
| var id: Int, | ||
| var image: String, | ||
| var name: String, | ||
| var trackCount: Int | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.github.krottv.tmstemp.domain | ||
|
|
||
| import android.media.Image | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class AlbumModel( | ||
| val id: Long, | ||
| val image: String, | ||
| val name: String, | ||
| val trackCount: Int) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.github.krottv.tmstemp.domain | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class Tracks( | ||
| var artist: String, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Модели данных нужно делать immutable - var. |
||
| var image: String, | ||
| var title: String, | ||
| var url: String | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.github.krottv.tmstemp.domain | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class TracksModel( | ||
| var album: Album , | ||
| var tracks: ArrayList<Tracks> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нужно делать immutable - List |
||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.github.krottv.tmstemp.presentation | ||
|
|
||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import com.github.krottv.tmstemp.data.ITunesRemoteDataSourceRetrofit | ||
| import com.github.krottv.tmstemp.data.MusicApi | ||
| import com.github.krottv.tmstemp.domain.AlbumModel | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| class AlbumsViewModel(val musicApi: MusicApi): ViewModel(){ | ||
|
|
||
| private val _stateITunes = MutableStateFlow<List<AlbumModel>?>(null) | ||
| val stateITunes: StateFlow<List<AlbumModel>?> = _stateITunes | ||
|
|
||
| fun loadData() { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Стоит добавить обработку ошибок. |
||
| viewModelScope.launch(Dispatchers.IO) { | ||
| _stateITunes.emit(musicApi.getAlbums()) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Плохая практика. MusicApi должен быть отдельным интерфейсом, его не нужно реализовывать.
Нужно создать второй интерфейс DataSource. Да, так больше кода дублируется, но это правильный подход.