Skip to content

Commit

Permalink
#18 feat: 로그아웃 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunBaee committed Sep 23, 2023
1 parent 8b88790 commit 5708f66
Show file tree
Hide file tree
Showing 21 changed files with 404 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.c7z.mappilogue_aos.data.data

data class MyPageMenuItem(
val icon : Int,
val title : String
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ class UserRepositoryImpl @Inject constructor(private val source : UserSource): U
override suspend fun requestModifyUserNickname(body: RequestModifyUserNickname): Result<Int> {
return source.requestModifyUserNickname(body)
}

override suspend fun requestLogOut(): Result<Int> {
return source.requestLogOut()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.PATCH
import retrofit2.http.POST

interface UserService {
@GET("/api/v1/users/profile")
Expand All @@ -16,4 +17,7 @@ interface UserService {
suspend fun requestModifyUserNickname(
@Body body : RequestModifyUserNickname
) : Response<BaseResponse>

@POST("/api/v1/users/logout")
suspend fun requestLogOut() : Response<BaseResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ class UserRemoteSource @Inject constructor(private val service : UserService): U
else -> Result.failure(IllegalArgumentException(res.errorBody()?.convertAndGetCode().toString()))
}
}

override suspend fun requestLogOut(): Result<Int> {
val res = service.requestLogOut()
return when(res.code()) {
in 200..399 -> Result.success(res.code())
else -> Result.failure(IllegalArgumentException(res.errorBody()?.convertAndGetCode().toString()))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ interface UserRepository {
suspend fun requestUserProfileData() : Result<ResponseUserProfileData.ResultUserProfileData>

suspend fun requestModifyUserNickname(body : RequestModifyUserNickname) : Result<Int>

suspend fun requestLogOut() : Result<Int>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ interface UserSource {
suspend fun requestUserProfileData() : Result<ResponseUserProfileData.ResultUserProfileData>

suspend fun requestModifyUserNickname(body : RequestModifyUserNickname) : Result<Int>

suspend fun requestLogOut() : Result<Int>
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class ComponentDialogTwoButton (
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.component_dialog_2_button, container, false)
Log.e("----", "onCreateView: CREATE", )
initBinding()
initUi()
return binding.root
Expand All @@ -48,6 +47,7 @@ class ComponentDialogTwoButton (
private fun initText() {
when(tag) {
"STOP_WRITING_TODO" -> case_STOP_WRITING_TODO()
"LOG_OUT" -> case_LOG_OUT()
}
}

Expand All @@ -62,7 +62,8 @@ class ComponentDialogTwoButton (
private fun initSecondClick() {
binding.componentDialog2ButtonTvSecondBtn.setOnClickListener {
onSecondBtnClicked.invoke()
onDismiss() }
onDismiss()
}
}

private fun case_STOP_WRITING_TODO() {
Expand All @@ -72,6 +73,12 @@ class ComponentDialogTwoButton (
binding.componentDialog2ButtonTvSecondBtn.text = "나가기"
}

private fun case_LOG_OUT() {
binding.componentDialog2ButtonTvTitle.text = "로그아웃 할까요?"
binding.componentDialog2ButtonTvFirstBtn.text = "취소"
binding.componentDialog2ButtonTvSecondBtn.text = "확인"
}

fun onDismiss() {
dismiss()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.c7z.mappilogue_aos.presentation.ui.main.home.today.adapter.TodayMarke
import com.c7z.mappilogue_aos.presentation.ui.main.home.today.adapter.TodayRecyclerViewAdapter
import com.c7z.mappilogue_aos.presentation.ui.main.home.today.viewmodel.TodayViewModel
import com.c7z.mappilogue_aos.presentation.util.DpToPxConverter
import com.c7z.mappilogue_aos.presentation.util.ItemDecorator

class TodayFragment: Fragment(){
private lateinit var binding: FragmentTodayBinding
Expand Down Expand Up @@ -66,26 +67,12 @@ class TodayFragment: Fragment(){

private fun initTodayRv() {
binding.todayRv.adapter = todayAdapter
binding.todayRv.addItemDecoration(VerticalItemDecorator(DpToPxConverter.dpToPx(16f, requireContext())))
binding.todayRv.addItemDecoration(ItemDecorator.VerticalItemDecorator(DpToPxConverter.dpToPx(16f, requireContext())))
}

private fun initTodayMarkedRv() {
binding.todayMarkedRv.setHasFixedSize(true)
binding.todayMarkedRv.adapter = todayMarkedAdapter
binding.todayMarkedRv.addItemDecoration(HorizontalItemDecorator(DpToPxConverter.dpToPx(14f, requireContext())))
}

inner class VerticalItemDecorator(private var spacing: Int): RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
if (parent.getChildAdapterPosition(view) != 0) outRect.top = spacing
}
}

inner class HorizontalItemDecorator(private var spacing: Int): RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
if (parent.getChildAdapterPosition(view) != 0) outRect.left = spacing
}
binding.todayMarkedRv.addItemDecoration(ItemDecorator.HorizontalItemDecorator(DpToPxConverter.dpToPx(14f, requireContext())))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.c7z.mappilogue_aos.presentation.ui.main.home.upcoming.adapter.Upcomin
import com.c7z.mappilogue_aos.presentation.ui.main.home.upcoming.adapter.UpcomingRecyclerViewAdapter
import com.c7z.mappilogue_aos.presentation.ui.main.home.upcoming.viewmodel.UpComingViewModel
import com.c7z.mappilogue_aos.presentation.util.DpToPxConverter
import com.c7z.mappilogue_aos.presentation.util.ItemDecorator

class UpcomingFragment: Fragment() {
private lateinit var binding: FragmentUpcomingBinding
Expand Down Expand Up @@ -70,30 +71,12 @@ class UpcomingFragment: Fragment() {
private fun initUpcomingRv() {
binding.upcomingRv.setHasFixedSize(true)
binding.upcomingRv.adapter = todayAdapter
binding.upcomingRv.addItemDecoration(VerticalItemDecorator(DpToPxConverter.dpToPx(16f, requireContext())))
binding.upcomingRv.addItemDecoration(ItemDecorator.VerticalItemDecorator(DpToPxConverter.dpToPx(16f, requireContext())))
}

private fun initUpcomingMarkedRv() {
binding.upcomingMarkedRv.setHasFixedSize(true)
binding.upcomingMarkedRv.adapter = todayMarkedAdapter
binding.upcomingMarkedRv.addItemDecoration(HorizontalItemDecorator(DpToPxConverter.dpToPx(14f, requireContext())))
}

inner class VerticalItemDecorator(private var spacing: Int): RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
if (parent.getChildAdapterPosition(view) != 0) outRect.top = spacing
}
}

inner class HorizontalItemDecorator(private var spacing: Int): RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
if (parent.getChildAdapterPosition(view) != 0) outRect.left = spacing
}
}

private fun dpToPx(dp: Float, context: Context): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics).toInt()
binding.upcomingMarkedRv.addItemDecoration(ItemDecorator.HorizontalItemDecorator(DpToPxConverter.dpToPx(14f, requireContext())))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,28 @@ import androidx.fragment.app.viewModels
import com.c7z.mappilogue_aos.R
import com.c7z.mappilogue_aos.databinding.FragmentMypageBinding
import com.c7z.mappilogue_aos.presentation.ui.change_profile.ChangeProfileActivity
import com.c7z.mappilogue_aos.presentation.ui.component.dialog.ComponentDialogTwoButton
import com.c7z.mappilogue_aos.presentation.ui.main.mypage.adapter.MyPageMenuAdapter
import com.c7z.mappilogue_aos.presentation.ui.main.mypage.viewmodel.MypageViewModel
import com.c7z.mappilogue_aos.presentation.ui.signin.SignInActivity
import com.c7z.mappilogue_aos.presentation.util.ItemDecorator
import com.kakao.sdk.user.UserApiClient
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MypageFragment : Fragment() {
private lateinit var binding : FragmentMypageBinding
private val viewModel: MypageViewModel by viewModels()

private val upperMenuAdapter by lazy { MyPageMenuAdapter(viewModel.upperMenuData(), ::onUpperMenuClicked) }
private val lowerMenuAdapter by lazy { MyPageMenuAdapter(viewModel.lowerMenuData(), ::onLowerMenuClicked) }
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_mypage, container, false)

initBinding()
initUi()
initObserve()

return binding.root
Expand All @@ -39,14 +46,67 @@ class MypageFragment : Fragment() {
binding.lifecycleOwner = this
}

private fun initUi() {
initUpperRv()
initLowerRv()
}

private fun initObserve() {
viewModel.userProfileData.observe(viewLifecycleOwner) {
Log.e("----", "initObserve: $it", )
observeLogOut()
}

private fun initUpperRv() {
binding.fgMyRvUpperMenu.apply {
adapter = upperMenuAdapter
addItemDecoration(ItemDecorator.MyPageItemDecorator(requireActivity()))
}
}

private fun initLowerRv() {
binding.fgMyRvLowerMenu.apply {
adapter = lowerMenuAdapter
addItemDecoration(ItemDecorator.MyPageItemDecorator(requireActivity()))
}
}

fun openChangeProfile() {
startActivity(Intent(requireActivity(), ChangeProfileActivity::class.java))
}

private fun onUpperMenuClicked(position : Int) {

}

private fun onLowerMenuClicked(position : Int) {
when(position) {
0 -> onLogoutClicked()
1 -> onSignOutClicked()
}
}

private fun onLogoutClicked() {
ComponentDialogTwoButton(::requestLogOut, "GREEN").show(requireActivity().supportFragmentManager, "LOG_OUT")
}

private fun requestLogOut() {
viewModel.requestLogOut()
}

private fun observeLogOut() {
viewModel.logOutStatus.observe(viewLifecycleOwner) {
if(it == 200) {
socialLogOut()
startActivity(Intent(requireActivity(), SignInActivity::class.java)).also { requireActivity().finish() }
}
}
}

private fun onSignOutClicked() {

}

private fun socialLogOut() {
UserApiClient.instance.logout { }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.c7z.mappilogue_aos.presentation.ui.main.mypage.adapter

import android.view.LayoutInflater
import android.view.MenuItem
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.c7z.mappilogue_aos.data.data.MyPageMenuItem
import com.c7z.mappilogue_aos.databinding.ItemRvMyPageMenuBinding

class MyPageMenuAdapter(
private val menuItems: List<MyPageMenuItem>,
private val onItemClicked: (Int) -> Unit
) : RecyclerView.Adapter<MyPageMenuAdapter.MyPageMenuViewHolder>() {
inner class MyPageMenuViewHolder(private val binding: ItemRvMyPageMenuBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: MyPageMenuItem) {
binding.data = item
binding.root.setOnClickListener { onItemClicked.invoke(absoluteAdapterPosition) }
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyPageMenuViewHolder {
return MyPageMenuViewHolder(
ItemRvMyPageMenuBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}

override fun getItemCount(): Int = menuItems.size

override fun onBindViewHolder(holder: MyPageMenuViewHolder, position: Int) {
holder.bind(menuItems[position])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.c7z.mappilogue_aos.R
import com.c7z.mappilogue_aos.data.data.MyPageMenuItem
import com.c7z.mappilogue_aos.data.remote.response.ResponseUserProfileData
import com.c7z.mappilogue_aos.domain.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -17,6 +18,9 @@ class MypageViewModel @Inject constructor(private val userRepository: UserReposi
private val _userProfileData = MutableLiveData<ResponseUserProfileData.ResultUserProfileData>()
val userProfileData : LiveData<ResponseUserProfileData.ResultUserProfileData> = _userProfileData

private val _logOutStatus = MutableLiveData<Int>()
val logOutStatus : LiveData<Int> = _logOutStatus

init {
requestUserProfile()
}
Expand All @@ -28,7 +32,26 @@ class MypageViewModel @Inject constructor(private val userRepository: UserReposi
}
}

fun requestLogOut() {
viewModelScope.launch {
userRepository.requestLogOut()
.onSuccess { _logOutStatus.value = it }
}
}

private fun ResponseUserProfileData.ResultUserProfileData.setImage() : ResponseUserProfileData.ResultUserProfileData {
return this.apply { this.profileImageUrl = R.drawable.ic_default_profile.toString() }
}

/** Dummy **/
fun upperMenuData() : List<MyPageMenuItem> = listOf(
MyPageMenuItem(R.drawable.ic_my_page_set_alarm, "알림 설정"),
MyPageMenuItem(R.drawable.ic_my_page_show_term, "이용약관"),
MyPageMenuItem(R.drawable.ic_my_page_send_report, "문의하기")
)

fun lowerMenuData() : List<MyPageMenuItem> = listOf(
MyPageMenuItem(R.drawable.ic_my_page_log_out, "로그아웃"),
MyPageMenuItem(R.drawable.ic_my_page_sign_out, "탈퇴하기")
)
}
Loading

0 comments on commit 5708f66

Please sign in to comment.