Skip to content

Commit

Permalink
Merge pull request #213 from wsb7788/feature-aos/refactor
Browse files Browse the repository at this point in the history
Refactor:  지도 조정 클래스 분리, 네비게이션 컴포넌트 활용, 패키지 세분화
  • Loading branch information
wsb7788 authored Dec 4, 2023
2 parents dd0e243 + 5f1371e commit 2a7a638
Show file tree
Hide file tree
Showing 48 changed files with 269 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.toRequestBody
Expand Down Expand Up @@ -55,36 +56,26 @@ class PostRepositoryImpl @Inject constructor(

override fun postCreatePost(title: String, postBlocks: List<PostBlockState>): Flow<CreatePostResponse> {

val request = CreatePostRequest(
title = title,
postBlocks = postBlocks.map {
when (it) {
is PostBlockState.IMAGE -> {
val requestBody = it.bitmap?.toByteArray()?.toRequestBody("image/webp".toMediaTypeOrNull())
?: return emptyFlow()
val multipartBody = MultipartBody.Part.createFormData("file", "image", requestBody)
val uploadResult = try {
runBlocking(Dispatchers.IO) {
snapPointApi.postImage(multipartBody)
return flowOf(true)
.map{
CreatePostRequest(
title = title,
postBlocks = postBlocks.map {
when (it) {
is PostBlockState.IMAGE -> {
val requestBody = it.bitmap?.toByteArray()?.toRequestBody("image/webp".toMediaType())!!
val multipartBody = MultipartBody.Part.createFormData("file", "image", requestBody)
val uploadResult = snapPointApi.postImage(multipartBody)
// TODO - 하나의 이미지 블럭에 사진이 여러개 들어갈 때 대응
it.asPostBlock().copy(
files = listOf(File(uploadResult.uuid))
)
}
} catch (e: Exception) {
e.printStackTrace()
return emptyFlow()
else -> it.asPostBlock()
}

// TODO - 하나의 이미지 블럭에 사진이 여러개 들어갈 때 대응
it.asPostBlock().copy(
files = listOf(File(uploadResult.uuid))
)
}
else -> it.asPostBlock()
}
}
)
Log.d("TAG", "postCreatePost: ${Json.encodeToString(request)}")

return flowOf(true)
.map{
)
}.map{request ->
snapPointApi.createPost(request)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class AuthActivity : BaseActivity<ActivityAuthBinding>(R.layout.activity_auth) {

initBinding()

collectViewModelData()
}

private fun initBinding() {
Expand All @@ -32,24 +31,6 @@ class AuthActivity : BaseActivity<ActivityAuthBinding>(R.layout.activity_auth) {
}
}

private fun collectViewModelData() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.RESUMED) {
viewModel.event.collect { event ->
when (event) {
is AuthEvent.Success -> {
navigateToMainMapActivity()
}
}
}
}
}
}

private fun navigateToMainMapActivity() {
val intent = Intent(this, MainActivity::class.java)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME)
startActivity(intent)
finish()
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,4 @@ import javax.inject.Inject

@HiltViewModel
class AuthViewModel @Inject constructor() : ViewModel() {

private val _event: MutableSharedFlow<AuthEvent> = MutableSharedFlow(
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)
val event: SharedFlow<AuthEvent> = _event.asSharedFlow()

var idCache: String = ""

fun sendSuccessResult() {
_event.tryEmit(AuthEvent.Success)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.boostcampwm2023.snappoint.presentation.auth.signin

sealed class SignInEvent{
data object NavigateToMainActivity: SignInEvent()
data class ShowMessage(val errorResId: Int): SignInEvent()
data object NavigateToSignup: SignInEvent()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.boostcampwm2023.snappoint.presentation.signin
package com.boostcampwm2023.snappoint.presentation.auth.signin

data class SignInFormState(
val email: String = "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.boostcampwm2023.snappoint.presentation.signin
package com.boostcampwm2023.snappoint.presentation.auth.signin

import android.os.Bundle
import android.view.View
Expand All @@ -7,7 +7,9 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.navigateUp
import com.boostcampwm2023.snappoint.R
import com.boostcampwm2023.snappoint.databinding.FragmentSigninBinding
import com.boostcampwm2023.snappoint.presentation.auth.AuthViewModel
Expand Down Expand Up @@ -40,15 +42,15 @@ class SignInFragment : BaseFragment<FragmentSigninBinding>(R.layout.fragment_sig
repeatOnLifecycle(Lifecycle.State.RESUMED) {
viewModel.event.collect { event ->
when (event) {
is SignInEvent.Fail -> {
showToastMessage(event.error)
is SignInEvent.ShowMessage -> {
showToastMessage(event.errorResId)
}

is SignInEvent.Success -> {
activityViewModel.sendSuccessResult()
is SignInEvent.NavigateToMainActivity -> {
navigateToMainActivity()
}

is SignInEvent.Signup -> {
is SignInEvent.NavigateToSignup -> {
navigateToSignup()
}
}
Expand All @@ -57,7 +59,13 @@ class SignInFragment : BaseFragment<FragmentSigninBinding>(R.layout.fragment_sig
}
}

private fun navigateToMainActivity() {
findNavController().navigate(SignInFragmentDirections.actionLoginFragmentToMainActivity())
requireActivity().finish()

}

private fun navigateToSignup() {
findNavController().navigate(R.id.action_loginFragment_to_signupFragment)
findNavController().navigate(SignInFragmentDirections.actionLoginFragmentToSignupFragment())
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.boostcampwm2023.snappoint.presentation.signin
package com.boostcampwm2023.snappoint.presentation.auth.signin

import android.util.Patterns
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -28,12 +28,14 @@ class SignInViewModel @Inject constructor(
private val loginRepository: SignInRepository
) : ViewModel() {

private val _signInFormUiState: MutableStateFlow<SignInFormState> = MutableStateFlow(SignInFormState(
private val _signInFormUiState: MutableStateFlow<SignInFormState> = MutableStateFlow(
SignInFormState(
email = "[email protected]",
password = "Str!n8Str!n8",
isEmailValid = true,
isPasswordValid = true
))
)
)
val signInFormUiState: StateFlow<SignInFormState> = _signInFormUiState.asStateFlow()

private val _event: MutableSharedFlow<SignInEvent> = MutableSharedFlow(
Expand Down Expand Up @@ -71,10 +73,10 @@ class SignInViewModel @Inject constructor(
}
.onEach {
loginUtil.setUserAuthData(email, password)
_event.emit(SignInEvent.Success)
_event.emit(SignInEvent.NavigateToMainActivity)
}
.catch {
_event.emit(SignInEvent.Fail(R.string.login_activity_fail))
_event.emit(SignInEvent.ShowMessage(R.string.login_activity_fail))
}
.onCompletion {
setProgressBarState(false)
Expand All @@ -83,7 +85,7 @@ class SignInViewModel @Inject constructor(
}

fun onSignUpButtonClick() {
_event.tryEmit(SignInEvent.Signup)
_event.tryEmit(SignInEvent.NavigateToSignup)
}

private fun setProgressBarState(isInProgress: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.boostcampwm2023.snappoint.presentation.signup
package com.boostcampwm2023.snappoint.presentation.auth.signup

import androidx.databinding.BindingAdapter
import com.google.android.material.textfield.TextInputLayout
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.boostcampwm2023.snappoint.presentation.auth.signup

sealed class SignupEvent {
data object NavigateToSignIn : SignupEvent()
data class ShowMessage(val messageResId: Int) : SignupEvent()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.boostcampwm2023.snappoint.presentation.signup
package com.boostcampwm2023.snappoint.presentation.auth.signup

import android.os.Bundle
import android.view.View
Expand Down Expand Up @@ -48,21 +48,20 @@ class SignupFragment : BaseFragment<FragmentSignupBinding>(R.layout.fragment_sig
repeatOnLifecycle(Lifecycle.State.RESUMED) {
viewModel.event.collect { event ->
when (event) {
is SignupEvent.Fail -> {
is SignupEvent.ShowMessage -> {
showToastMessage(event.messageResId)
}

is SignupEvent.Success -> {
showToastMessage(R.string.signup_fragment_create_account_success)
navigateToLogin()
is SignupEvent.NavigateToSignIn -> {
navigateToSignIn()
}
}
}
}
}
}

private fun navigateToLogin() {
private fun navigateToSignIn() {
findNavController().popBackStack()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.boostcampwm2023.snappoint.presentation.signup
package com.boostcampwm2023.snappoint.presentation.auth.signup

data class SignupUiState(
val email: String = "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.boostcampwm2023.snappoint.presentation.signup
package com.boostcampwm2023.snappoint.presentation.auth.signup

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand Down Expand Up @@ -142,12 +142,14 @@ class SignupViewModel @Inject constructor(
setProgressBarState(true)
}
.onEach {
_event.emit(SignupEvent.Success)
_event.emit(SignupEvent.NavigateToSignIn)
}
.catch {
updateEmailErrorResId(it.message)
_event.emit(
SignupEvent.Fail(_uiState.value.emailErrorResId ?: R.string.signup_fragment_fail)
SignupEvent.ShowMessage(
_uiState.value.emailErrorResId ?: R.string.signup_fragment_fail
)
)
}
.onCompletion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ abstract class BaseActivity<B: ViewDataBinding>(
}

protected fun showToastMessage(resId: Int) {
Toast.makeText(this, getString(resId), Toast.LENGTH_LONG).show()
Toast.makeText(this, getString(resId), Toast.LENGTH_SHORT).show()
}

protected fun showToastMessage(message: String) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ abstract class BaseFragment<B : ViewDataBinding>(
}

protected fun showToastMessage(resId: Int) {
Toast.makeText(activity, getString(resId), Toast.LENGTH_LONG).show()
Toast.makeText(activity, getString(resId), Toast.LENGTH_SHORT).show()
}

protected fun showToastMessage(message: String) {
Toast.makeText(activity, message, Toast.LENGTH_LONG).show()
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CreatePostViewModel @Inject constructor(
if(index == idx) {
when(postBlock){
is PostBlockState.TEXT -> postBlock.copy(content = content)
is PostBlockState.IMAGE -> postBlock.copy(content = content)
is PostBlockState.IMAGE -> postBlock.copy(description = content)
is PostBlockState.VIDEO -> TODO()
}
}else{
Expand Down
Loading

0 comments on commit 2a7a638

Please sign in to comment.