Skip to content

Commit

Permalink
Merge pull request #2 from codejoo9098/test/modifyall
Browse files Browse the repository at this point in the history
Test : 모든 유닛 테스트 컴파일 에러 해결 및 로직 수정
  • Loading branch information
codejoo9098 authored Oct 18, 2024
2 parents 27b272e + b99afa1 commit 0851e56
Show file tree
Hide file tree
Showing 12 changed files with 466 additions and 427 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import androidx.appcompat.widget.SearchView
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -21,6 +24,7 @@ import com.juniori.puzzle.ui.videoplayer.playvideo.PlayVideoActivity
import com.juniori.puzzle.ui.gallery.GalleryState
import com.juniori.puzzle.domain.constant.PlayResultConst.RESULT_DELETE
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MyGalleryFragment : Fragment() {
Expand Down Expand Up @@ -76,75 +80,86 @@ class MyGalleryFragment : Fragment() {
layoutManager = gridLayoutManager
}

viewModel.list.observe(viewLifecycleOwner) { dataList ->
binding.mygallerySwipeRefresh.isRefreshing = false

recyclerAdapter.submitList(dataList)
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.list.collect { dataList ->
binding.mygallerySwipeRefresh.isRefreshing = false

binding.mygalleryAddVideoBtn.isVisible = dataList.isEmpty()
binding.mygalleryAddVideoText.isVisible = dataList.isEmpty()
}
recyclerAdapter.submitList(dataList)

binding.mygalleryAddVideoBtn.setOnClickListener {
view.findNavController().navigate(R.id.bottomsheet_main_addvideo)
binding.mygalleryAddVideoBtn.isVisible = dataList.isEmpty()
binding.mygalleryAddVideoText.isVisible = dataList.isEmpty()
}
}
}

binding.mygallerySwipeRefresh.setOnRefreshListener {
viewModel.getMyData()
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.refresh.collect { isRefresh ->
binding.progressMyGallery.isVisible = isRefresh
}
}
}

