This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
28 lesson homework #40
Open
davydenkokolya2
wants to merge
2
commits into
krottv:main
Choose a base branch
from
davydenkokolya2:28_lesson_homework
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
app/src/main/java/com/github/krottv/tmstemp/MainActivity.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/github/krottv/tmstemp/data/DataSourceFake.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/github/krottv/tmstemp/data/ITunesRemoteDataSourceRetrofit.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/github/krottv/tmstemp/data/LibraryRemoteDataSourceRetrofit.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/github/krottv/tmstemp/data/MusicApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/github/krottv/tmstemp/domain/Album.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/github/krottv/tmstemp/domain/AlbumModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/github/krottv/tmstemp/domain/Tracks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
var image: String, | ||
var title: String, | ||
var url: String | ||
) |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/github/krottv/tmstemp/domain/TracksModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/github/krottv/tmstemp/presentation/AlbumsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
_stateITunes.emit(musicApi.getAlbums()) | ||
} | ||
} | ||
|
||
|
||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/github/krottv/tmstemp/presentation/TracksViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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 com.github.krottv.tmstemp.domain.Tracks | ||
import com.github.krottv.tmstemp.domain.TracksModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
class TracksViewModel(val musicApi: MusicApi): ViewModel() { | ||
private val _stateITunes = MutableStateFlow<TracksModel?>(null) | ||
val stateITunes: StateFlow<TracksModel?> = _stateITunes | ||
|
||
fun loadTracks() { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
_stateITunes.emit(musicApi.getTracks(1)) | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/github/krottv/tmstemp/view/AlbumsAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.github.krottv.tmstemp.view | ||
|
||
import android.graphics.Outline | ||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.view.ViewOutlineProvider | ||
import androidx.recyclerview.widget.RecyclerView | ||
import coil.load | ||
import com.github.krottv.tmstemp.R | ||
import com.github.krottv.tmstemp.domain.AlbumModel | ||
import com.github.krottv.tmstemp.domain.Tracks | ||
|
||
class AlbumsAdapter(data: List<AlbumModel>): RecyclerView.Adapter<AlbumsViewHolder>() { | ||
|
||
var data: List<AlbumModel> = data | ||
set(value) { | ||
field = value | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumsViewHolder { | ||
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_album, parent, false) | ||
|
||
return AlbumsViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: AlbumsViewHolder, position: Int) { | ||
val item = data[position] | ||
holder.imageAlbum.load(item.image) | ||
holder.textAlbum.text = item.name | ||
|
||
holder.imageAlbum.clipToOutline = true | ||
holder.imageAlbum.outlineProvider = object: ViewOutlineProvider() { | ||
override fun getOutline(p0: View, p1: Outline) { | ||
p1.setRoundRect(0, 0, p0.width, p0.height, 10.0F) | ||
} | ||
} | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return data.size | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Плохой паттерн.