Skip to content

Commit

Permalink
feat #126 내 정보 수정 데이터 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sun-hyuck committed Jun 4, 2024
1 parent 17a65b1 commit 28b365d
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class UserModel(
var birthDay: Int? = null,
var gender: String = "", // 성별
var email: String = "", // 이메일
var phoneNumber: String = "", // 휴대폰 번호
var phoneNumber: String? = "", // 휴대폰 번호
var agreementCheck1: Boolean = false, // 서비스 이용 약관 동의 여부
var agreementCheck2: Boolean = false, // 개인정보 수집 동의 여부
var agreementCheck3: Boolean = false, // 마케팅 정보 수신 동의 여부
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,88 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import androidx.lifecycle.Observer
import kr.co.lion.application.finalproject_aparttalk.R
import kr.co.lion.application.finalproject_aparttalk.databinding.FragmentEditUserInfoBinding
import kr.co.lion.application.finalproject_aparttalk.databinding.FragmentEditUserInfoNumberBinding
import kr.co.lion.application.finalproject_aparttalk.util.InfoFragmentName


class EditUserInfoFragment : Fragment() {
lateinit var fragmentEditUserInfoBinding: FragmentEditUserInfoBinding
private var _binding: FragmentEditUserInfoBinding? = null
private val binding get() = _binding!!
lateinit var infoActivity: InfoActivity


private lateinit var userViewModel: UserViewModel


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment

fragmentEditUserInfoBinding = FragmentEditUserInfoBinding.inflate(layoutInflater)
_binding = FragmentEditUserInfoBinding.inflate(inflater, container, false)
infoActivity = activity as InfoActivity
userViewModel = infoActivity.userViewModel

settingToolbar()
settingButton()
return fragmentEditUserInfoBinding.root
populateUserInfo()

val uid = "some-uid"
userViewModel.loadUser(uid)



return binding.root
}

private fun populateUserInfo() {
userViewModel.user.observe(viewLifecycleOwner) { user ->
user?.let {
binding.editUserInfoNameInput.setText(it.name)
binding.editUserInfoEmailInput.setText(it.email)
binding.editUserInfoNumberInput.setText(it.phoneNumber ?: "-")
binding.editUserInfoBirthInput.setText("${it.birthYear}-${it.birthMonth}-${it.birthDay}")
binding.editUserInfoAdressInput.setText("${it.apartmentDong}${it.apartmentHo}")
}
}
}

fun settingToolbar() {
fragmentEditUserInfoBinding.apply {
binding.apply {
editUserInfoToolbar.apply {
// 뒤로가기
setNavigationIcon(R.drawable.icon_back)
setNavigationOnClickListener {
// 전화면으로 돌아가기.
infoActivity.removeFragment(InfoFragmentName.EDIT_USER_INFO_NUMBER_FRAGMENT)
infoActivity.supportFragmentManager.popBackStack()
}
}


}
}
fun settingButton() {
fragmentEditUserInfoBinding.apply {
userInfoEditButton.apply {
setOnClickListener {
infoActivity.replaceFragment(InfoFragmentName.INFO_FRAGMENT, true, true, null)
binding.userInfoEditButton.setOnClickListener {
val user = userViewModel.user.value ?: return@setOnClickListener

}
}
val updatedUser = user.copy(
name = binding.editUserInfoNameInput.text.toString(),
email = binding.editUserInfoEmailInput.text.toString(),
phoneNumber = binding.editUserInfoNumberInput.text.toString().takeIf { it.isNotBlank() },
apartmentDong = binding.editUserInfoAdressInput.text.toString().split("").getOrNull(0)?.toIntOrNull() ?: user.apartmentDong,
apartmentHo = binding.editUserInfoAdressInput.text.toString().split("").getOrNull(0)?.toIntOrNull() ?: user.apartmentHo
)
userViewModel.updateUser(updatedUser)
infoActivity.supportFragmentManager.popBackStack()
}
}

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,127 +1,59 @@
package kr.co.lion.application.finalproject_aparttalk.ui.info

import android.os.Bundle
import android.os.SystemClock
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.google.android.material.transition.MaterialSharedAxis
import androidx.lifecycle.ViewModelProvider
import kr.co.lion.application.finalproject_aparttalk.R
import kr.co.lion.application.finalproject_aparttalk.databinding.ActivityInfoBinding
import kr.co.lion.application.finalproject_aparttalk.db.local.LocalUserDataSource
import kr.co.lion.application.finalproject_aparttalk.db.remote.UserDataSource
import kr.co.lion.application.finalproject_aparttalk.repository.UserRepository
import kr.co.lion.application.finalproject_aparttalk.util.InfoFragmentName

class InfoActivity : AppCompatActivity() {
lateinit var binding: ActivityInfoBinding

// 프래그먼트의 주소값을 담을 프로퍼티
var oldFragment: Fragment? = null
var newFragment: Fragment? = null
lateinit var userViewModel: UserViewModel

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

binding = ActivityInfoBinding.inflate(layoutInflater)
setContentView(binding.root)
// ViewModel 초기화
val userRepository = UserRepository(UserDataSource(), LocalUserDataSource(this))
userViewModel = ViewModelProvider(this, UserViewModelFactory(userRepository)).get(UserViewModel::class.java)

// FireCheckMainFragment 나타나도록 한다.
replaceFragment(InfoFragmentName.INFO_FRAGMENT, false, false, null)
}

// 지정한 Fragment를 보여주는 메서드
// name : 프래그먼트 이름
// addToBackStack : BackStack에 포함 시킬 것인지
// isAnimate : 애니메이션을 보여줄 것인지
// data : 새로운 프래그먼트에 전달할 값이 담겨져 있는 Bundle 객체
fun replaceFragment(name: InfoFragmentName, addToBackStack:Boolean, isAnimate:Boolean, data:Bundle?){

SystemClock.sleep(200)

// Fragment를 교체할 수 있는 객체를 추출한다.
val fragmentTransaction = supportFragmentManager.beginTransaction()

// oldFragment에 newFragment가 가지고 있는 Fragment 객체를 담아준다.
if(newFragment != null){
oldFragment = newFragment
// 초기 프래그먼트 설정
if (savedInstanceState == null) {
replaceFragment(InfoFragmentName.INFO_FRAGMENT, false, false, null)
}
}

// 이름으로 분기한다.
// Fragment의 객체를 생성하여 변수에 담아준다.
when(name){
InfoFragmentName.INFO_FRAGMENT -> {
newFragment = InfoFragment()
}
InfoFragmentName.EDIT_USER_INFO_FRAGMENT -> {
newFragment = EditUserInfoFragment()
}
InfoFragmentName.EDIT_USER_INFO_NUMBER_FRAGMENT -> {
newFragment = EditUserInfoNumberFragment()
}
fun replaceFragment(
fragmentName: InfoFragmentName,
addToBackStack: Boolean,
clearBackStack: Boolean,
args: Bundle?
) {
val fragment: Fragment = when (fragmentName) {
InfoFragmentName.INFO_FRAGMENT -> InfoFragment()
InfoFragmentName.EDIT_USER_INFO_FRAGMENT -> EditUserInfoFragment()
}

// 새로운 Fragment에 전달할 객체가 있다면 arguments 프로퍼티에 넣어준다.
if(data != null){
newFragment?.arguments = data
if (args != null) {
fragment.arguments = args
}

if(newFragment != null){

// 애니메이션 설정
if(isAnimate == true){
// oldFragment -> newFragment
// oldFragment : exitTransition
// newFragment : enterTransition

// newFragment -> oldFragment
// oldFragment : reenterTransition
// newFragment : returnTransition
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.infoFragmentContainerView, fragment)

// MaterialSharedAxis : 좌우, 위아래, 공중 바닥 사이로 이동하는 애니메이션 효과
// X - 좌우
// Y - 위아래
// Z - 공중 바닥
// 두 번째 매개변수 : 새로운 화면이 나타나는 것인지 여부를 설정해준다.
// true : 새로운 화면이 나타나는 애니메이션이 동작한다.
// false : 이전으로 되돌아가는 애니메이션이 동작한다.

if(oldFragment != null){
// old에서 new가 새롭게 보여질 때 old의 애니메이션
oldFragment?.exitTransition = MaterialSharedAxis(MaterialSharedAxis.X, true)
// new에서 old로 되돌아갈때 old의 애니메이션
oldFragment?.reenterTransition = MaterialSharedAxis(MaterialSharedAxis.X, false)

oldFragment?.enterTransition = null
oldFragment?.returnTransition = null
}

// old에서 new가 새롭게 보여질 때 new의 애니메이션
newFragment?.enterTransition = MaterialSharedAxis(MaterialSharedAxis.X, true)
// new에서 old로 되돌아갈때 new의 애니메이션
newFragment?.returnTransition = MaterialSharedAxis(MaterialSharedAxis.X, false)

newFragment?.exitTransition = null
newFragment?.reenterTransition = null
}

// Fragment를 교체한다.(이전 Fragment가 없으면 새롭게 추가하는 역할을 수행한다)
// 첫 번째 매개 변수 : Fragment를 배치할 FragmentContainerView의 ID
// 두 번째 매개 변수 : 보여주고하는 Fragment 객체를
fragmentTransaction.replace(R.id.infoFragmentContainerView, newFragment!!)

// addToBackStack 변수의 값이 true면 새롭게 보여질 Fragment를 BackStack에 포함시켜 준다.
if(addToBackStack == true){
// BackStack 포함 시킬때 이름을 지정해주면 원하는 Fragment를 BackStack에서 제거할 수 있다.
fragmentTransaction.addToBackStack(name.str)
}
// Fragment 교체를 확정한다.
fragmentTransaction.commit()
if (addToBackStack) {
transaction.addToBackStack(null)
}
}

// BackStack에서 Fragment를 제거한다.
fun removeFragment(name: InfoFragmentName){
SystemClock.sleep(200)
if (clearBackStack) {
supportFragmentManager.popBackStack(null, androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE)
}

// 지정한 이름으로 있는 Fragment를 BackStack에서 제거한다.
supportFragmentManager.popBackStack(name.str, FragmentManager.POP_BACK_STACK_INCLUSIVE)
transaction.commit()
}
}
Loading

0 comments on commit 28b365d

Please sign in to comment.