viewModel.refresh.observe(viewLifecycleOwner) { isRefresh ->
binding.progressMyGallery.isVisible = isRefresh
}
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.state.collect { state ->
when (state) {
GalleryState.NONE -> {
snackBar?.dismiss()
}

viewModel.state.observe(viewLifecycleOwner) { state ->
when (state) {
GalleryState.NONE -> {
snackBar?.dismiss()
}
GalleryState.END_PAGING -> {
snackBar = Snackbar.make(
view,
R.string.gallery_end_paging,
Snackbar.LENGTH_SHORT
).apply {
setAction(R.string.gallery_check) {
dismiss()
}
}

GalleryState.END_PAGING -> {
snackBar = Snackbar.make(
view,
R.string.gallery_end_paging,
Snackbar.LENGTH_SHORT
).apply {
setAction(R.string.gallery_check) {
dismiss()
snackBar?.show()
}
}

snackBar?.show()
}

GalleryState.NETWORK_ERROR_PAGING -> {
snackBar =
Snackbar.make(
view,
R.string.gallery_paging_error,
Snackbar.LENGTH_INDEFINITE
)
.setAction(R.string.gallery_retry) {
viewModel.getPaging(recyclerAdapter.itemCount)
}
snackBar?.show()
}
GalleryState.NETWORK_ERROR_PAGING -> {
snackBar =
Snackbar.make(
view,
R.string.gallery_paging_error,
Snackbar.LENGTH_INDEFINITE
)
.setAction(R.string.gallery_retry) {
viewModel.getPaging(recyclerAdapter.itemCount)
}
snackBar?.show()
}

GalleryState.NETWORK_ERROR_BASE -> {
binding.mygallerySwipeRefresh.isRefreshing = false
snackBar =
Snackbar.make(
view,
R.string.gallery_init_load_error,
Snackbar.LENGTH_INDEFINITE
)
.setAction(R.string.gallery_retry) {
viewModel.getMyData()
}
snackBar?.show()
GalleryState.NETWORK_ERROR_BASE -> {
binding.mygallerySwipeRefresh.isRefreshing = false
snackBar =
Snackbar.make(
view,
R.string.gallery_init_load_error,
Snackbar.LENGTH_INDEFINITE
)
.setAction(R.string.gallery_retry) {
viewModel.getMyData()
}
snackBar?.show()
}
}
}
}
}

binding.mygalleryAddVideoBtn.setOnClickListener {
view.findNavController().navigate(R.id.bottomsheet_main_addvideo)
}

binding.mygallerySwipeRefresh.setOnRefreshListener {
viewModel.getMyData()
}

binding.searchMyGallery.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.juniori.puzzle.ui.gallery.mygallery

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.juniori.puzzle.data.APIResponse
Expand All @@ -14,7 +12,10 @@ import com.juniori.puzzle.ui.gallery.GalleryState
import com.juniori.puzzle.domain.constant.PagingConst.ITEM_CNT
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -24,16 +25,16 @@ class MyGalleryViewModel @Inject constructor(
val getUserInfoUseCase: GetUserInfoUseCase,
val getSearchedMyVideoUseCase: GetSearchedMyVideoUseCase
) : ViewModel() {
private val _list = MutableLiveData<List<VideoInfoEntity>>()
val list: LiveData<List<VideoInfoEntity>>
private val _list = MutableStateFlow<List<VideoInfoEntity>>(emptyList())
val list: StateFlow<List<VideoInfoEntity>>
get() = _list

private val _refresh = MutableLiveData(false)
val refresh: LiveData<Boolean>
private val _refresh = MutableStateFlow(false)
val refresh: StateFlow<Boolean>
get() = _refresh

private val _state = MutableLiveData(GalleryState.NONE)
val state: LiveData<GalleryState>
private val _state = MutableStateFlow(GalleryState.NONE)
val state: StateFlow<GalleryState>
get() = _state

private var query = ""
Expand All @@ -52,9 +53,10 @@ class MyGalleryViewModel @Inject constructor(
getMyData()
}

private fun getQueryData() {
private fun getQueryData() = viewModelScope.launch {
if (refresh.value == true) {
return
cancel()
return@launch
}
val uid = getUid()

Expand All @@ -64,31 +66,30 @@ class MyGalleryViewModel @Inject constructor(
if (uid == null) {
_state.value = GalleryState.NETWORK_ERROR_BASE
} else {
viewModelScope.launch {
_refresh.value = true
val data = getSearchedMyVideoUseCase(uid, 0, query)
if (data is APIResponse.Success) {
_state.value = GalleryState.NONE

val result = data.result
if (result.isNullOrEmpty().not()) {
if (result.size < ITEM_CNT) {
pagingEndFlag = true
}
_list.value = result
_refresh.value = true
val data = getSearchedMyVideoUseCase(uid, 0, query)
if (data is APIResponse.Success) {
_state.value = GalleryState.NONE

val result = data.result
if (result.isEmpty().not()) {
if (result.size < ITEM_CNT) {
pagingEndFlag = true
}
} else {
_state.value = GalleryState.NETWORK_ERROR_BASE
_list.value = result
}

_refresh.value = false
} else {
_state.value = GalleryState.NETWORK_ERROR_BASE
}

_refresh.value = false
}
}

private fun getBaseData() {
private fun getBaseData() = viewModelScope.launch {
if (refresh.value == true) {
return
cancel()
return@launch
}
val uid = getUid()

Expand All @@ -98,68 +99,63 @@ class MyGalleryViewModel @Inject constructor(
if (uid == null) {
_state.value = GalleryState.NETWORK_ERROR_BASE
} else {
viewModelScope.launch {
_refresh.value = true
val data = getMyVideoListUseCase(uid, 0)
if (data is APIResponse.Success) {
_state.value = GalleryState.NONE

val result = data.result
if (result.isNullOrEmpty().not()) {
if (result.size < ITEM_CNT) {
pagingEndFlag = true
}
_list.value = result
_refresh.value = true
val data = getMyVideoListUseCase(uid, 0)
if (data is APIResponse.Success) {
_state.value = GalleryState.NONE

val result = data.result
if (result.isEmpty().not()) {
if (result.size < ITEM_CNT) {
pagingEndFlag = true
}
} else {
_state.value = GalleryState.NETWORK_ERROR_BASE
_list.value = result
}

_refresh.value = false
} else {
_state.value = GalleryState.NETWORK_ERROR_BASE
}

_refresh.value = false
}
}

fun getPaging(start: Int) {
fun getPaging(start: Int) = viewModelScope.launch {
if (refresh.value == true || pagingEndFlag) {
return
cancel()
return@launch
}

val uid = getUid()
if (uid == null) {
_state.value = GalleryState.NETWORK_ERROR_PAGING
} else {
viewModelScope.launch {
_refresh.value = true
val data = if (query.isBlank()) {
getMyVideoListUseCase(uid, start)
} else {
getSearchedMyVideoUseCase(uid, start, query)
}
_refresh.value = true
val data = if (query.isBlank()) {
getMyVideoListUseCase(uid, start)
} else {
getSearchedMyVideoUseCase(uid, start, query)
}

if (data is APIResponse.Success) {
val result = data.result
if (result.isNullOrEmpty()) {
viewModelScope.launch(Dispatchers.IO) {
_state.postValue(GalleryState.END_PAGING)
delay(1000)
_state.postValue(GalleryState.NONE)
}
pagingEndFlag = true
} else {
_state.value = GalleryState.NONE
addItems(result)
}
if (data is APIResponse.Success) {
val result = data.result
if (result.isEmpty()) {
_state.value = GalleryState.END_PAGING
delay(1000)
_state.value = GalleryState.NONE
pagingEndFlag = true
} else {
_state.value = GalleryState.NETWORK_ERROR_PAGING
_state.value = GalleryState.NONE
addItems(result)
}

_refresh.value = false
} else {
_state.value = GalleryState.NETWORK_ERROR_PAGING
}

_refresh.value = false
}
}

fun getMyData() {
fun getMyData() = viewModelScope.launch {
if (query.isEmpty()) {
getBaseData()
} else {
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/com/juniori/puzzle/ui/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.juniori.puzzle.ui.login

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.juniori.puzzle.data.APIResponse
import com.juniori.puzzle.domain.APIErrorType
import com.juniori.puzzle.domain.TempAPIResponse
import com.juniori.puzzle.domain.entity.UserInfoEntity
import com.juniori.puzzle.domain.usecase.common.GetUserInfoUseCase
Expand All @@ -22,13 +24,13 @@ class LoginViewModel @Inject constructor(
val loginFlow: StateFlow<TempAPIResponse<UserInfoEntity>?> = _loginFlow

fun loginUser(idToken: String) = viewModelScope.launch {
val result = requestLoginUseCase(idToken).apply {
if (this is TempAPIResponse.Success) {
postUserInfoUseCase(data.uid, data.nickname, data.profileImage)
}
val response = requestLoginUseCase(idToken)
if (response is TempAPIResponse.Success && postUserInfoUseCase(response.data.uid, response.data.nickname, response.data.profileImage) is APIResponse.Success) {
_loginFlow.value = response
}
else {
_loginFlow.value = TempAPIResponse.Failure(APIErrorType.SERVER_ERROR)
}

_loginFlow.value = result
}

}
Loading

0 comments on commit 0851e56

Please sign in to comment.