Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

homework #47

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 1 addition & 7 deletions app/src/main/java/com/example/app/data/UploadMusicWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,16 @@ class UploadMusicWorker(
) : CoroutineWorker(appContext, params) {

private val downloadNotification: DownloadNotification = DownloadNotification(appContext)
private val baseUrl = inputData.getString("2")
private val remoteDataSourceRetrofit = RemoteDataSourceRetrofit()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Используй koin + Dependency Inversion

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут у меня не работает через koin

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сделай, чтобы работало. https://insert-koin.io/docs/reference/koin-android/workmanager/


override suspend fun doWork(): Result {

setForeground(getForegroundInfo())
val url = inputData.getString("1")
Log.i("test", url!!)
val fileName = url.substring(url.lastIndexOf("/") + 1)
val fileName = url!!.substring(url.lastIndexOf("/") + 1)
val pathWhereYouWantToSaveFile = appContext.filesDir.absolutePath + fileName
val responseBody = url.let { remoteDataSourceRetrofit.downloadFile(it).body() }
saveFile(responseBody, pathWhereYouWantToSaveFile)
//delay(2000L)
Log.i("test", appContext.filesDir.absolutePath.toString())

//Log.i("Test", tracks.url + " " + pathWhereYouWantToSaveFile)

return Result.success()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.app.data.purchase

import com.android.billingclient.api.ProductDetails
import com.example.mymusicplayer.data.purchase.Mapper
import com.example.app.domain.purchase.PeriodType
import com.example.app.domain.purchase.ProductEntity

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/example/app/data/purchase/Mapper.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.mymusicplayer.data.purchase
package com.example.app.data.purchase

interface Mapper<FROM, TO> {
fun map(from: FROM): TO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Activity
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.BillingFlowParams
import com.android.billingclient.api.ProductDetails
import com.example.mymusicplayer.data.purchase.PurchaseStateInteractorGooglePlay
import com.example.app.domain.purchase.ProductEntity
import com.example.app.domain.purchase.PurchaseMakeInteractor

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.example.mymusicplayer.data.purchase
package com.example.app.data.purchase

import android.content.Context
import com.android.billingclient.api.*
import com.example.app.data.purchase.GooglePlayProductMapper
import com.example.app.domain.purchase.ProductEntity
import com.example.app.domain.purchase.PurchaseStateInteractor
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -17,7 +16,7 @@ class PurchaseStateInteractorGooglePlay(
}

val _isPremium = MutableStateFlow(false)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

либо делай private, либо это одна и та же переменная

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

К переменной isPremium нельзя обратиться

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ну а зачем две isPremium тогда?

override val isPremium: StateFlow<Boolean> = _isPremium
override var isPremium: StateFlow<Boolean> = _isPremium
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Оставляй val.


private val purchasesUpdatedListener =
PurchasesUpdatedListener { billingResult, purchases ->
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/example/app/data/remote/MusicApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ interface MusicApi {
suspend fun getItunesAlbums(): List<AlbumModel>

@GET("api/?p=getItunesTracks")
suspend fun getItunesTracks(@Query("albumId") albumId: Long): TracksWithAlbums
suspend fun getItunesTracks(albumId: Long): TracksWithAlbums

@GET("api/?p=getLibraryAlbums")
suspend fun getLibraryAlbums(): List<AlbumModel>

@GET("api/?p=getLibraryTracks")
suspend fun getLibraryTracks(@Query("albumId") albumId: Long): TracksWithAlbums
suspend fun getLibraryTracks(albumId: Long): TracksWithAlbums

@Streaming
@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ class RemoteDataSourceRetrofit() : RemoteDataSource {
private val retrofit = Retrofit.Builder()
.baseUrl("https://stellio.ru/.inspiry/")
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()
.build().create() as MusicApi

override suspend fun getItunesAlbums(): List<AlbumModel> {
return (retrofit.create() as MusicApi).getItunesAlbums()
return retrofit.getItunesAlbums()
}

override suspend fun getItunesTracks(albumId: Long): TracksWithAlbums {
return (retrofit.create() as MusicApi).getItunesTracks(1)
return retrofit.getItunesTracks(1)
}

override suspend fun getLibraryAlbums(): List<AlbumModel> {
return (retrofit.create() as MusicApi).getLibraryAlbums()
return retrofit.getLibraryAlbums()
}

override suspend fun getLibraryTracks(albumId: Long): TracksWithAlbums {
return (retrofit.create() as MusicApi).getLibraryTracks(albumId)
return retrofit.getLibraryTracks(albumId)
}

override suspend fun downloadFile(fileUrl: String): Response<ResponseBody> {
return (retrofit.create() as MusicApi).downloadFile(fileUrl);
return retrofit.downloadFile(fileUrl);
}
}
12 changes: 0 additions & 12 deletions app/src/main/java/com/example/app/data/room/AlbumsDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,4 @@ interface AlbumsDao {
@Query("SELECT * FROM albums where typeAlbum = 'library'")
suspend fun getLibraryAlbums(): List<AlbumDBModel>

/*@Query("select * from albums where id = :albumId")
fun findAlbumById(albumId: Long): Flow<AlbumDBModel?>*/

/*@Transaction
suspend fun savesAlbumsTenTime(albums: List<AlbumDBModel>) {
for (i in 1..10) {
saveAlbums(albums)
}
}*/

/*@Query("select * from albums where id = :albumId")
suspend fun getAlbumWithTracks(albumId: Long): TracksWithAlbumEntity*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ package com.example.app.presentation.view
import android.app.Service
import android.content.Intent
import android.os.IBinder
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*


class UsbConnectService : Service() {

private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
private val downloadNotification: DownloadNotification = DownloadNotification(this)

override fun onCreate() {
super.onCreate()

runBlocking {
launch {
startForeground(10, downloadNotification.downloadNotification())
loadDataFake()
stopSelf()
downloadNotification.downloadDoneNotification()
}
coroutineScope.launch {
startForeground(10, downloadNotification.downloadNotification())
loadDataFake()
stopSelf()
downloadNotification.downloadDoneNotification()
}
}

Expand All @@ -29,16 +27,19 @@ class UsbConnectService : Service() {
}

private suspend fun loadDataFake() {

downloadNotification.createNotificationChannel()

for (i in 1..100) {
//Log.i("test", "Done")
downloadNotification.processDownloadNotification(i)
}
}

override fun onBind(p0: Intent?): IBinder? {
return null
}

override fun onDestroy() {
super.onDestroy()
coroutineScope.cancel()
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package com.example.app.presentation.view.albumsrecycler

import android.content.res.Resources
import android.graphics.Outline
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import coil.load

import com.example.app.domain.AlbumModel
import com.example.app.view.R

class AlbumsAdapter(data: List<AlbumModel>) : RecyclerView.Adapter<AlbumsViewHolder>() {

var data: List<AlbumModel> = data
set(value) {
field = value
}
class AlbumsAdapter(var data: List<AlbumModel>) : RecyclerView.Adapter<AlbumsViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumsViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_album, parent, false)
Expand All @@ -26,17 +23,20 @@ class AlbumsAdapter(data: List<AlbumModel>) : RecyclerView.Adapter<AlbumsViewHol

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() {
holder.imageAlbum.apply {
load(item.image)
outlineProvider = object : ViewOutlineProvider() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

дублирование кода -переноси в общие функции.

override fun getOutline(p0: View, p1: Outline) {
p1.setRoundRect(0, 0, p0.width, p0.height, 10.0F)
p1.setRoundRect(0, 0, p0.width, p0.height, dpToPx(10))
}
}
}

}
private fun dpToPx(dp: Int): Float = dp * Resources.getSystem().displayMetrics.density

override fun getItemCount(): Int {
return data.size
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.app.presentation.view.tracksrecycler

import android.content.res.Resources
import android.graphics.Outline
import android.view.LayoutInflater
import android.view.View
Expand All @@ -10,38 +11,40 @@ import coil.load
import com.example.app.domain.TrackModel
import com.example.app.view.R

class TracksAdapter(data: List<TrackModel>, private val onItemClick: (View, TrackModel) -> Boolean) : RecyclerView.Adapter<TracksViewHolder>() {

var data: List<TrackModel> = data
set(value) {
field = value
}
class TracksAdapter(
var data: List<TrackModel>,
private val onItemClick: (View, TrackModel) -> Boolean
) : RecyclerView.Adapter<TracksViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TracksViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_track, parent, false)

return TracksViewHolder(view)
}

private fun dpToPx(dp: Int): Float = dp * Resources.getSystem().displayMetrics.density
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Во все классы будешь копировать?


override fun onBindViewHolder(holder: TracksViewHolder, position: Int) {
val item = data[position]
holder.imageTrack.load(item.image.replace("{w}", "200").replace("{h}", "200"))
holder.titleTrack.text = item.title
holder.textTrack.text = item.artist

holder.imageTrack.clipToOutline = true
holder.imageTrack.outlineProvider = object: ViewOutlineProvider() {
override fun getOutline(p0: View, p1: Outline) {
p1.setRoundRect(0, 0, p0.width, p0.height, 8.0F)
holder.imageTrack.apply {
load(item.image.replace("{w}", "200").replace("{h}", "200"))
clipToOutline = true
outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(p0: View, p1: Outline) {
p1.setRoundRect(0, 0, p0.width, p0.height, dpToPx(10))
}
}
}

holder.itemView.setOnLongClickListener{
holder.titleTrack.text = item.title
holder.textTrack.text = item.artist
holder.itemView.setOnLongClickListener {
onItemClick(it, item)
return@setOnLongClickListener true
}
}


override fun getItemCount(): Int {
return data.size
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.example.app.presentation.viewmodel

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.app.data.remote.TracksMyMusicDataSource
import com.example.app.data.remote.TracksMyMusicDataSourceImpl
import com.example.app.domain.TrackModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -17,7 +15,6 @@ class TracksMyMusicViewModel(val musicApi: TracksMyMusicDataSource): ViewModel()
val stateITunes: StateFlow<List<TrackModel>?> = _stateITunes

fun loadTracks() {
//Log.i("test", "view")
viewModelScope.launch(Dispatchers.IO) {
_stateITunes.emit(musicApi.getTracks())
}
Expand Down