Explorar o código

code improvements

Milan Jurkulak %!s(int64=3) %!d(string=hai) anos
pai
achega
e8e63343eb

+ 1 - 1
app/src/main/kotlin/org/mjdev/tvapp/data/remote/Stream.kt

@@ -16,7 +16,7 @@ import com.squareup.moshi.JsonClass
 class Stream {
 
     @Json(name = "channel")
-    var channel: String? = null
+    var channelId: String? = null
 
     @Json(name = "url")
     var url: String? = null

+ 1 - 1
app/src/main/kotlin/org/mjdev/tvapp/ui/pages/MainPage.kt

@@ -54,7 +54,7 @@ class MainPage : Page() {
             MainViewModel.mockMainViewModel(context)
         }
 
-        val streamingData = viewModel.categoryList.collectAsState()
+        val streamingData = viewModel.movieList.collectAsState()
         val countryList = viewModel.countryList.collectAsState()
         val featuredMovieList = viewModel.featuredMovieList.collectAsState()
         val messages = viewModel.messages.collectAsState()

+ 17 - 28
app/src/main/kotlin/org/mjdev/tvapp/viewmodel/MainViewModel.kt

@@ -10,16 +10,14 @@ package org.mjdev.tvapp.viewmodel
 
 import android.content.ComponentName
 import android.content.Context
-import androidx.lifecycle.viewModelScope
 import dagger.hilt.android.lifecycle.HiltViewModel
 import dagger.hilt.android.qualifiers.ApplicationContext
-import kotlinx.coroutines.flow.SharingStarted
 import kotlinx.coroutines.flow.StateFlow
 import kotlinx.coroutines.flow.channelFlow
 import kotlinx.coroutines.flow.flow
-import kotlinx.coroutines.flow.stateIn
 import kotlinx.coroutines.runBlocking
 import org.mjdev.tvapp.BuildConfig
+import org.mjdev.tvapp.R
 import org.mjdev.tvapp.activity.MainActivity
 import org.mjdev.tvlib.extensions.ListExt.asMap
 import org.mjdev.tvlib.helpers.cursor.AudioCursor
@@ -57,9 +55,12 @@ class MainViewModel @Inject constructor(
     @Inject
     lateinit var localPhotoCursor: PhotoCursor
 
+    private val noCategoryString by lazy {
+        context.getString(R.string.no_category)
+    }
+
     val apps = appsManager(
-        context,
-        ComponentName(
+        context, ComponentName(
             BuildConfig.APPLICATION_ID,
             MainActivity::class.java.name
         )
@@ -67,11 +68,7 @@ class MainViewModel @Inject constructor(
 
     val messages: StateFlow<List<Message>> = channelFlow {
         send(movieRepository.getMessages().getOrThrow())
-    }.stateIn(
-        viewModelScope,
-        SharingStarted.WhileSubscribed(5000L),
-        listOf()
-    )
+    }.stateInViewModel()
 
     val featuredMovieList: StateFlow<List<Any?>> = flow {
         mutableListOf<Any?>().apply {
@@ -88,25 +85,21 @@ class MainViewModel @Inject constructor(
         }.also { result ->
             emit(result)
         }
-    }.stateIn(
-        viewModelScope,
-        SharingStarted.WhileSubscribed(5000L),
-        listOf()
-    )
-
-    val categoryList: StateFlow<Map<String, List<Movie>>> = flow {
-        movieRepository.getMovies().getOrThrow().sortedBy { m ->
+    }.stateInViewModel()
+
+    val movieList: StateFlow<Map<String, List<Movie>>> = flow {
+        movieRepository.getMovies().getOrDefault(emptyList()).map { movie ->
+            movie.apply {
+                category = category ?: noCategoryString
+            }
+        }.sortedBy { m ->
             m.category
         }.asMap { m ->
             Pair(m.category, m)
         }.also { map ->
             emit(map)
         }
-    }.stateIn(
-        viewModelScope,
-        SharingStarted.WhileSubscribed(5000L),
-        mapOf()
-    )
+    }.stateInViewModel()
 
     val countryList: StateFlow<List<String>> = flow {
         movieRepository.getMovies().getOrThrow().filter { it.country != null }.map {
@@ -116,11 +109,7 @@ class MainViewModel @Inject constructor(
         }.also {
             emit(it)
         }
-    }.stateIn(
-        viewModelScope,
-        SharingStarted.WhileSubscribed(5000L),
-        listOf()
-    )
+    }.stateInViewModel()
 
     fun findMovie(id: Long?): Movie? = runBlocking {
         movieRepository.findMovieById(id).getOrNull()