Skip to content

Commit

Permalink
#6 mypage_fragment/설정 화면 : 2 닉네임 변경
Browse files Browse the repository at this point in the history
 - 닉네임 변경 화면 및 변경하고 돌아왔을 때 완료 팝업 구현
  • Loading branch information
likppi10 committed Aug 22, 2022
1 parent 28d61a8 commit 636ca85
Show file tree
Hide file tree
Showing 9 changed files with 625 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.ftw.hometerview.ui.main.home.review.LocationReviewListViewModel
import com.ftw.hometerview.ui.searchaddressbuilding.SearchAddressBuildingViewModel
import com.ftw.hometerview.ui.searchcompanyresult.SearchCompanyResultViewModel
import com.ftw.hometerview.ui.splash.SplashViewModel
import com.ftw.hometerview.ui.updatenickname.UpdateNicknameViewModel
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -82,4 +83,13 @@ class ActivityViewModelModule {
)
}

@Provides
@ActivityScoped
fun provideUpdateNicknameViewModel(
dispatcher: Dispatcher
): UpdateNicknameViewModel {
return UpdateNicknameViewModel(
dispatcher
)
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
package com.ftw.hometerview.ui.main.mypage

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.ftw.hometerview.R
import com.ftw.hometerview.databinding.FragmentHomeBinding
import com.ftw.hometerview.databinding.FragmentMyPageBinding
import com.ftw.hometerview.ui.main.home.HomeViewModel
import com.ftw.hometerview.ui.updatenickname.UpdateNicknameActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class MyPageFragment : Fragment() {

private val updateNicknameLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode != Activity.RESULT_OK) return@registerForActivityResult
val nickname =
result.data?.getStringExtra(UpdateNicknameActivity.UPDATE_NICKNAME_RESULT_KEY)
if (nickname == null) return@registerForActivityResult
viewModel.showPopup.value = true
}

companion object {
fun newInstance() = MyPageFragment()
}
Expand All @@ -27,6 +45,7 @@ class MyPageFragment : Fragment() {
@Inject
lateinit var viewModel: MyPageViewModel


override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -45,5 +64,76 @@ class MyPageFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
observeEvent()
observeShowBanner()
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private fun observeEvent() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.event.collect { event ->
when (event) {
MyPageViewModel.Event.None -> {}
is MyPageViewModel.Event.onClickUpdateNickname -> {
updateNicknameActivity(event.nickname)
}
}
}
}
}
}

private fun observeShowBanner() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.showPopup.collect { showBanner ->

binding.updateCompletePopup.isVisible = showBanner
if (showBanner) {
binding.updateCompletePopup.apply {

visibility = View.INVISIBLE

val appearAnimation =
AnimationUtils.loadAnimation(requireContext(), R.anim.appear_popup)
val fadeoutAnimation =
AnimationUtils.loadAnimation(requireContext(), R.anim.anim_fade_out)

appearAnimation.setAnimationListener(object :
Animation.AnimationListener {
override fun onAnimationStart(arg0: Animation) {
visibility = View.VISIBLE
}
override fun onAnimationRepeat(arg0: Animation) {}
override fun onAnimationEnd(arg0: Animation) {
startAnimation(fadeoutAnimation)
}
})

fadeoutAnimation.setAnimationListener(object :
Animation.AnimationListener {
override fun onAnimationStart(arg0: Animation) {}
override fun onAnimationRepeat(arg0: Animation) {}
override fun onAnimationEnd(arg0: Animation) {
visibility = View.GONE
viewModel.showPopup.value = false
}
})

startAnimation(appearAnimation)
}
}
}
}
}
}

