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 all 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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.9.1'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.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,26 +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 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 @@ -6,6 +6,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 @@ -19,12 +20,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
Expand Up @@ -10,6 +10,7 @@ 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.color.primary.white.white
import com.xquare.domain.entity.meal.AllMealEntity
Expand All @@ -30,12 +31,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
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 @@ -26,6 +25,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.AsyncImage
import com.semicolon.design.Body1
Expand Down Expand Up @@ -54,10 +54,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 @@ -317,7 +317,7 @@ fun HomeMealItem(
borderColor:Color,
) {
val scrollState = rememberScrollState()
var borderColor = borderColor
var borderColor1 = borderColor

Column(
modifier = Modifier
Expand All @@ -333,8 +333,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
Expand Up @@ -17,7 +17,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 @@ -31,6 +30,7 @@ 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.AsyncImage
import com.semicolon.design.Body1
Expand All @@ -52,7 +52,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
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()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xquare.xquare_android.feature.profile

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.net.Uri
Expand Down Expand Up @@ -36,6 +37,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import coil.compose.AsyncImage
import com.semicolon.design.Body1
Expand All @@ -54,24 +56,23 @@ import com.xquare.xquare_android.component.CenterAppBar
import com.xquare.xquare_android.component.modal.ConfirmModal
import com.xquare.xquare_android.navigation.AppNavigationItem
import com.xquare.xquare_android.util.DevicePaddings
import com.xquare.xquare_android.util.makeToast
import com.xquare.xquare_android.util.toFile
import org.threeten.bp.format.DateTimeFormatter
import java.io.File

@SuppressLint("UnrememberedMutableState")
@Composable
fun ProfileScreen(navController: NavController) {
val context = LocalContext.current
val viewModel: ProfileViewModel = hiltViewModel()
var profile: ProfileEntity? by remember { mutableStateOf(null) }
val profile = viewModel.profile.collectAsStateWithLifecycle().value
var githubConnected by remember { mutableStateOf<Boolean?>(null) }

LaunchedEffect(Unit) {
viewModel.fetchProfile()
viewModel.fetchOAuthCheck()
viewModel.eventFlow.collect {
when (it) {
is ProfileViewModel.Event.Success -> {
is ProfileViewModel.Event.Success -> {
profile = it.data
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.xquare.xquare_android.feature.profile

import android.util.Log
import com.xquare.domain.entity.github.GithubOAuthEntity
import com.xquare.domain.entity.profile.ProfileEntity
import com.xquare.domain.usecase.attachment.UploadFileUseCase
Expand All @@ -11,6 +10,8 @@ import com.xquare.domain.usecase.user.FetchProfileUseCase
import com.xquare.domain.usecase.user.FixProfileImageUseCase
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.first
import java.io.File
import javax.inject.Inject
Expand All @@ -25,11 +26,18 @@ class ProfileViewModel @Inject constructor(
private val fetchGithubOAuthCheckUseCase: FetchGithubOAuthCheckUseCase,
) : BaseViewModel<ProfileViewModel.Event>() {

private val _profile = MutableStateFlow(
ProfileEntity(
"", "", "",1,1,1,""
)
)
val profile: StateFlow<ProfileEntity> = _profile

fun fetchProfile() =
execute(
job = { fetchProfileUseCase.execute(Unit) },
onSuccess = { it.collect { profile -> emitEvent(Event.Success(profile)) } },
onFailure = { emitEvent(Event.Failure) }
onSuccess = { it.collect { profile -> _profile.tryEmit(profile)} },
onFailure = { }
)

fun fixProfileImage(image: String?) {
Expand Down Expand Up @@ -84,9 +92,7 @@ class ProfileViewModel @Inject constructor(

object OAuthNotConnected : Event()
data class UploadFileSuccess(val data: List<String>) : Event()
object UploadFileFailure : Event()

object ImageChangeSuccess : Event()
object ImageChangeFailure : Event()
}
}
Loading
Loading