Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔀 :: (#201) Feature/160 collect as state #202

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.9.1'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

implementation "io.coil-kt:coil-compose:1.4.0"
implementation "androidx.lifecycle:lifecycle-runtime-compose:2.6.0-alpha04"
}

apply plugin: 'com.google.gms.google-services'
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@ import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.semicolon.design.Body3
import com.semicolon.design.color.primary.black.black
Expand All @@ -42,28 +38,16 @@ import com.xquare.domain.entity.notification.AlarmEntity
import com.xquare.xquare_android.R
import com.xquare.xquare_android.component.Header
import com.xquare.xquare_android.util.DevicePaddings
import com.xquare.xquare_android.util.makeToast
import org.openjdk.tools.javac.parser.Tokens.Token
import org.openjdk.tools.javac.parser.Tokens.tokensKey
import java.time.temporal.ChronoUnit

@Composable
fun AlarmScreen(
navController: NavController,
) {
val context = LocalContext.current
val viewModel: AlarmViewModel = hiltViewModel()
var alarmList: AlarmEntity? by remember { mutableStateOf(null) }
val alarmList = viewModel.alarmList.collectAsStateWithLifecycle().value
LaunchedEffect(Unit) {
viewModel.fetchAlarmList()
viewModel.eventFlow.collect {
when (it) {
is AlarmViewModel.Event.Success -> alarmList = it.data
is AlarmViewModel.Event.Failure -> {
makeToast(context, "알림을 불러오는데 실패했습니다")
}
}
}
}
Alarm(
alarmList = alarmList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.xquare.xquare_android.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import javax.inject.Inject

@HiltViewModel
Expand All @@ -21,12 +22,11 @@ class AlarmViewModel @Inject constructor(
fun fetchAlarmList() =
execute(
job = { fetchAlarmUseCase.execute(Unit) },
onSuccess = { emitEvent(Event.Success(it)) },
onFailure = { emitEvent(Event.Failure) }
onSuccess = { it.collect{ alarmList -> _alarmList.tryEmit(alarmList)} },
onFailure = { }
)

sealed class Event {
data class Success(val data: AlarmEntity) : Event()
object Failure : Event()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.xquare.xquare_android.feature.allmeal

import MealDetail
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
Expand All @@ -10,9 +9,9 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.semicolon.design.color.primary.white.white
import com.xquare.domain.entity.meal.AllMealEntity
Expand All @@ -33,12 +32,10 @@ fun AllMealScreen(
viewModel.fetchAllMeal()
viewModel.eventFlow.collect {
when (it) {
is AllMealViewModel.Event.Success -> {
allMeal = it.data
}
is AllMealViewModel.Event.Failure -> {
makeToast(context, "급식을 불러오는 데 실패했습니다")
}
is AllMealViewModel.Event.Success -> allMeal = it.data
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AllMealViewModel @Inject constructor(
private val fetchAllMealUseCase: FetchAllMealUseCase
) : BaseViewModel<AllMealViewModel.Event>() {


fun fetchAllMeal() =
execute(
job = { fetchAllMealUseCase.execute(Unit) },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import androidx.compose.animation.AnimatedContentScope.SlideDirection.Companion.End
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
Expand All @@ -23,8 +22,6 @@ import com.semicolon.design.color.primary.gray.gray700
import com.semicolon.design.color.primary.gray.gray800
import com.semicolon.design.color.primary.gray.gray900
import com.semicolon.design.color.primary.purple.purple200
import com.semicolon.design.color.primary.purple.purple400
import com.semicolon.design.color.system.green.green100
import com.xquare.domain.entity.meal.AllMealEntity
import com.xquare.xquare_android.util.toKorean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -27,6 +26,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import coil.compose.rememberAsyncImagePainter
import com.semicolon.design.Body1
Expand Down Expand Up @@ -55,10 +55,10 @@ fun HomeScreen(navController: NavController) {
val mainActivity = context as MainActivity

val viewModel: HomeViewModel = hiltViewModel()
val userData = viewModel.userSimpleData.collectAsState().value
val meal = viewModel.todayMeal.collectAsState().value
val classPosition = viewModel.classPosition.collectAsState().value
val passCheck = viewModel.passCheck.collectAsState().value
val userData = viewModel.userSimpleData.collectAsStateWithLifecycle().value
val meal = viewModel.todayMeal.collectAsStateWithLifecycle().value
val classPosition = viewModel.classPosition.collectAsStateWithLifecycle().value
val passCheck = viewModel.passCheck.collectAsStateWithLifecycle().value
LaunchedEffect(Unit) {
mainActivity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
viewModel.run {
Expand Down Expand Up @@ -323,7 +323,7 @@ fun HomeMealItem(
borderColor:Color,
) {
val scrollState = rememberScrollState()
var borderColor = borderColor
var borderColor1 = borderColor

Column(
modifier = Modifier
Expand All @@ -339,8 +339,8 @@ fun HomeMealItem(
.verticalScroll(scrollState)
) {
Row(modifier = Modifier.fillMaxWidth()) {
if (borderColor == gray50) {
borderColor = black
if (borderColor1 == gray50) {
borderColor1 = black
}

Body1(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.xquare.xquare_android.feature.pick

import android.os.Build
import android.text.TextUtils
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -21,7 +18,6 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -36,13 +32,13 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import coil.compose.rememberAsyncImagePainter
import com.semicolon.design.Body1
import com.semicolon.design.Subtitle2
import com.semicolon.design.Subtitle4
import com.semicolon.design.color.primary.gray.gray200
import com.semicolon.design.color.primary.gray.gray300
import com.semicolon.design.color.primary.gray.gray50
import com.semicolon.design.color.primary.gray.gray700
import com.semicolon.design.color.primary.gray.gray800
Expand All @@ -59,7 +55,7 @@ fun PassScreen(
val context = LocalContext.current
val mainActivity: MainActivity = context as MainActivity
val vm: PassViewModel = hiltViewModel()
val passData = vm.passData.collectAsState().value
val passData = vm.passData.collectAsStateWithLifecycle().value

LaunchedEffect(Unit) {
mainActivity.window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.xquare.xquare_android.feature.pick

import android.os.Build
import androidx.annotation.RequiresApi
import com.xquare.domain.entity.pick.PassDataEntity
import com.xquare.domain.usecase.pick.FetchPassDataUseCase
import com.xquare.xquare_android.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import javax.inject.Inject

@HiltViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.ui.unit.dp
import com.semicolon.design.Body1
import com.semicolon.design.Body3
import com.semicolon.design.Subtitle4
import com.semicolon.design.color.primary.black.black
import com.semicolon.design.color.primary.gray.gray50
import com.xquare.domain.entity.point.PointHistoriesEntity
import org.threeten.bp.LocalDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Scaffold
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.semicolon.design.Body2
import com.semicolon.design.color.primary.dark.dark50
Expand All @@ -25,25 +25,13 @@ import com.xquare.domain.entity.point.PointHistoriesEntity
import com.xquare.xquare_android.R
import com.xquare.xquare_android.component.CenterAppBar
import com.xquare.xquare_android.util.DevicePaddings
import com.xquare.xquare_android.util.makeToast

@Composable
fun PointHistoryScreen(navController: NavController) {
val context = LocalContext.current
val viewModel: PointHistoryViewModel = hiltViewModel()
var pointHistories: PointHistoriesEntity? by remember { mutableStateOf(null) }
val pointHistories = viewModel.pointHistory.collectAsStateWithLifecycle().value
LaunchedEffect(Unit) {
viewModel.fetchGoodPointHistories(offlineOnly = false)
viewModel.eventFlow.collect {
when (it) {
is PointHistoryViewModel.Event.Success -> {
pointHistories = it.data
}
is PointHistoryViewModel.Event.Failure -> {
makeToast(context, "상벌점을 불러오는 데 실패했습니다")
}
}
}
}
PointHistory(
pointHistoriesEntity = pointHistories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.xquare.domain.usecase.point.FetchBadPointHistoriesUseCase
import com.xquare.domain.usecase.point.FetchGoodPointHistoriesUseCase
import com.xquare.xquare_android.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

@HiltViewModel
Expand All @@ -13,24 +15,31 @@ class PointHistoryViewModel @Inject constructor(
private val fetchBadPointHistoriesUseCase: FetchBadPointHistoriesUseCase,
) : BaseViewModel<PointHistoryViewModel.Event>() {

private val _pointHistory = MutableStateFlow(
PointHistoriesEntity(
goodPoint = 0,
badPoint = 0,
pointHistories = listOf(),
)
)
val pointHistory: StateFlow<PointHistoriesEntity> = _pointHistory
fun fetchGoodPointHistories(offlineOnly: Boolean = true) {
execute(
job = { fetchGoodPointHistoriesUseCase.execute(offlineOnly) },
onSuccess = { it.collect { pointHistory -> emitEvent(Event.Success(pointHistory)) } },
onSuccess = { it.collect { pointHistory -> _pointHistory.tryEmit(pointHistory)} },
onFailure = { emitEvent(Event.Failure) }
)
}

fun fetchBadPointHistories(offlineOnly: Boolean = true) {
execute(
job = { fetchBadPointHistoriesUseCase.execute(offlineOnly) },
onSuccess = { it.collect { pointHistory -> emitEvent(Event.Success(pointHistory)) } },
onSuccess = { it.collect { pointHistory -> _pointHistory.tryEmit(pointHistory)} },
onFailure = { emitEvent(Event.Failure) }
)
}

sealed class Event {
data class Success(val data: PointHistoriesEntity) : Event()
object Failure : Event()
}
}
Loading