-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/week xml07 #19
base: develop-xml
Are you sure you want to change the base?
Feat/week xml07 #19
Changes from 3 commits
edb66a8
d4bc82f
3ef6911
d97ecc0
b9cc67b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.sopt.now.data | ||
|
||
import com.sopt.now.datasource.AuthService | ||
import com.sopt.now.model.login.RequestLoginDto | ||
import com.sopt.now.model.signup.RequestSignUpDto | ||
import com.sopt.now.repository.AuthRepository | ||
import com.sopt.now.utils.BaseResponse | ||
import retrofit2.Response | ||
|
||
class AuthRepositoryImpl( | ||
private val authService: AuthService | ||
): AuthRepository { | ||
override suspend fun loginUser(data: RequestLoginDto): Response<BaseResponse<Unit>> { | ||
return authService.login(data) | ||
} | ||
|
||
override suspend fun signupUser(data: RequestSignUpDto): Response<BaseResponse<Unit>> { | ||
return authService.signUp(data) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.sopt.now.data | ||
|
||
import com.sopt.now.datasource.InfoService | ||
import com.sopt.now.model.info.UserInfo | ||
import com.sopt.now.repository.MyPageRepository | ||
import com.sopt.now.utils.BaseResponse | ||
import retrofit2.Response | ||
|
||
class MyPageRepositoryImpl( | ||
private val infoService: InfoService | ||
) : MyPageRepository { | ||
override suspend fun getUserInfo(): Response<BaseResponse<UserInfo>> { | ||
return infoService.getUserInfo() | ||
} | ||
|
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 뷰모델의 서버 통신 로직들은 ui단의 뷰모델이 아닌 data단의 repostiory impl에서 수행하는게 더 좋을 것 같아요! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
package com.sopt.now.presentation.auth.login | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.sopt.now.ApplicationClass.SharedPreferences.editor | ||
import com.sopt.now.model.login.RequestLoginDto | ||
import com.sopt.now.repository.AuthRepository | ||
import com.sopt.now.utils.Constants.Companion.MEMBER_ID | ||
import com.sopt.now.utils.NetworkUtil | ||
import com.sopt.now.utils.ServicePool.loginService | ||
import com.sopt.now.utils.UiState | ||
import dagger.hilt.android.HiltAndroidApp | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
class LoginViewModel : ViewModel() { | ||
@HiltViewModel | ||
class LoginViewModel @Inject constructor( | ||
private val authRepository: AuthRepository | ||
) : ViewModel() { | ||
private val _state = MutableStateFlow<UiState<Unit>>(UiState.LOADING) | ||
val state get() = _state.asStateFlow() | ||
Comment on lines
23
to
24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사소하지만 변수의 타입을 명시적으로 적어주는 것이 좋습니다! |
||
|
||
fun postLogin(data: RequestLoginDto) { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
_state.value = UiState.LOADING | ||
runCatching { | ||
loginService.login(data) | ||
authRepository.loginUser(data) | ||
}.onSuccess { | ||
if (it.isSuccessful) { | ||
editor.putString(MEMBER_ID, it.headers()["location"]) | ||
|
@@ -33,12 +36,14 @@ class LoginViewModel : ViewModel() { | |
_state.value = UiState.SUCCESS(null) | ||
} else { | ||
_state.value = UiState.FAILURE( | ||
it.errorBody()?.let { e -> NetworkUtil.getErrorResponse(e)?.message }.toString() | ||
it.errorBody()?.let { e -> NetworkUtil.getErrorResponse(e)?.message } | ||
.toString() | ||
) | ||
} | ||
}.onFailure { | ||
it.printStackTrace() | ||
} | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
package com.sopt.now.presentation.auth.signup | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.sopt.now.model.signup.RequestSignUpDto | ||
import com.sopt.now.repository.AuthRepository | ||
import com.sopt.now.utils.NetworkUtil | ||
import com.sopt.now.utils.ServicePool.authService | ||
import com.sopt.now.utils.UiState | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
class SignUpViewModel : ViewModel() { | ||
@HiltViewModel | ||
class SignUpViewModel @Inject constructor( | ||
private val authRepository: AuthRepository | ||
) : ViewModel() { | ||
private val _state = MutableStateFlow<UiState<Unit>>(UiState.LOADING) | ||
val state = _state.asStateFlow() | ||
val state get() = _state.asStateFlow() | ||
|
||
fun signUp(data: RequestSignUpDto) { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
_state.value = UiState.LOADING | ||
runCatching { | ||
authService.signUp(data) | ||
authRepository.signupUser(data) | ||
}.onSuccess { | ||
if (it.isSuccessful) _state.value = UiState.SUCCESS(null) | ||
else { | ||
Comment on lines
+16
to
30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 진짜 멋있다..이 언니 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.sopt.now.repository | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://developer.android.com/topic/architecture?hl=ko |
||
|
||
import com.sopt.now.model.login.RequestLoginDto | ||
import com.sopt.now.model.signup.RequestSignUpDto | ||
import com.sopt.now.utils.BaseResponse | ||
import retrofit2.Response | ||
|
||
interface AuthRepository { | ||
suspend fun loginUser(data: RequestLoginDto): Response<BaseResponse<Unit>> | ||
|
||
suspend fun signupUser(data: RequestSignUpDto): Response<BaseResponse<Unit>> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.sopt.now.repository | ||
|
||
import com.sopt.now.model.info.UserInfo | ||
import com.sopt.now.utils.BaseResponse | ||
import retrofit2.Response | ||
|
||
interface MyPageRepository { | ||
suspend fun getUserInfo(): Response<BaseResponse<UserInfo>> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,19 @@ import com.sopt.now.BuildConfig | |
import com.sopt.now.datasource.AuthService | ||
import com.sopt.now.datasource.InfoService | ||
import com.sopt.now.utils.ApiFactory.create | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import java.util.concurrent.TimeUnit | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object ApiFactory { | ||
private const val BASE_URL: String = BuildConfig.AUTH_BASE_URL | ||
|
||
Comment on lines
+19
to
23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ApiFactory와 모듈이 utils 폴더 아래에 있는 것은 어색하다고 느껴지네요! |
||
|
@@ -30,12 +36,16 @@ object ApiFactory { | |
.build() | ||
} | ||
|
||
@Provides | ||
fun provideAuthService(): AuthService { | ||
return retrofit.create(AuthService::class.java) | ||
} | ||
|
||
@Provides | ||
fun provideInfoService(): InfoService { | ||
return retrofit.create(InfoService::class.java) | ||
} | ||
|
||
inline fun <reified T> create(): T = | ||
retrofit.create(T::class.java) // create를 통해서 retrofit 구현체 생성 | ||
} | ||
Comment on lines
49
to
51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어 inline함수다 코인액에서 본..!! 역시 아는 만큼 보인다.. |
||
|
||
object ServicePool { | ||
val authService: AuthService by lazy { create<AuthService>() } | ||
val loginService: AuthService by lazy { create<AuthService>() } | ||
val infoService: InfoService by lazy { create<InfoService>() } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.sopt.now.utils | ||
|
||
import com.sopt.now.data.AuthRepositoryImpl | ||
import com.sopt.now.data.MyPageRepositoryImpl | ||
import com.sopt.now.datasource.AuthService | ||
import com.sopt.now.datasource.InfoService | ||
import com.sopt.now.repository.AuthRepository | ||
import com.sopt.now.repository.MyPageRepository | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object AuthModule { | ||
@Provides | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
컴포넌트도 붙여주세요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그리고 repository 인터페이서의 경우 provides보단 binds가 적합합니다! |
||
fun provideAuthRepository(authService: AuthService): AuthRepository { | ||
return AuthRepositoryImpl(authService) | ||
} | ||
|
||
@Provides | ||
fun provideMyPageRepository(infoService: InfoService): MyPageRepository { | ||
return MyPageRepositoryImpl(infoService) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
키야.. 믓찌다..