diff --git a/app/src/main/java/org/sopt/and/core/UserInfo.kt b/app/src/main/java/org/sopt/and/core/UserInfo.kt deleted file mode 100644 index a025ab0..0000000 --- a/app/src/main/java/org/sopt/and/core/UserInfo.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.sopt.and.core - -data class UserInfo ( - val email: String? = "" -) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/datasource/HobbyDataSource.kt b/app/src/main/java/org/sopt/and/core/data/datasource/HobbyDataSource.kt new file mode 100644 index 0000000..8ea0fd1 --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/datasource/HobbyDataSource.kt @@ -0,0 +1,10 @@ +package org.sopt.and.core.data.datasource + +import org.sopt.and.core.data.dto.BaseResponse +import org.sopt.and.core.data.dto.response.GetHobbyResponse + +interface HobbyDataSource { + suspend fun getHobby( + token: String + ): BaseResponse +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/datasource/SignInDataSource.kt b/app/src/main/java/org/sopt/and/core/data/datasource/SignInDataSource.kt new file mode 100644 index 0000000..13a5b07 --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/datasource/SignInDataSource.kt @@ -0,0 +1,11 @@ +package org.sopt.and.core.data.datasource + +import org.sopt.and.core.data.dto.BaseResponse +import org.sopt.and.core.data.dto.reqeust.LoginRequest +import org.sopt.and.core.data.dto.response.LoginResponse + +interface SignInDataSource { + suspend fun postSignIn( + request: LoginRequest + ): BaseResponse +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/datasource/SignUpDataSource.kt b/app/src/main/java/org/sopt/and/core/data/datasource/SignUpDataSource.kt new file mode 100644 index 0000000..b2be195 --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/datasource/SignUpDataSource.kt @@ -0,0 +1,11 @@ +package org.sopt.and.core.data.datasource + +import org.sopt.and.core.data.dto.BaseResponse +import org.sopt.and.core.data.dto.reqeust.CreateUserRequest +import org.sopt.and.core.data.dto.response.CreateUserResponse + +interface SignUpDataSource { + suspend fun postSignUp( + request: CreateUserRequest + ): BaseResponse +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/datasourceimpl/HobbyDataSourceImpl.kt b/app/src/main/java/org/sopt/and/core/data/datasourceimpl/HobbyDataSourceImpl.kt new file mode 100644 index 0000000..62b4533 --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/datasourceimpl/HobbyDataSourceImpl.kt @@ -0,0 +1,12 @@ +package org.sopt.and.core.data.datasourceimpl + +import org.sopt.and.core.data.datasource.HobbyDataSource +import org.sopt.and.core.data.dto.BaseResponse +import org.sopt.and.core.data.dto.response.GetHobbyResponse +import org.sopt.and.core.data.service.HobbyService + +class HobbyDataSourceImpl( + private val hobbyService: HobbyService +) : HobbyDataSource { + override suspend fun getHobby(token: String): BaseResponse = hobbyService.getMyHobby(token) +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/datasourceimpl/SignInDataSourceImpl.kt b/app/src/main/java/org/sopt/and/core/data/datasourceimpl/SignInDataSourceImpl.kt new file mode 100644 index 0000000..f8f40c2 --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/datasourceimpl/SignInDataSourceImpl.kt @@ -0,0 +1,14 @@ +package org.sopt.and.core.data.datasourceimpl + +import org.sopt.and.core.data.datasource.SignInDataSource +import org.sopt.and.core.data.dto.BaseResponse +import org.sopt.and.core.data.dto.reqeust.LoginRequest +import org.sopt.and.core.data.dto.response.LoginResponse +import org.sopt.and.core.data.service.SignInService + + +class SignInDataSourceImpl( + private val signInService: SignInService +) : SignInDataSource { + override suspend fun postSignIn(request: LoginRequest): BaseResponse = signInService.login(request) +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/datasourceimpl/SignUpDataSourceImpl.kt b/app/src/main/java/org/sopt/and/core/data/datasourceimpl/SignUpDataSourceImpl.kt new file mode 100644 index 0000000..2cf98ae --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/datasourceimpl/SignUpDataSourceImpl.kt @@ -0,0 +1,13 @@ +package org.sopt.and.core.data.datasourceimpl + +import org.sopt.and.core.data.datasource.SignUpDataSource +import org.sopt.and.core.data.dto.BaseResponse +import org.sopt.and.core.data.dto.reqeust.CreateUserRequest +import org.sopt.and.core.data.dto.response.CreateUserResponse +import org.sopt.and.core.data.service.SignUpService + +class SignUpDataSourceImpl( + private val signUpService: SignUpService +) : SignUpDataSource { + override suspend fun postSignUp(request: CreateUserRequest): BaseResponse = signUpService.signUp(request) +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/dto/BaseResponse.kt b/app/src/main/java/org/sopt/and/core/data/dto/BaseResponse.kt index f25cf73..827ab3b 100644 --- a/app/src/main/java/org/sopt/and/core/data/dto/BaseResponse.kt +++ b/app/src/main/java/org/sopt/and/core/data/dto/BaseResponse.kt @@ -1,10 +1,10 @@ package org.sopt.and.core.data.dto +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @Serializable data class BaseResponse ( - val status: Int, - val message: String, - val data: T? = null + @SerialName("result") + val result: T ) diff --git a/app/src/main/java/org/sopt/and/core/data/dto/response/CreateUserResponse.kt b/app/src/main/java/org/sopt/and/core/data/dto/response/CreateUserResponse.kt index d794158..1fb3e56 100644 --- a/app/src/main/java/org/sopt/and/core/data/dto/response/CreateUserResponse.kt +++ b/app/src/main/java/org/sopt/and/core/data/dto/response/CreateUserResponse.kt @@ -5,12 +5,6 @@ import kotlinx.serialization.Serializable @Serializable data class CreateUserResponse( - @SerialName("result") - val result: Result -) { - @Serializable - data class Result( - @SerialName("no") - val no: Int - ) -} \ No newline at end of file + @SerialName("no") + val userId: Int +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/dto/response/GetHobbyResponse.kt b/app/src/main/java/org/sopt/and/core/data/dto/response/GetHobbyResponse.kt index 8434f1a..d46d45b 100644 --- a/app/src/main/java/org/sopt/and/core/data/dto/response/GetHobbyResponse.kt +++ b/app/src/main/java/org/sopt/and/core/data/dto/response/GetHobbyResponse.kt @@ -5,12 +5,7 @@ import kotlinx.serialization.Serializable @Serializable data class GetHobbyResponse( - @SerialName("result") - val result: Result -) { - @Serializable - data class Result( - @SerialName("hobby") - val hobby: String - ) -} + @SerialName("hobby") + val hobby: String +) + diff --git a/app/src/main/java/org/sopt/and/core/data/dto/response/LoginResponse.kt b/app/src/main/java/org/sopt/and/core/data/dto/response/LoginResponse.kt index e4b3038..016c5d5 100644 --- a/app/src/main/java/org/sopt/and/core/data/dto/response/LoginResponse.kt +++ b/app/src/main/java/org/sopt/and/core/data/dto/response/LoginResponse.kt @@ -6,12 +6,8 @@ import kotlinx.serialization.Serializable @Serializable data class LoginResponse( - @SerialName("result") - val result: Result -) { - @Serializable - data class Result( - @SerialName("token") - val token: String - ) -} + @SerialName("token") + val token: String +) + + diff --git a/app/src/main/java/org/sopt/and/core/data/repositoryimpl/HobbyRepositoryImpl.kt b/app/src/main/java/org/sopt/and/core/data/repositoryimpl/HobbyRepositoryImpl.kt new file mode 100644 index 0000000..930ad5d --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/repositoryimpl/HobbyRepositoryImpl.kt @@ -0,0 +1,14 @@ +package org.sopt.and.core.data.repositoryimpl + +import org.sopt.and.core.data.datasource.HobbyDataSource +import org.sopt.and.core.data.dto.response.GetHobbyResponse +import org.sopt.and.domain.entity.HobbyData +import org.sopt.and.domain.entity.SignInData +import org.sopt.and.domain.repository.HobbyRepository + +class HobbyRepositoryImpl(private val hobbyDataSource: HobbyDataSource) : HobbyRepository { + override suspend fun getHobby(token: String): Result = runCatching { + val response = hobbyDataSource.getHobby(token) + HobbyData(response.result.hobby) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/repositoryimpl/SignInRepositoryImpl.kt b/app/src/main/java/org/sopt/and/core/data/repositoryimpl/SignInRepositoryImpl.kt new file mode 100644 index 0000000..5cba193 --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/repositoryimpl/SignInRepositoryImpl.kt @@ -0,0 +1,13 @@ +package org.sopt.and.core.data.repositoryimpl + +import org.sopt.and.core.data.datasource.SignInDataSource +import org.sopt.and.core.data.dto.reqeust.LoginRequest +import org.sopt.and.domain.entity.SignInData +import org.sopt.and.domain.repository.SignInRepository + +class SignInRepositoryImpl(private val signInDataSource: SignInDataSource) : SignInRepository { + override suspend fun signIn(username: String, password: String): Result = runCatching { + val response = signInDataSource.postSignIn(LoginRequest(username, password)) + SignInData(response.result.token) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/repositoryimpl/SignUpRepositoryImpl.kt b/app/src/main/java/org/sopt/and/core/data/repositoryimpl/SignUpRepositoryImpl.kt new file mode 100644 index 0000000..ff1c4ef --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/repositoryimpl/SignUpRepositoryImpl.kt @@ -0,0 +1,14 @@ +package org.sopt.and.core.data.repositoryimpl + +import org.sopt.and.core.data.datasource.SignUpDataSource +import org.sopt.and.core.data.dto.reqeust.CreateUserRequest +import org.sopt.and.core.data.dto.response.CreateUserResponse +import org.sopt.and.domain.entity.SignUpData +import org.sopt.and.domain.repository.SignUpRepository + +class SignUpRepositoryImpl(private val signUpDataSource: SignUpDataSource) : SignUpRepository { + override suspend fun signUp(username: String, password: String, hobby: String): Result = runCatching { + val response = signUpDataSource.postSignUp(CreateUserRequest(username, password, hobby)) + SignUpData(response.result.userId) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/service/HobbyService.kt b/app/src/main/java/org/sopt/and/core/data/service/HobbyService.kt new file mode 100644 index 0000000..58e6507 --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/service/HobbyService.kt @@ -0,0 +1,13 @@ +package org.sopt.and.core.data.service + +import org.sopt.and.core.data.dto.BaseResponse +import org.sopt.and.core.data.dto.response.GetHobbyResponse +import retrofit2.http.GET +import retrofit2.http.Header + +interface HobbyService { + @GET("user/my-hobby") + suspend fun getMyHobby( + @Header("token") token: String + ): BaseResponse +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/service/ServicePool.kt b/app/src/main/java/org/sopt/and/core/data/service/ServicePool.kt new file mode 100644 index 0000000..e6b408a --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/service/ServicePool.kt @@ -0,0 +1,9 @@ +package org.sopt.and.core.data.service + +import org.sopt.and.network.RetrofitInstance + +object ServicePool { + val signInService: SignInService = RetrofitInstance.create() + val signUpService: SignUpService = RetrofitInstance.create() + val hobbyService: HobbyService = RetrofitInstance.create() +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/service/SignInService.kt b/app/src/main/java/org/sopt/and/core/data/service/SignInService.kt new file mode 100644 index 0000000..2578996 --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/service/SignInService.kt @@ -0,0 +1,14 @@ +package org.sopt.and.core.data.service + +import org.sopt.and.core.data.dto.BaseResponse +import org.sopt.and.core.data.dto.reqeust.LoginRequest +import org.sopt.and.core.data.dto.response.LoginResponse +import retrofit2.http.Body +import retrofit2.http.POST + +interface SignInService { + @POST("login") + suspend fun login( + @Body request: LoginRequest + ): BaseResponse +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/data/service/SignUpService.kt b/app/src/main/java/org/sopt/and/core/data/service/SignUpService.kt new file mode 100644 index 0000000..d97e51b --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/data/service/SignUpService.kt @@ -0,0 +1,14 @@ +package org.sopt.and.core.data.service + +import org.sopt.and.core.data.dto.BaseResponse +import org.sopt.and.core.data.dto.reqeust.CreateUserRequest +import org.sopt.and.core.data.dto.response.CreateUserResponse +import retrofit2.http.Body +import retrofit2.http.POST + +interface SignUpService { + @POST("user") + suspend fun signUp( + @Body request: CreateUserRequest + ): BaseResponse +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/core/util/DefaultErrorHandler.kt b/app/src/main/java/org/sopt/and/core/util/DefaultErrorHandler.kt new file mode 100644 index 0000000..9060add --- /dev/null +++ b/app/src/main/java/org/sopt/and/core/util/DefaultErrorHandler.kt @@ -0,0 +1,23 @@ +package org.sopt.and.core.util + +import android.content.Context +import retrofit2.HttpException +import org.sopt.and.R + +interface ErrorHandler { + fun handleNetworkError(exception: Throwable?): String +} + +class DefaultErrorHandler(private val context: Context) : ErrorHandler { + override fun handleNetworkError(exception: Throwable?): String { + return when (exception) { + is HttpException -> when (exception.code()) { + 400 -> context.getString(R.string.network_error_400) + 403 -> context.getString(R.string.network_error_403) + 409 -> context.getString(R.string.network_error_409) + else -> context.getString(R.string.network_error) + } + else -> context.getString(R.string.network_error) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/entity/HobbyData.kt b/app/src/main/java/org/sopt/and/domain/entity/HobbyData.kt new file mode 100644 index 0000000..0852df5 --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/entity/HobbyData.kt @@ -0,0 +1,5 @@ +package org.sopt.and.domain.entity + +data class HobbyData( + val hobby: String +) diff --git a/app/src/main/java/org/sopt/and/domain/entity/SignInData.kt b/app/src/main/java/org/sopt/and/domain/entity/SignInData.kt new file mode 100644 index 0000000..19b44e7 --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/entity/SignInData.kt @@ -0,0 +1,6 @@ +package org.sopt.and.domain.entity + +data class SignInData( + val token: String +) + diff --git a/app/src/main/java/org/sopt/and/domain/entity/SignUpData.kt b/app/src/main/java/org/sopt/and/domain/entity/SignUpData.kt new file mode 100644 index 0000000..d05c537 --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/entity/SignUpData.kt @@ -0,0 +1,5 @@ +package org.sopt.and.domain.entity + +data class SignUpData( + val userId: Int +) diff --git a/app/src/main/java/org/sopt/and/domain/repository/HobbyRepository.kt b/app/src/main/java/org/sopt/and/domain/repository/HobbyRepository.kt new file mode 100644 index 0000000..80981d5 --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/repository/HobbyRepository.kt @@ -0,0 +1,8 @@ +package org.sopt.and.domain.repository + +import org.sopt.and.core.data.dto.response.GetHobbyResponse +import org.sopt.and.domain.entity.HobbyData + +interface HobbyRepository { + suspend fun getHobby(token: String): Result +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/repository/RepositoryPool.kt b/app/src/main/java/org/sopt/and/domain/repository/RepositoryPool.kt new file mode 100644 index 0000000..020898a --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/repository/RepositoryPool.kt @@ -0,0 +1,23 @@ +package org.sopt.and.domain.repository + +import org.sopt.and.core.data.datasourceimpl.HobbyDataSourceImpl +import org.sopt.and.core.data.datasourceimpl.SignInDataSourceImpl +import org.sopt.and.core.data.datasourceimpl.SignUpDataSourceImpl +import org.sopt.and.core.data.repositoryimpl.HobbyRepositoryImpl +import org.sopt.and.core.data.repositoryimpl.SignInRepositoryImpl +import org.sopt.and.core.data.repositoryimpl.SignUpRepositoryImpl +import org.sopt.and.core.data.service.ServicePool + +object RepositoryPool { + val hobbyRepository: HobbyRepository by lazy { + HobbyRepositoryImpl(HobbyDataSourceImpl(ServicePool.hobbyService)) + } + + val signInRepository: SignInRepository by lazy { + SignInRepositoryImpl(SignInDataSourceImpl(ServicePool.signInService)) + } + + val signUpRepository: SignUpRepository by lazy { + SignUpRepositoryImpl(SignUpDataSourceImpl(ServicePool.signUpService)) + } +} diff --git a/app/src/main/java/org/sopt/and/domain/repository/SignInRepository.kt b/app/src/main/java/org/sopt/and/domain/repository/SignInRepository.kt new file mode 100644 index 0000000..eb74384 --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/repository/SignInRepository.kt @@ -0,0 +1,7 @@ +package org.sopt.and.domain.repository + +import org.sopt.and.domain.entity.SignInData + +interface SignInRepository { + suspend fun signIn(username: String, password: String): Result +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/domain/repository/SignUpRepository.kt b/app/src/main/java/org/sopt/and/domain/repository/SignUpRepository.kt new file mode 100644 index 0000000..7fad5e2 --- /dev/null +++ b/app/src/main/java/org/sopt/and/domain/repository/SignUpRepository.kt @@ -0,0 +1,8 @@ +package org.sopt.and.domain.repository + +import org.sopt.and.core.data.dto.response.CreateUserResponse +import org.sopt.and.domain.entity.SignUpData + +interface SignUpRepository { + suspend fun signUp(username: String, password: String, hobby: String): Result +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/navigation/NavGraph.kt b/app/src/main/java/org/sopt/and/navigation/NavGraph.kt index dbfcc94..c2b504d 100644 --- a/app/src/main/java/org/sopt/and/navigation/NavGraph.kt +++ b/app/src/main/java/org/sopt/and/navigation/NavGraph.kt @@ -5,7 +5,6 @@ import androidx.compose.ui.Modifier import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable -import org.sopt.and.core.UserInfo import org.sopt.and.presentation.home.HomeScreen import org.sopt.and.presentation.mypage.MyPageScreen import org.sopt.and.presentation.search.SearchScreen @@ -22,7 +21,7 @@ sealed class Screen(val route: String) { } @Composable -fun NavGraph(navController: NavHostController, userInfo: UserInfo) { +fun NavGraph(navController: NavHostController) { NavHost( navController = navController, startDestination = Screen.SignIn.route diff --git a/app/src/main/java/org/sopt/and/network/AuthService.kt b/app/src/main/java/org/sopt/and/network/AuthService.kt deleted file mode 100644 index f78ed50..0000000 --- a/app/src/main/java/org/sopt/and/network/AuthService.kt +++ /dev/null @@ -1,28 +0,0 @@ -package org.sopt.and.network - -import org.sopt.and.core.data.dto.reqeust.CreateUserRequest -import org.sopt.and.core.data.dto.reqeust.LoginRequest -import org.sopt.and.core.data.dto.response.CreateUserResponse -import org.sopt.and.core.data.dto.response.GetHobbyResponse -import org.sopt.and.core.data.dto.response.LoginResponse -import retrofit2.http.Body -import retrofit2.http.GET -import retrofit2.http.Header -import retrofit2.http.POST - -interface AuthService { - @POST("user") - suspend fun signUp( - @Body request: CreateUserRequest - ): CreateUserResponse - - @POST("login") - suspend fun login( - @Body request: LoginRequest - ): LoginResponse - - @GET("user/my-hobby") - suspend fun getMyHobby( - @Header("token") token: String - ): GetHobbyResponse -} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/network/RetrofitInstance.kt b/app/src/main/java/org/sopt/and/network/RetrofitInstance.kt index 09521dd..136fb52 100644 --- a/app/src/main/java/org/sopt/and/network/RetrofitInstance.kt +++ b/app/src/main/java/org/sopt/and/network/RetrofitInstance.kt @@ -33,6 +33,3 @@ object RetrofitInstance { } -object ServicePool { - val authService = RetrofitInstance.create() -} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/presentation/MainActivity.kt b/app/src/main/java/org/sopt/and/presentation/MainActivity.kt index 8e17af1..e1c4787 100644 --- a/app/src/main/java/org/sopt/and/presentation/MainActivity.kt +++ b/app/src/main/java/org/sopt/and/presentation/MainActivity.kt @@ -16,7 +16,6 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.rememberNavController import org.sopt.and.R -import org.sopt.and.core.UserInfo import org.sopt.and.navigation.NavGraph import org.sopt.and.navigation.Screen import org.sopt.and.presentation.common.BottomNavigationBar @@ -59,8 +58,7 @@ fun MainScreen(userEmail: String?) { ) { paddingValues -> Box(modifier = Modifier.padding(paddingValues)) { NavGraph( - navController = navController, - userInfo = UserInfo(userEmail) + navController = navController ) } } diff --git a/app/src/main/java/org/sopt/and/presentation/auth/AuthViewModel.kt b/app/src/main/java/org/sopt/and/presentation/auth/AuthViewModel.kt index 3797e36..99cf639 100644 --- a/app/src/main/java/org/sopt/and/presentation/auth/AuthViewModel.kt +++ b/app/src/main/java/org/sopt/and/presentation/auth/AuthViewModel.kt @@ -2,16 +2,15 @@ package org.sopt.and.presentation.auth import android.app.Application import android.content.Context -import android.util.Log import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import kotlinx.coroutines.launch import org.sopt.and.R -import org.sopt.and.core.data.dto.reqeust.CreateUserRequest -import org.sopt.and.core.data.dto.reqeust.LoginRequest -import org.sopt.and.network.ServicePool.authService +import org.sopt.and.core.util.DefaultErrorHandler +import org.sopt.and.domain.entity.SignInData +import org.sopt.and.domain.repository.RepositoryPool import org.sopt.and.utils.KeyStorage import retrofit2.HttpException @@ -19,6 +18,8 @@ class AuthViewModel(application: Application) : AndroidViewModel(application) { private val context = getApplication().applicationContext + private val errorHandler = DefaultErrorHandler(context) + private val _username = MutableLiveData("") val username: LiveData get() = _username @@ -32,6 +33,9 @@ class AuthViewModel(application: Application) : AndroidViewModel(application) { val signUpSuccess = MutableLiveData(false) val errorMessage = MutableLiveData("") + private val signInRepository = RepositoryPool.signInRepository + private val signUpRepository = RepositoryPool.signUpRepository + fun setUsername(username: String) { _username.value = username } @@ -46,30 +50,18 @@ class AuthViewModel(application: Application) : AndroidViewModel(application) { fun onSignInClick(onSignInSuccess: () -> Unit) { viewModelScope.launch { - try { - val response = authService.login( - LoginRequest( - username = _username.value ?: "", - password = _password.value ?: "" - ) - ) + val result = signInRepository.signIn(_username.value.orEmpty(), _password.value.orEmpty()) + + result.onSuccess { signInData -> context.getSharedPreferences("auth", Context.MODE_PRIVATE).edit() - .putString("token", response.result.token) + .putString("token", signInData.token.orEmpty()) .apply() signInSuccess.value = true errorMessage.value = context.getString(R.string.signin_success) onSignInSuccess() - } catch (e: HttpException) { - when (e.code()) { - 400 -> errorMessage.value = context.getString(R.string.network_error_400) - 403 -> errorMessage.value = context.getString(R.string.network_error_403) - else -> errorMessage.value = context.getString(R.string.signin_fail) - } - signInSuccess.value = false - } catch (e: Exception) { - errorMessage.value = context.getString(R.string.network_error) - signInSuccess.value = false + }.onFailure{ + handleSignInError(result.exceptionOrNull()) } } } @@ -81,33 +73,23 @@ class AuthViewModel(application: Application) : AndroidViewModel(application) { return@launch } - if(!isValidPassword(_password.value ?: "")){ + if (!isValidPassword(_password.value ?: "")) { errorMessage.value = context.getString(R.string.sign_up_error_password_form) return@launch } - try { - val response = authService.signUp( - CreateUserRequest( - username = _username.value.orEmpty(), - password = _password.value.orEmpty(), - hobby = _hobby.value.orEmpty() - ) - ) + val result = signUpRepository.signUp( + _username.value.orEmpty(), + _password.value.orEmpty(), + _hobby.value.orEmpty() + ) + + if (result.isSuccess) { signUpSuccess.value = true errorMessage.value = context.getString(R.string.signup_success) onSignUpSuccess() - } catch (e: HttpException) { - when (e.code()) { - 400 -> errorMessage.value = context.getString(R.string.network_error_400) - 409 -> errorMessage.value = context.getString(R.string.network_error_409) - else -> errorMessage.value = context.getString(R.string.signup_fail) - } - signUpSuccess.value = false - } catch (e: Exception) { - Log.e("SignUpError", "Exception occurred: ${e.message}", e) - errorMessage.value = context.getString(R.string.network_error) - signUpSuccess.value = false + } else { + handleSignUpError(result.exceptionOrNull()) } } } @@ -121,5 +103,15 @@ class AuthViewModel(application: Application) : AndroidViewModel(application) { private fun isValidPassword(password: String): Boolean { return Regex(KeyStorage.EMAIL_REGEX).matches(password) } + + private fun handleSignUpError(exception: Throwable?) { + errorMessage.value = errorHandler.handleNetworkError(exception) + signUpSuccess.value = false + } + + private fun handleSignInError(exception: Throwable?) { + errorMessage.value = errorHandler.handleNetworkError(exception) + signInSuccess.value = false + } } diff --git a/app/src/main/java/org/sopt/and/presentation/auth/SignInData.kt b/app/src/main/java/org/sopt/and/presentation/auth/SignInData.kt deleted file mode 100644 index 49166e7..0000000 --- a/app/src/main/java/org/sopt/and/presentation/auth/SignInData.kt +++ /dev/null @@ -1,6 +0,0 @@ -package org.sopt.and.presentation.auth - -data class LoginResult( - val token: String -) - diff --git a/app/src/main/java/org/sopt/and/presentation/auth/SignUpData.kt b/app/src/main/java/org/sopt/and/presentation/auth/SignUpData.kt deleted file mode 100644 index 5469742..0000000 --- a/app/src/main/java/org/sopt/and/presentation/auth/SignUpData.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.sopt.and.presentation.auth - -data class SignUpResult( - val no: Int -) diff --git a/app/src/main/java/org/sopt/and/presentation/home/HomeScreen.kt b/app/src/main/java/org/sopt/and/presentation/home/HomeScreen.kt index 36ea420..373d6e1 100644 --- a/app/src/main/java/org/sopt/and/presentation/home/HomeScreen.kt +++ b/app/src/main/java/org/sopt/and/presentation/home/HomeScreen.kt @@ -132,7 +132,7 @@ fun HomeScreen( items(uiState.editorPicks) { content -> ImageWithTextCard( imageRes = content.imageRes, - title = "Image${content.title}", + title = context.getString(content.title), modifier = Modifier ) } diff --git a/app/src/main/java/org/sopt/and/presentation/mypage/HobbyData.kt b/app/src/main/java/org/sopt/and/presentation/mypage/HobbyData.kt deleted file mode 100644 index 0f813b2..0000000 --- a/app/src/main/java/org/sopt/and/presentation/mypage/HobbyData.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.sopt.and.presentation.mypage - -data class HobbyResult( - val hobby: String -) diff --git a/app/src/main/java/org/sopt/and/presentation/mypage/MyPageScreen.kt b/app/src/main/java/org/sopt/and/presentation/mypage/MyPageScreen.kt index b1db287..197ecb3 100644 --- a/app/src/main/java/org/sopt/and/presentation/mypage/MyPageScreen.kt +++ b/app/src/main/java/org/sopt/and/presentation/mypage/MyPageScreen.kt @@ -39,7 +39,6 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import org.sopt.and.R -import org.sopt.and.core.UserInfo import androidx.lifecycle.viewmodel.compose.viewModel import org.sopt.and.ui.theme.ANDANDROIDTheme diff --git a/app/src/main/java/org/sopt/and/presentation/mypage/MyPageViewModel.kt b/app/src/main/java/org/sopt/and/presentation/mypage/MyPageViewModel.kt index d6874b2..0428f85 100644 --- a/app/src/main/java/org/sopt/and/presentation/mypage/MyPageViewModel.kt +++ b/app/src/main/java/org/sopt/and/presentation/mypage/MyPageViewModel.kt @@ -1,5 +1,6 @@ package org.sopt.and.presentation.mypage +import android.app.Application import android.content.Context import android.util.Log import androidx.lifecycle.ViewModel @@ -7,51 +8,34 @@ import androidx.lifecycle.viewModelScope import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch -import org.sopt.and.network.RetrofitInstance -import org.sopt.and.network.ServicePool.authService +import org.sopt.and.core.util.DefaultErrorHandler +import org.sopt.and.domain.repository.RepositoryPool import retrofit2.HttpException import java.io.IOException class MyPageViewModel : ViewModel() { + + private val _uiState = MutableStateFlow(MyPageUiState()) val uiState = _uiState.asStateFlow() private val _errorMessage = MutableStateFlow(null) val errorMessage = _errorMessage.asStateFlow() + private val hobbyRepository = RepositoryPool.hobbyRepository + fun fetchMyHobby(context: Context) { + val errorHandler = DefaultErrorHandler(context) viewModelScope.launch { - try { - val token = context.getSharedPreferences("auth", Context.MODE_PRIVATE) - .getString("token", "") ?: "" - - val response = authService.getMyHobby(token) - _uiState.value = MyPageUiState(hobby = response.result.hobby) - } catch (e: HttpException) { - when (e.code()) { - 401 -> { - _errorMessage.value = "인증 토큰이 없습니다" - Log.e("MyPage", "Authentication error: Missing token", e) - } - 403 -> { - _errorMessage.value = "유효하지 않은 토큰입니다" - Log.e("MyPage", "Authentication error: Invalid token", e) - } - 404 -> { - _errorMessage.value = "유효하지 못한 요청입니다" - Log.e("MyPage", "Not found error", e) - } - else -> { - _errorMessage.value = "서버 오류가 발생했습니다" - Log.e("MyPage", "Server error: ${e.code()}", e) - } - } - } catch (e: IOException) { - _errorMessage.value = "네트워크 연결을 확인해주세요" - Log.e("MyPage", "Network error", e) - } catch (e: Exception) { - _errorMessage.value = "알 수 없는 오류가 발생했습니다" - Log.e("MyPage", "Unexpected error", e) + val token = context.getSharedPreferences("auth", Context.MODE_PRIVATE) + .getString("token", "") ?: "" + + val result = hobbyRepository.getHobby(token) + + result.onSuccess { hobbyData -> + _uiState.value = MyPageUiState(hobby = hobbyData.hobby) + }.onFailure { e -> + _errorMessage.value = errorHandler.handleNetworkError(e) } } }