Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
πŸšͺ 카카였 및 μ„œλ²„ νšŒμ›νƒˆν‡΄ κΈ°λŠ₯ κ΅¬ν˜„ (#82)
Browse files Browse the repository at this point in the history
* [feat] homeν™”λ©΄ κ°ˆλ•Œ 선택할 λ‚ μ§œ μ΄ˆκΈ°ν™”

* [feat] Home ν™”λ©΄ 일기가 없을 μ‹œ λ ˆμ΄μ•„μ›ƒ μˆ˜μ •

* [style] signIn, mypage 색상 μˆ˜μ •

* [feat] νšŒμ› νƒˆν‡΄ κΈ°λŠ₯ μž‘μ—…

* [style] home ν™”λ©΄ μˆ˜μ •

* [feat] splash ν™”λ©΄ 생성

* [feat] Splash ν™”λ©΄  μž¬μ„€μ •

* [feat] νšŒμ›νƒˆν‡΄ μ‹œ dataStore μ‚­μ œ
  • Loading branch information
HamBeomJoon authored Aug 13, 2024
1 parent a117758 commit 5e6733b
Show file tree
Hide file tree
Showing 21 changed files with 326 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.nabi.data.model.BaseResponse
import com.nabi.data.model.auth.NicknameResponseDTO
import com.nabi.data.model.auth.SignInRequestDTO
import com.nabi.data.model.auth.SignInResponseDTO
import com.nabi.data.model.auth.WithdrawResponseDTO

interface AuthRemoteDataSource {
suspend fun signIn(
Expand All @@ -14,4 +15,8 @@ interface AuthRemoteDataSource {
accessToken: String,
nickname: String
): Result<BaseResponse<NicknameResponseDTO>>

suspend fun withdraw(
accessToken: String
): Result<BaseResponse<WithdrawResponseDTO>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.nabi.data.model.BaseResponse
import com.nabi.data.model.auth.NicknameResponseDTO
import com.nabi.data.model.auth.SignInRequestDTO
import com.nabi.data.model.auth.SignInResponseDTO
import com.nabi.data.model.auth.WithdrawResponseDTO
import com.nabi.data.service.AuthService
import javax.inject.Inject

Expand Down Expand Up @@ -47,4 +48,22 @@ class AuthRemoteDataSourceImpl @Inject constructor(
Result.failure(e)
}
}

override suspend fun withdraw(accessToken: String): Result<BaseResponse<WithdrawResponseDTO>> {
return try {
val response = authService.withdraw(accessToken)
if (response.isSuccessful) {
val authResponse = response.body()
if (authResponse != null) {
Result.success(authResponse)
} else {
Result.failure(Exception("Withdraw failed: response body is null"))
}
} else {
Result.failure(Exception("Withdraw failed: ${response.message()}"))
}
} catch (e: Exception) {
Result.failure(e)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.nabi.data.model.auth


import com.google.gson.annotations.SerializedName

data class WithdrawResponseDTO(
@SerializedName("message")
val message: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,24 @@ class AuthRepositoryImpl @Inject constructor(
Result.failure(result.exceptionOrNull() ?: Exception("Unknown error"))
}
}

override suspend fun withdraw(accessToken: String): Result<String> {
val result = authRemoteDataSource.withdraw(accessToken)

return if (result.isSuccess) {
val res = result.getOrNull()
if(res != null){
val data = res.data
if(data != null){
Result.success(data.message)
} else {
Result.failure(Exception("Withdraw Failed: data is null"))
}
} else {
Result.failure(Exception("Withdraw Failed: response body is null"))
}
} else {
Result.failure(result.exceptionOrNull() ?: Exception("Unknown error"))
}
}
}
7 changes: 7 additions & 0 deletions Nabi/data/src/main/java/com/nabi/data/service/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import com.nabi.data.model.BaseResponse
import com.nabi.data.model.auth.NicknameResponseDTO
import com.nabi.data.model.auth.SignInRequestDTO
import com.nabi.data.model.auth.SignInResponseDTO
import com.nabi.data.model.auth.WithdrawResponseDTO
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.Header
import retrofit2.http.POST
import retrofit2.http.Query
Expand All @@ -22,4 +24,9 @@ interface AuthService {
@Header("Authorization") accessToken: String,
@Query("nickname") nickname: String
): Response<BaseResponse<NicknameResponseDTO>>

@DELETE("/auth")
suspend fun withdraw(
@Header("Authorization") accessToken: String
): Response<BaseResponse<WithdrawResponseDTO>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ interface AuthRepository {
suspend fun signIn(idToken: String, provider: AuthProvider): Result<SignInInfo>

suspend fun setNickname(accessToken: String, nickname: String): Result<NicknameInfo>

suspend fun withdraw(accessToken: String): Result<String>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.nabi.domain.usecase.auth

import com.nabi.domain.repository.AuthRepository

class WithdrawUseCase(private val repository: AuthRepository) {

suspend operator fun invoke(accessToken: String): Result<String> {
return repository.withdraw("Bearer $accessToken")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.nabi.domain.usecase.datastore

import com.nabi.domain.model.auth.NicknameInfo
import com.nabi.domain.repository.AuthRepository
import com.nabi.domain.repository.DataStoreRepository

class ClearUserDataUseCase(private val repository: DataStoreRepository) {

suspend operator fun invoke(): Result<Boolean> {
return repository.clearUserData()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.nabi.nabi.di
import com.nabi.domain.repository.AuthRepository
import com.nabi.domain.usecase.auth.SetNicknameUseCase
import com.nabi.domain.usecase.auth.SignInUseCase
import com.nabi.domain.usecase.auth.WithdrawUseCase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -28,4 +29,12 @@ object AuthUseCaseModule {
): SetNicknameUseCase {
return SetNicknameUseCase(repository = repository)
}

@Provides
@Singleton
fun provideWithdrawUseCase(
repository: AuthRepository
): WithdrawUseCase {
return WithdrawUseCase(repository = repository)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nabi.nabi.di

import com.nabi.domain.repository.DataStoreRepository
import com.nabi.domain.usecase.datastore.ClearUserDataUseCase
import com.nabi.domain.usecase.datastore.GetAccessTokenUseCase
import com.nabi.domain.usecase.datastore.GetAuthProviderUseCase
import com.nabi.domain.usecase.datastore.GetRecentAuthProviderUseCase
Expand Down Expand Up @@ -46,4 +47,12 @@ object DataStoreUseCaseModule {
): SaveSignInInfoUseCase {
return SaveSignInInfoUseCase(repository = repository)
}

@Provides
@Singleton
fun provideClearUserDataUseCase(
repository: DataStoreRepository
): ClearUserDataUseCase {
return ClearUserDataUseCase(repository = repository)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class SharedDateViewModel : ViewModel() {
private val _selectedDate = MutableLiveData("")
val selectedDate: LiveData<String> get() = _selectedDate

private val _date = MutableLiveData<DiarySelectInfo>()
val date: LiveData<DiarySelectInfo> get() = _date
private val _date = MutableLiveData<DiarySelectInfo?>()
val date: LiveData<DiarySelectInfo?> get() = _date

fun changeSelectedDate(date: String) {
_selectedDate.value = date
Expand All @@ -23,5 +23,6 @@ class SharedDateViewModel : ViewModel() {

fun clearData() {
_selectedDate.value = ""
_date.value = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home) {
showToast(it.message)
LoggerUtils.e("λ©”μΈνŽ˜μ΄μ§€ 데이터 뢈러였기 μ‹€νŒ¨: ${it.message}")
binding.rvDiary.visibility = View.GONE
binding.tvEmptyDiary.visibility = View.VISIBLE
}

is UiState.Success -> {
val diaryList = it.data.recentFiveDiaries
if (diaryList.isEmpty()) {
binding.rvDiary.visibility = View.GONE
binding.tvEmptyDiary.visibility = View.VISIBLE
} else {
homeRvAdapter.setData(diaryList)
consecutiveDay = it.data.consecutiveWritingDays
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.nabi.nabi.views.myPage

import android.annotation.SuppressLint
import android.content.Intent
import androidx.datastore.dataStore
import androidx.fragment.app.viewModels
import com.nabi.nabi.R
import com.nabi.nabi.base.BaseFragment
import com.nabi.nabi.base.NabiApplication.Companion.consecutiveDay
import com.nabi.nabi.base.NabiApplication.Companion.nickname
import com.nabi.nabi.custom.CustomDialog
import com.nabi.nabi.databinding.FragmentMypageBinding
import com.nabi.nabi.utils.LoggerUtils
import com.nabi.nabi.utils.UiState
import com.nabi.nabi.views.MainActivity
import com.nabi.nabi.views.home.HomeFragment
import com.nabi.nabi.views.sign.SignActivity
import com.nabi.nabi.views.sign.SignInNicknameFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MyPageFragment : BaseFragment<FragmentMypageBinding>(R.layout.fragment_mypage) {
private val myPageViewModel: MyPageViewModel by viewModels()

@SuppressLint("SetTextI18n")
override fun initView() {
Expand All @@ -33,11 +41,11 @@ class MyPageFragment : BaseFragment<FragmentMypageBinding>(R.layout.fragment_myp
}

binding.btnWithdraw.setOnClickListener {
val withdrawDialog = CustomDialog.getInstance(CustomDialog.DialogType.UNLINK)
val withdrawDialog = CustomDialog.getInstance(CustomDialog.DialogType.UNLINK, nickname)

withdrawDialog.setButtonClickListener(object : CustomDialog.OnButtonClickListener {
override fun onButton1Clicked() {

myPageViewModel.withdraw()
}

override fun onButton2Clicked() {
Expand All @@ -48,4 +56,37 @@ class MyPageFragment : BaseFragment<FragmentMypageBinding>(R.layout.fragment_myp
withdrawDialog.show(parentFragmentManager, "WithdrawDialog")
}
}

override fun setObserver() {
super.setObserver()
myPageViewModel.withdrawState.observe(viewLifecycleOwner) {
when (it) {
is UiState.Loading -> {}
is UiState.Failure -> {
showToast("νšŒμ› νƒˆν‡΄ μ‹€νŒ¨")
}

is UiState.Success -> {
myPageViewModel.clearData()
}
}
}

myPageViewModel.clearState.observe(viewLifecycleOwner)
{
when (it) {
is UiState.Loading -> {}
is UiState.Failure -> {
showToast("νšŒμ› 정보 μ‚­μ œ μ‹€νŒ¨")
}

is UiState.Success -> {
requireActivity().apply {
startActivity(Intent(this, SignActivity::class.java))
finish()
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.nabi.nabi.views.myPage

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.nabi.domain.usecase.auth.WithdrawUseCase
import com.nabi.domain.usecase.datastore.ClearUserDataUseCase
import com.nabi.domain.usecase.datastore.GetAccessTokenUseCase
import com.nabi.nabi.utils.LoggerUtils
import com.nabi.nabi.utils.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MyPageViewModel @Inject constructor(
private val withdrawUseCase: WithdrawUseCase,
private val clearUserDataUseCase: ClearUserDataUseCase,
private val getAccessTokenUseCase: GetAccessTokenUseCase
) : ViewModel() {

private val _withdrawState = MutableLiveData<UiState<String>>(UiState.Loading)
val withdrawState: LiveData<UiState<String>> get() = _withdrawState

private val _clearState = MutableLiveData<UiState<Boolean>>(UiState.Loading)
val clearState: LiveData<UiState<Boolean>> get() = _clearState


fun withdraw() {
_withdrawState.value = UiState.Loading

viewModelScope.launch {
val accessToken = getAccessTokenUseCase.invoke().getOrNull().orEmpty()

try {
withdrawUseCase(accessToken)
.onSuccess {
_withdrawState.value = UiState.Success(it)
}
.onFailure { e ->
LoggerUtils.e("Withdraw failed: ${e.message}")
_withdrawState.value = UiState.Failure(message = e.message.toString())
}
} catch (e: Exception) {
LoggerUtils.e("Withdraw exception: ${e.message}")
_withdrawState.value = UiState.Failure(message = e.message.toString())
}
}
}

fun clearData() {
_clearState.value = UiState.Loading

viewModelScope.launch {
try {
clearUserDataUseCase()
.onSuccess {
_clearState.value = UiState.Success(it)
}
.onFailure { e ->
LoggerUtils.e("Clear User Data failed: ${e.message}")
_clearState.value = UiState.Failure(message = e.message.toString())
}
} catch (e: Exception) {
LoggerUtils.e("Clear User Data: ${e.message}")
_clearState.value = UiState.Failure(message = e.message.toString())
}
}
}
}
Loading

0 comments on commit 5e6733b

Please sign in to comment.