-
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/#17] week7-compose / 필수과제 #19
base: develop-compose
Are you sure you want to change the base?
Changes from all commits
b117997
2a18e0b
6b94cf8
abc4d85
a075d09
42f9f9f
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,8 @@ | ||
package com.sopt.now.compose.repository | ||
|
||
import com.sopt.now.data.model.RequestLoginDto | ||
import com.sopt.now.data.model.ResponseLoginDto | ||
|
||
interface LoginRepository { | ||
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. 레포지토리 다 나눈 거 깔끔하네요-!! |
||
suspend fun login(loginDto: RequestLoginDto): Result<ResponseLoginDto> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.sopt.now.compose.repository | ||
|
||
import com.sopt.now.data.model.RequestSignUpDto | ||
import com.sopt.now.data.model.ResponseSignUpDto | ||
|
||
interface SignUpRepository { | ||
suspend fun signUp(signUpDto: RequestSignUpDto): Result<ResponseSignUpDto> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.sopt.now.compose.repository | ||
|
||
import com.sopt.now.data.model.ResponseUserInfoDto | ||
|
||
interface UserInfoRepository { | ||
suspend fun getUserInfo(): Result<ResponseUserInfoDto> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.sopt.now.compose.repositoryimpl | ||
|
||
import com.sopt.now.AuthService | ||
import com.sopt.now.compose.repository.LoginRepository | ||
import com.sopt.now.data.model.RequestLoginDto | ||
import com.sopt.now.data.model.ResponseLoginDto | ||
|
||
|
||
class LoginRepositoryImpl( | ||
private val authService: AuthService | ||
) : LoginRepository { | ||
override suspend fun login(loginDto: RequestLoginDto): Result<ResponseLoginDto> { | ||
return runCatching { | ||
val response = authService.login(loginDto) | ||
if (response.isSuccessful) { | ||
response.body()?.let { | ||
Result.success(it) | ||
} ?: Result.failure(Exception("Body is null")) | ||
} else { | ||
Result.failure(Exception(response.errorBody()?.string())) | ||
} | ||
}.getOrElse { | ||
Result.failure(it) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.sopt.now.compose.repositoryimpl | ||
|
||
import com.sopt.now.AuthService | ||
import com.sopt.now.compose.repository.SignUpRepository | ||
import com.sopt.now.data.model.RequestSignUpDto | ||
import com.sopt.now.data.model.ResponseSignUpDto | ||
|
||
class SignUpRepositoryImpl( | ||
private val authService: AuthService | ||
) : SignUpRepository { | ||
override suspend fun signUp(signUpDto: RequestSignUpDto): Result<ResponseSignUpDto> { | ||
return runCatching { | ||
val response = authService.signUp(signUpDto) | ||
if (response.isSuccessful) { | ||
response.body()?.let { | ||
Result.success(it) | ||
} ?: Result.failure(Exception("Body is null")) | ||
} else { | ||
Result.failure(Exception(response.errorBody()?.string())) | ||
} | ||
}.getOrElse { | ||
Result.failure(it) | ||
} | ||
Comment on lines
+20
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.
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.sopt.now.compose.repositoryimpl | ||
|
||
import com.sopt.now.AuthService | ||
import com.sopt.now.compose.repository.UserInfoRepository | ||
import com.sopt.now.data.model.ResponseUserInfoDto | ||
|
||
class UserInfoRepositoryImpl( | ||
private val authService: AuthService | ||
) : UserInfoRepository { | ||
override suspend fun getUserInfo(): Result<ResponseUserInfoDto> { | ||
return runCatching { | ||
val response = authService.getUserInfo() | ||
if (response.isSuccessful) { | ||
response.body()?.let { | ||
Result.success(it) | ||
} ?: Result.failure(Exception("Body is null")) | ||
} else { | ||
Result.failure(Exception(response.errorBody()?.string())) | ||
} | ||
}.getOrElse { | ||
Result.failure(it) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package com.sopt.now.compose.screen.auth.login | ||
|
||
import android.widget.Toast | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
|
@@ -25,21 +24,27 @@ import androidx.compose.ui.unit.dp | |
import androidx.compose.ui.unit.sp | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.navigation.NavHostController | ||
import com.sopt.now.ServicePool | ||
import com.sopt.now.compose.R | ||
import com.sopt.now.compose.component.ActionButton | ||
import com.sopt.now.compose.component.InputField | ||
import com.sopt.now.compose.navigation.NavRoutes | ||
import com.sopt.now.compose.repositoryimpl.LoginRepositoryImpl | ||
import com.sopt.now.compose.utils.ToastUtil.toast | ||
import com.sopt.now.data.model.RequestLoginDto | ||
|
||
@Composable | ||
fun LoginScreen( | ||
navController: NavHostController | ||
) { | ||
val viewModel: LoginViewModel = viewModel() | ||
val context = LocalContext.current | ||
val authService = ServicePool.authService | ||
val loginRepository = LoginRepositoryImpl(authService) | ||
val viewModel: LoginViewModel = viewModel(factory = LoginViewModelFactory(loginRepository)) | ||
|
||
var userId by rememberSaveable { mutableStateOf("") } | ||
var userPwd by rememberSaveable { mutableStateOf("") } | ||
val loginState = viewModel.loginState.observeAsState() | ||
val context = LocalContext.current // to use ToastMessage | ||
val loginState by viewModel.loginState.observeAsState() | ||
|
||
Column(modifier = Modifier.padding(16.dp)) { | ||
LoginTitle() | ||
|
@@ -58,19 +63,19 @@ fun LoginScreen( | |
isPassword = true | ||
) | ||
|
||
when (val state = loginState.value) { | ||
when (val state = loginState) { | ||
is LoginState.Loading -> CircularProgressIndicator() | ||
is LoginState.Success -> { | ||
LaunchedEffect(state.message) { | ||
Toast.makeText(context, state.message, Toast.LENGTH_LONG).show() | ||
toast(context, state.message) | ||
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. 이렇게 쓰니까 훨씬 깔끔하네요! 배우고 갑니당 |
||
navController.navigate(NavRoutes.HOME) { | ||
popUpTo(NavRoutes.LOGIN) { inclusive = true } | ||
} | ||
} | ||
} | ||
is LoginState.Error -> { | ||
LaunchedEffect(state.message) { | ||
Toast.makeText(context, state.message, Toast.LENGTH_LONG).show() | ||
toast(context, state.message) | ||
} | ||
ActionButton( | ||
text = stringResource(id = R.string.login_button_signin), | ||
|
@@ -97,6 +102,7 @@ fun LoginScreen( | |
} | ||
} | ||
|
||
|
||
@Composable | ||
fun LoginTitle() { | ||
Column( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.sopt.now.compose.screen.auth.login | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.sopt.now.compose.repository.LoginRepository | ||
|
||
class LoginViewModelFactory(private val loginRepository: LoginRepository) : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(LoginViewModel::class.java)) { | ||
@Suppress("UNCHECKED_CAST") | ||
return LoginViewModel(loginRepository) as T | ||
} | ||
throw IllegalArgumentException("알 수 없는 ViewModel 클래스") | ||
} | ||
} |
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.
ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ 때리기 삭제한 거 웃기네