Skip to content

Commit

Permalink
Merge pull request #9 from clemenceroumy/fix-lists
Browse files Browse the repository at this point in the history
fix: change list enum to data class + init user custom list on startApp
  • Loading branch information
clemenceroumy authored Sep 14, 2023
2 parents 50a019b + 152baa0 commit fd2a7c0
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import com.croumy.hltb_wearos.presentation.data.database.dao.LogDao
import com.croumy.hltb_wearos.presentation.data.database.entity.LogEntity
import com.croumy.hltb_wearos.presentation.models.Timer
import com.croumy.hltb_wearos.presentation.models.TimerState
import com.croumy.hltb_wearos.presentation.models.api.Category
import com.soywiz.klock.DateTime
import com.soywiz.klock.milliseconds
import com.soywiz.klock.plus
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import java.util.Date
import javax.inject.Inject
import javax.inject.Singleton
Expand All @@ -23,7 +27,8 @@ import kotlin.concurrent.fixedRateTimer
@Singleton
class AppService @Inject constructor(
private val logDao: LogDao,
preferencesService: PreferencesService
preferencesService: PreferencesService,
private val hltbService: HLTBService
) {
val isLoggedIn = MutableStateFlow(preferencesService.token != null && preferencesService.token?.isNotEmpty() == true)
val isLoggingIn = MutableStateFlow(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,54 @@ package com.croumy.hltb_wearos.presentation.models.api

import androidx.compose.ui.graphics.Color

enum class Categories {
PLAYING {
abstract class Category {
abstract val value: String
abstract val label: String?
abstract val color: Color

object Playing: Category() {
override val value: String = "playing"
override val label: String = "Playing"
override var label: String = "Playing"
override val color: Color = Color(0xFF26762E)
},
BACKLOG {
}

object Backlog: Category() {
override val value: String = "backlog"
override val label: String = "Backlog"
override var label: String = "Backlog"
override val color: Color = Color(0xFF20669B)
},
CUSTOM {
}

object Custom: Category() {
override val value: String = "custom"
override val label: String = "Paused"
override var label: String? = "Paused"
override val color: Color = Color(0xFF175A53)
},
CUSTOM2 {
}

object Custom2: Category() {
override val value: String = "custom2"
override val label: String = "Stopped"
override var label: String? = "Stopped"
override val color: Color = Color(0xFF175A53)
},
CUSTOM3 {
}

object Custom3: Category() {
override val value: String = "custom3"
override val label: String = "Let's Play"
override var label: String? = "Let's Play"
override val color: Color = Color(0xFF175A53)
},
COMPLETED {
}

object Completed: Category() {
override val value: String = "completed"
override val label: String = "Completed"
override var label: String = "Completed"
override val color: Color = Color(0xFF753C76)
},
RETIRED {
}

object Retired: Category() {
override val value: String = "retired"
override val label: String = "Retired"
override var label: String = "Retired"
override val color: Color = Color(0xFF9F2A2B)
};
}

abstract val value: String
abstract val label: String
abstract val color: Color
companion object {
val all = listOf(Playing, Backlog, Custom, Custom2, Custom3, Completed, Retired).filter { it.label != null && it.label?.isNotEmpty() == true }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.croumy.hltb_wearos.presentation.models.api

import com.croumy.hltbwearos.BuildConfig


data class GameRequest(
val lists: List<String> = Categories.values().map { it.value },
val lists: List<String> = Category.all.map { it.value },
val set_playstyle: String = "comp_all",
val limit: Int = 1000,
val name: String = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.croumy.hltb_wearos.presentation.models.api

import com.soywiz.klock.Time
import com.soywiz.klock.milliseconds
import com.soywiz.klock.seconds

data class GameListResponse(
Expand Down Expand Up @@ -60,16 +59,16 @@ data class Game(
) {
val picture: String get() = "https://howlongtobeat.com/games/$game_image"
val timePlayed get() = Time(invested_pro.seconds)
val categories get(): List<Categories> {
val categories: MutableList<Categories> = mutableListOf()
val categories get(): List<Category> {
val categories: MutableList<Category> = mutableListOf()
when {
list_playing == 1 -> categories.add(Categories.PLAYING)
list_backlog == 1 -> categories.add(Categories.BACKLOG)
list_custom == 1 -> categories.add(Categories.CUSTOM)
list_custom2 == 1 -> categories.add(Categories.CUSTOM2)
list_custom3 == 1 -> categories.add(Categories.CUSTOM3)
list_comp == 1 -> categories.add(Categories.COMPLETED)
list_retired == 1 -> categories.add(Categories.RETIRED)
list_playing == 1 -> categories.add(Category.Playing)
list_backlog == 1 -> categories.add(Category.Backlog)
list_custom == 1 -> categories.add(Category.Custom)
list_custom2 == 1 -> categories.add(Category.Custom2)
list_custom3 == 1 -> categories.add(Category.Custom3)
list_comp == 1 -> categories.add(Category.Completed)
list_retired == 1 -> categories.add(Category.Retired)
}

return categories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ data class UserResponseData(

data class UserResponse(
val user_id: String,
val user_name: String
val user_name: String,
val set_customtab: String,
val set_customtab2: String,
val set_customtab3: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.croumy.hltb_wearos.presentation.data.AppService
import com.croumy.hltb_wearos.presentation.data.HLTBService
import com.croumy.hltb_wearos.presentation.data.PreferencesService
import com.croumy.hltb_wearos.presentation.models.TimerState
import com.croumy.hltb_wearos.presentation.models.api.Categories
import com.croumy.hltb_wearos.presentation.models.api.Category
import com.croumy.hltb_wearos.presentation.models.api.Game
import com.croumy.hltb_wearos.presentation.navigation.NavRoutes
import com.croumy.hltb_wearos.presentation.services.TimerService
Expand Down Expand Up @@ -78,7 +78,7 @@ class GameViewModel @Inject constructor(
isLoading.value = true
val result = hltbService.getGames()
game.value = result?.data?.gamesList?.firstOrNull { it.game_id == gameId }
if(!isRefresh) isInPlayingList.value = game.value?.categories?.contains(Categories.PLAYING) ?: false
if(!isRefresh) isInPlayingList.value = game.value?.categories?.contains(Category.Playing) ?: false
isLoading.value = false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.croumy.hltb_wearos.presentation.ui.home

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.focusable
Expand All @@ -21,11 +20,8 @@ import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
Expand All @@ -34,33 +30,24 @@ import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.input.rotary.onRotaryScrollEvent
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.lifecycleScope
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.foundation.lazy.ScalingLazyListState
import androidx.wear.compose.foundation.lazy.items
import androidx.wear.compose.material.PositionIndicator
import androidx.wear.compose.material.Scaffold
import androidx.wear.compose.material.SwipeToDismissBoxState
import androidx.wear.compose.material.SwipeToDismissValue
import androidx.wear.compose.material.Text
import androidx.wear.compose.material.TimeText
import com.croumy.hltb_wearos.presentation.LocalNavController
import com.croumy.hltb_wearos.presentation.LocalNavSwipeBox
import com.croumy.hltb_wearos.presentation.components.GameItem
import com.croumy.hltb_wearos.presentation.models.Constants
import com.croumy.hltb_wearos.presentation.models.api.Categories
import com.croumy.hltb_wearos.presentation.models.api.Category
import com.croumy.hltb_wearos.presentation.theme.Dimensions
import com.croumy.hltb_wearos.presentation.theme.HLTBwearosTheme
import com.croumy.hltb_wearos.presentation.theme.shimmerColor
import com.croumy.hltb_wearos.presentation.ui.home.logs.LogsScreen
import com.valentinilk.shimmer.shimmer
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@OptIn(ExperimentalFoundationApi::class)
Expand All @@ -76,10 +63,8 @@ fun HomeScreen(
val needRefresh = navController.currentBackStackEntry?.savedStateHandle?.getLiveData<Boolean>("needRefresh")
val previousGameId = navController.currentBackStackEntry?.savedStateHandle?.getLiveData<Int>("previousGameId")

val categories = Categories.values().sortedArray()

// CROWN SCROLL CONFIG + LISTS STATES
val focusRequester = listOf(FocusRequester()).plus((categories).map { FocusRequester() })
val focusRequester = listOf(FocusRequester()).plus((Category.all).map { FocusRequester() })
val horizontalScrollState = rememberLazyListState(initialFirstVisibleItemIndex = 1)
val horizontalFirstVisibleIndex = remember { derivedStateOf { horizontalScrollState.firstVisibleItemIndex } }

Expand All @@ -102,7 +87,7 @@ fun HomeScreen(
// SCROLL TO PLAYING LIST
horizontalScrollState.scrollToItem(1)
//AND SCROLL TO GAME ITEM
val gameIndex = viewModel.gamesByCategories[Categories.PLAYING]?.indexOfFirst { it.game_id == previousGameId?.value }
val gameIndex = viewModel.gamesByCategories[Category.Playing]?.indexOfFirst { it.game_id == previousGameId?.value }
viewModel.currentListState.value = viewModel.listStates[1]
viewModel.currentListState.value.scrollToItem(gameIndex ?: 0)
navController.currentBackStackEntry?.savedStateHandle?.set(Constants.HOME_PREVIOUS_GAME_ID, null)
Expand All @@ -127,7 +112,7 @@ fun HomeScreen(
)
}

itemsIndexed(viewModel.categories) { index, category ->
itemsIndexed(Category.all) { index, category ->
val games = viewModel.gamesByCategories[category] ?: emptyList()

Column(
Expand All @@ -142,7 +127,7 @@ fun HomeScreen(
horizontal = Dimensions.sPadding,
vertical = Dimensions.xxsPadding
),
) { Text(category.label) }
) { Text(category.label ?: "") }

ScalingLazyColumn(
state = viewModel.listStates[index + 1],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.croumy.hltb_wearos.presentation.ui.home

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.foundation.lazy.ScalingLazyListState
import com.croumy.hltb_wearos.presentation.data.AppService
import com.croumy.hltb_wearos.presentation.data.HLTBService
import com.croumy.hltb_wearos.presentation.models.api.Categories
import com.croumy.hltb_wearos.presentation.models.api.Category
import com.croumy.hltb_wearos.presentation.models.api.Game
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
Expand All @@ -22,14 +19,13 @@ class HomeViewModel @Inject constructor(
private val hltbService: HLTBService
): ViewModel() {
private val games: MutableState<List<Game>> = mutableStateOf(emptyList())
val categories = Categories.values().sortedArray()
// IN VIEWMODEL TO KEEP LIST SCROLLSTATE ON NAVIGATION
val listStates = listOf(ScalingLazyListState(initialCenterItemIndex = 0)).plus((categories).map { ScalingLazyListState(initialCenterItemIndex = 0) })
val listStates = listOf(ScalingLazyListState(initialCenterItemIndex = 0)).plus((Category.all).map { ScalingLazyListState(initialCenterItemIndex = 0) })
val currentListState = mutableStateOf(listStates[1])

val gamesByCategories get(): Map<Categories, List<Game>> {
val map = mutableMapOf<Categories, List<Game>>()
categories.forEach { category ->
val gamesByCategories get(): Map<Category, List<Game>> {
val map = mutableMapOf<Category, List<Game>>()
Category.all.forEach { category ->
map[category] = games.value.filter { it.categories.contains(category) }
}
return map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ fun StartApp(
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
lifecycleOwner.lifecycleScope.launch {checkConditions() }
lifecycleOwner.lifecycleScope.launch {
checkConditions()
}
}
}
lifecycleOwner.lifecycle.addObserver(observer)
Expand All @@ -74,6 +76,7 @@ fun StartApp(
if (isLoggedIn.value) {
if (isLoggedIn.value && isLoggingIn.value) {
delay(400) // TIME TO SHOW THE CHECK ICON
viewModel.initCategories()
}
navigateToHome()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.croumy.hltb_wearos.presentation.ui.startapp

import androidx.compose.runtime.collectAsState
import androidx.lifecycle.ViewModel
import com.croumy.hltb_wearos.presentation.data.AppService
import com.croumy.hltb_wearos.presentation.data.PreferencesService
import com.croumy.hltb_wearos.presentation.data.HLTBService
import com.croumy.hltb_wearos.presentation.models.api.Category
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class StartAppViewModel @Inject constructor(
val appService: AppService
): ViewModel()
val appService: AppService,
private val hltbService: HLTBService
) : ViewModel() {
suspend fun initCategories() {
val user = hltbService.getUser()
Category.Custom.label = user?.set_customtab
Category.Custom2.label = user?.set_customtab2
Category.Custom3.label = user?.set_customtab3
}
}

0 comments on commit fd2a7c0

Please sign in to comment.