private fun updateNicknameActivity(nickname: String) {
updateNicknameLauncher.launch(UpdateNicknameActivity.newIntent(requireContext(), nickname))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ class MyPageViewModel (
private val getCachedUserUseCase: GetCachedUserUseCase
) {

sealed class Event {
object None : Event()
class onClickUpdateNickname(val nickname: String) : Event()
}

private val _user: MutableStateFlow<User> = MutableStateFlow(User.NONE)
val user: StateFlow<User> = _user.asStateFlow()

private val _event: MutableStateFlow<Event> = MutableStateFlow(Event.None)
val event: StateFlow<Event> = _event.asStateFlow()

val showPopup: MutableStateFlow<Boolean> = MutableStateFlow(false)

init {
CoroutineScope(dispatcher.ui()).launch {
flow {
Expand All @@ -25,7 +35,11 @@ class MyPageViewModel (
.collect {
_user.value = it
}

}
}
}

fun onClickUpdateNickname() {
_event.value = Event.onClickUpdateNickname(user.value.nickName)
_event.value = Event.None
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,94 @@
package com.ftw.hometerview.ui.updatenickname

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcelable
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.ftw.hometerview.R
import com.ftw.hometerview.databinding.ActivityBuildingReviewBinding
import com.ftw.hometerview.databinding.ActivityUpdateNickNameBinding
import com.ftw.hometerview.ui.buildinglist.BuildingListActivity
import com.ftw.hometerview.ui.main.MainActivity
import com.ftw.hometerview.ui.main.MainViewModel
import com.ftw.hometerview.ui.searchcompanyresult.SearchCompanyResultActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
import javax.inject.Inject

@AndroidEntryPoint
class UpdateNicknameActivity : AppCompatActivity() {

companion object {
val UPDATE_NICKNAME_RESULT_KEY = "UPDATE_NICKNAME_RESULT_KEY"
fun newIntent(context: Context, nickname: String): Intent {
return Intent(context, UpdateNicknameActivity::class.java)
.putExtra(UPDATE_NICKNAME_RESULT_KEY, nickname)
}
}

@Inject
lateinit var viewModel: UpdateNicknameViewModel

private lateinit var binding: ActivityUpdateNickNameBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_update_nick_name)

binding = DataBindingUtil.setContentView<ActivityUpdateNickNameBinding>(
this,
R.layout.activity_update_nick_name
).apply {
viewModel = this@UpdateNicknameActivity.viewModel
}

val nickname = intent.getStringExtra(UPDATE_NICKNAME_RESULT_KEY) ?: return
viewModel.nickname.value = nickname
observe()
}

fun observe() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.nickname.collect {
binding.lengthTextview.text = getString(R.string.written_nickname_length, it.length)
}
}
}
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.event.collect { event ->
when (event) {
UpdateNicknameViewModel.Event.None -> {}
is UpdateNicknameViewModel.Event.OnClickNext -> {
onClickNextFromThirdStepSearchCompany(event.nickname)
}
}
}
}
}
}

private fun onClickNextFromThirdStepSearchCompany(nickname: String) {
setResult(
Activity.RESULT_OK,
Intent().putExtra(UPDATE_NICKNAME_RESULT_KEY, nickname)
)

Log.d("1232123", "onClickNextFromThirdStepSearchCompany: $nickname")
finish()
}

@Parcelize
private data class Argument(
val nickname: String
) : Parcelable
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
package com.ftw.hometerview.ui.updatenickname

import com.ftw.domain.entity.Company
import com.ftw.domain.entity.LocationReview
import com.ftw.domain.entity.User
import com.ftw.domain.usecase.review.GetLocationReviewsUseCase
import com.ftw.domain.usecase.user.GetCachedUserUseCase
import com.ftw.hometerview.dispatcher.Dispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

class UpdateNicknameViewModel(
dispatcher: Dispatcher,
private val getCachedUserUseCase: GetCachedUserUseCase
dispatcher: Dispatcher
) {

private val _user: MutableStateFlow<User> = MutableStateFlow(User.NONE)
val user: StateFlow<User> = _user.asStateFlow()
sealed class Event {
object None : Event()
class OnClickNext(val nickname: String) : Event()
}

private val _event: MutableStateFlow<Event> = MutableStateFlow(Event.None)
val event: StateFlow<Event> = _event.asStateFlow()

val nickname: MutableStateFlow<String> = MutableStateFlow("")

init {
CoroutineScope(dispatcher.ui()).launch {
flow {
emit(getCachedUserUseCase())
}
.catch { emit(User("", Company.NONE)) }
.collect {
_user.value = it
}

}
fun onClickUpdateComplete() {
_event.value = Event.OnClickNext(nickname.value)
_event.value = Event.None
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/anim/appear_popup.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromYDelta="-90%"
android:toYDelta="0">
</translate>
Loading

0 comments on commit 636ca85

Please sign in to comment.