-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/seminar7 basic #9
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
base: develop
Are you sure you want to change the base?
Changes from 13 commits
a4ec4fd
37a0782
cd3278c
4380c67
d31f234
d99a935
00eacd0
28033c6
84261f3
9ba0caa
64d5fdb
1cdaec6
bd4015a
f624a80
0ed0212
0489e44
f237bd4
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,11 @@ | ||
| package org.sopt.dosopttemplate.data.dataclass | ||
|
|
||
| object UserInfo { | ||
| var userInfoList = User( | ||
| nick = "", | ||
| self_description = "", | ||
| id = "", | ||
| pw = "", | ||
| mbti = "", | ||
| ) | ||
| } | ||
|
Member
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. 여기서 mapping함수를 추가하면 좋습니다!
Contributor
Author
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. 매핑함수를 공부한지 얼마 안돼서
Member
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. 제가쓴 mapping함수랑 클린아키텍쳐나 ob들이 사용했던 map함수의 목적이 달라서,, |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,8 @@ import kotlinx.serialization.Serializable | |
| @Serializable | ||
| data class RequestLoginDto ( | ||
| @SerialName("username") | ||
| val username: String, | ||
| var username: String, | ||
|
|
||
| @SerialName("password") | ||
| val password: String, | ||
| var password: String, | ||
|
||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import org.sopt.dosopttemplate.data.dto.request.RequestUserDataDto | |
| import org.sopt.dosopttemplate.data.dto.response.ResponseLoginDto | ||
| import retrofit2.Call | ||
| import org.sopt.dosopttemplate.data.dto.response.ResponseUserDataDto | ||
| import retrofit2.Response | ||
| import retrofit2.http.Body | ||
| import retrofit2.http.GET | ||
| import retrofit2.http.POST | ||
|
|
@@ -14,17 +15,17 @@ import retrofit2.http.Path | |
| interface AuthService { | ||
|
|
||
| @POST("api/v1/members/sign-in") | ||
| fun postLogin( | ||
| fun postLogin( | ||
| @Body request: RequestLoginDto, | ||
| ): Call<ResponseLoginDto> | ||
| ): Response<ResponseLoginDto> | ||
|
|
||
| @POST("api/v1/members") | ||
| fun signUp( | ||
| fun postSignUp( | ||
| @Body request: RequestSignUpDto, | ||
| ): Call<Unit> | ||
| ): Response<Unit> | ||
|
|
||
| @GET("api/v1/members/{memberId}") | ||
| fun getUserInfo( | ||
| @Path("memberId") memberId: Int | ||
| ): Call<ResponseUserDataDto> | ||
| ): Response<ResponseUserDataDto> | ||
|
Contributor
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. 코루틴이 아직 적용이 안되었는데 다음에 적용해봅시다!
Contributor
Author
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. 이제는 적용 제대로 됐죠....????? 이번 커밋에 뷰모델 안 가져와있던거 제대로 가져와서 아마 된거 같은데.....된거 맞겠....죠?!?!???
Contributor
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가 안붙었는데 적용 안한거 같은데
Contributor
Author
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. 어라 이거 진짜 엄청 삽질하면서 다 바꾸고 커밋 다 올렸는데??????안스가 또????? 집가서 함 확인해봐야겠네영...
Contributor
Author
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. 확인해봤는데 일단 안스에서는 다 누락없이 푸시하고 깃헙에도 올라간거 같은데 이 코리 창에서는 변경된 부분이 안보이네요...! 저번엔 됐었던거 같은데... 🤔🤔 혹시 깃헙 pr 올린 뒤에 커밋 올리면 이미 이전에 코리한 코드창에는 반영이 안되던가요...?일단 0ed0212 요 커밋을 봐주시죠~~!~! 코루틴 이렇게하면 이번엔 진짜로 적용된거 맞죠...???!
Contributor
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 |
|---|---|---|
|
|
@@ -4,26 +4,26 @@ package org.sopt.dosopttemplate.presentation.login | |
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import androidx.lifecycle.lifecycleScope | ||
| import kotlinx.coroutines.launch | ||
| import org.sopt.dosopttemplate.databinding.ActivityLoginBinding | ||
| import org.sopt.dosopttemplate.presentation.mainhome.MainHomeActivity | ||
| import org.sopt.dosopttemplate.module.ServicePool.authService | ||
| import org.sopt.dosopttemplate.presentation.mainhome.MainHomeViewModel | ||
| import org.sopt.dosopttemplate.presentation.signUp.SignUpActivity | ||
| import org.sopt.dosopttemplate.databinding.ActivityLoginBinding | ||
| import org.sopt.dosopttemplate.data.dto.request.RequestLoginDto | ||
| import org.sopt.dosopttemplate.data.dto.response.ResponseLoginDto | ||
| import org.sopt.dosopttemplate.utils.toast | ||
| import org.sopt.dosopttemplate.utils.snackbar | ||
| import retrofit2.Call | ||
| import retrofit2.Callback | ||
| import retrofit2.Response | ||
|
|
||
|
|
||
| class LoginActivity : AppCompatActivity() { | ||
|
|
||
| private lateinit var binding: ActivityLoginBinding | ||
| private lateinit var loginViewModel: LoginViewModel | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| binding = ActivityLoginBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
| loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java) | ||
|
|
||
|
|
||
| initLoginClickListener() | ||
|
|
@@ -73,12 +73,37 @@ class LoginActivity : AppCompatActivity() { | |
| val id = binding.etLoginId.text.toString() | ||
| val password = binding.etLoginPw.text.toString() | ||
| binding.btLogin.setOnClickListener { | ||
| checkLoginAvailableFromServer(id, password) | ||
| loginViewModel.checkLoginAvailableFromServer(id, password) | ||
| checkLoginAvailableFromServer() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun checkLoginAvailableFromServer() { | ||
| lifecycleScope.launch { | ||
| loginViewModel.loginState.collect { loginState -> | ||
| when (loginState) { | ||
| is LoginState.Success -> { | ||
| toast("로그인 성공") | ||
| val intent = Intent(this@LoginActivity, MainHomeActivity::class.java) | ||
| startActivity(intent) | ||
| } | ||
|
|
||
| is LoginState.Error -> { | ||
| toast("로그인 실패") | ||
| } | ||
|
|
||
| is LoginState.Loading -> { | ||
| toast("로그인 중") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun checkLoginAvailableFromServer(id: String, password: String) { | ||
| /* | ||
|
|
||
|
|
||
|
||
| authService.postLogin(RequestLoginDto(id, password)) | ||
| .enqueue(object : Callback<ResponseLoginDto> { | ||
| override fun onResponse( | ||
|
|
@@ -88,7 +113,7 @@ class LoginActivity : AppCompatActivity() { | |
| if (response.isSuccessful) { | ||
| val data: ResponseLoginDto = requireNotNull(response.body()!!) | ||
| val userId: Int = data.id | ||
| toast("로그인 성공, 유저의 ID는 $userId 입니다") | ||
| toast("로그인 성공") | ||
| val intent = Intent(this@LoginActivity, MainHomeActivity::class.java) | ||
| intent.putExtra("id", userId) | ||
| startActivity(intent) | ||
|
|
@@ -101,7 +126,7 @@ class LoginActivity : AppCompatActivity() { | |
| toast("서버 에러 발생") | ||
| } | ||
| }) | ||
| } | ||
| */ | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.sopt.dosopttemplate.presentation.login | ||
|
|
||
| import org.sopt.dosopttemplate.data.dto.response.ResponseLoginDto | ||
|
|
||
| sealed class LoginState { | ||
| data class Success(val userId: ResponseLoginDto) : LoginState() | ||
| object Loading: LoginState() | ||
| object Error : LoginState() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package org.sopt.dosopttemplate.presentation.login | ||
|
|
||
| import androidx.lifecycle.LiveData | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import org.sopt.dosopttemplate.data.dataclass.UserInfo | ||
| import org.sopt.dosopttemplate.data.dto.request.RequestLoginDto | ||
| import org.sopt.dosopttemplate.module.ApiFactory | ||
| import org.sopt.dosopttemplate.module.AuthService | ||
|
|
||
| class LoginViewModel() : ViewModel() { | ||
| private val _loginSuccess: MutableLiveData<Boolean> = MutableLiveData() | ||
| var loginSuccess: LiveData<Boolean> = _loginSuccess | ||
|
|
||
| private val _loginState = MutableStateFlow<LoginState>(LoginState.Loading) | ||
| val loginState: StateFlow<LoginState> = _loginState.asStateFlow() | ||
|
||
|
|
||
|
|
||
| fun checkLoginAvailableFromServer(id: String, password: String) { | ||
|
||
| viewModelScope.launch { | ||
| val loginService = ApiFactory.create<AuthService>() | ||
| kotlin.runCatching { | ||
| loginService.postLogin(RequestLoginDto(id, password)) | ||
| }.onSuccess { | ||
| val body = it.body() | ||
| if (it.body() != null) { | ||
| _loginState.value = LoginState.Success(body!!) | ||
| UserInfo.userInfoList.id = id | ||
| UserInfo.userInfoList.pw = password | ||
|
|
||
| } else { | ||
| _loginState.value = LoginState.Error | ||
| } | ||
| }.onFailure { | ||
| _loginState.value = LoginState.Error | ||
| } | ||
|
|
||
| ///* | ||
| // ServicePool.authService.postLogin(RequestLoginDto(id, password)) | ||
| // .enqueue(object : Callback<ResponseLoginDto> { | ||
| // override fun onResponse( | ||
| // call: Call<ResponseLoginDto>, | ||
| // response: Response<ResponseLoginDto>, | ||
| // ) { | ||
| // if (response.isSuccessful) { | ||
| // val data: ResponseLoginDto = requireNotNull(response.body()!!) | ||
| // | ||
| // | ||
| // val userId: Int = data.id | ||
| // loginSuccess.value = true | ||
| // toast("로그인 성공") | ||
| // val intent = Intent(context, MainHomeActivity::class.java) | ||
| // intent.putExtra("id", userId) | ||
| // | ||
| // | ||
| // startActivity(intent) | ||
| // } else { | ||
|
Comment on lines
+43
to
+62
Member
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. 심화과제하고 추후에 주석은 지워주세요!
Contributor
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. 동의합니당! |
||
| // toast("아이디와 패스워드가 일치 하지 않습니다.") | ||
| // } | ||
| // } | ||
| // | ||
| // override fun onFailure(call: Call<ResponseLoginDto>, t: Throwable) { | ||
| // toast("서버 에러 발생") | ||
| // } | ||
| // }) | ||
| //*/ | ||
|
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,7 @@ class MainHomeActivity : AppCompatActivity() { | |
| private fun clickBottomNavigation() { | ||
| var userId = intent.getIntExtra("id", -1) | ||
|
|
||
| mainHomeViewModel.userId=userId | ||
| // mainHomeViewModel.userId=userId | ||
|
||
| binding.bnvHome.setOnItemSelectedListener { | ||
| mainHomeViewModel.clickBottomNavigation(it.itemId) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,12 @@ import androidx.fragment.app.Fragment | |
| import androidx.lifecycle.LiveData | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import org.sopt.dosopttemplate.MyApplication | ||
| import org.sopt.dosopttemplate.data.dataclass.Friend | ||
| import org.sopt.dosopttemplate.R | ||
| import org.sopt.dosopttemplate.data.dataclass.User | ||
| import org.sopt.dosopttemplate.data.dataclass.UserInfo | ||
| import org.sopt.dosopttemplate.data.dto.request.RequestUserDataDto | ||
| import org.sopt.dosopttemplate.data.dto.response.ResponseUserDataDto | ||
| import org.sopt.dosopttemplate.module.ServicePool.authService | ||
|
|
@@ -19,10 +22,13 @@ import retrofit2.Response | |
|
|
||
| class MainHomeViewModel : ViewModel() { | ||
| private val _navigateTo = MutableLiveData<Fragment>() | ||
| val navigateTo: LiveData<Fragment> | ||
| get() = _navigateTo | ||
| val navigateTo: LiveData<Fragment> get() = _navigateTo | ||
|
Comment on lines
24
to
+25
Contributor
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. 혹시 이 코드는 어떤 목적으로 사용 되는 것 일까요??
Contributor
Author
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. 이 친구는 아마 하단 네비게이션 부분이 터치 인식됐을 때, viewModel에서 어떤 fragment를 보여줘야되는지 naviagetTo에 저장하고, activity에서 navigateTo값을 observe해서 fragment를 바꾸는 목적으로 사용했습니다!
Contributor
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. viewModel에서 어떤 프레그먼트로 이동할지 알 필요가 있을까요? |
||
|
|
||
| private var _userId: MutableLiveData<String> = MutableLiveData<String>() | ||
| val userId: LiveData<String> = _userId | ||
| val userPw: MutableLiveData<String> = MutableLiveData<String>() | ||
|
|
||
|
|
||
| var userId: Int = 0 | ||
| fun clickBottomNavigation(itemId: Int): Boolean { | ||
|
|
||
| when (itemId) { | ||
|
|
@@ -37,7 +43,7 @@ class MainHomeViewModel : ViewModel() { | |
| } | ||
|
|
||
| R.id.menu_mypage -> { | ||
| myPageUserInfo(userId) | ||
| myPageUserInfo() | ||
| _navigateTo.value = MyPageFragment() | ||
| return true | ||
| } | ||
|
|
@@ -52,29 +58,9 @@ class MainHomeViewModel : ViewModel() { | |
| } | ||
|
|
||
|
|
||
| private fun myPageUserInfo(userId: Int) { | ||
| authService.getUserInfo(userId) | ||
| .enqueue(object : retrofit2.Callback<ResponseUserDataDto> { | ||
| override fun onResponse( | ||
| call: Call<ResponseUserDataDto>, | ||
| response: Response<ResponseUserDataDto>, | ||
| ) { | ||
| if (response.isSuccessful) { | ||
| val data: ResponseUserDataDto = requireNotNull(response.body()) | ||
| val userNickname = data.nickname | ||
| val userUsername = data.username | ||
|
|
||
| //sharedpreference로 넘기기 | ||
| MyApplication.prefs.setString("nick", userNickname) | ||
| MyApplication.prefs.setString("username", userUsername) | ||
| MyApplication.prefs.setString("id", userId.toString()) | ||
| } | ||
| } | ||
|
|
||
| override fun onFailure(call: Call<ResponseUserDataDto>, t: Throwable) { | ||
| _errorMsg.value = "서버 에러 발생" | ||
| } | ||
| }) | ||
| private fun myPageUserInfo() { | ||
| _userId.value = UserInfo.userInfoList.id | ||
| userPw.value = UserInfo.userInfoList.pw | ||
|
Contributor
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. 얘도
Contributor
Author
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. 근데 userPw는 이 뷰모델에서 User로 값 복붙 해주는거 밖에 안하는데 그래도 _userPw 만들어 놓는게 나을까요??
Contributor
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. 복붙밖에 안한다면 private로 만드는게 좋지 않을까요? |
||
| } | ||
|
|
||
| private var _errorMsg = MutableLiveData<String>() | ||
|
|
||
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.
디폴트 값으로 ""를 준 이유를 한 번 생각해보시면 좋을 것 같아요!
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.
디폴드 값을 주긴줘야되는데 디폴트값으로 넣을만한 값을 딱히 모르겠어서 ""로 퉁친건데 보통 일반적으로 사용되는 디폴드값이 있을까요??
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.
왜 꼭 디폴트 값을 주어야 할지 nonNull을 주면 안되는지 생각해보면 좋을 것 같아서요 답은 프로젝트의 성격에 따라 다릅니다 string의 기본값은 ""을 대부분 사용해요