-
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 all 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,16 @@ | ||
| package org.sopt.dosopttemplate.data.dataclass | ||
|
|
||
| object UserInfo { | ||
| var userInfoList = User( | ||
| nick = "", | ||
| self_description = "", | ||
| id = "", | ||
| pw = "", | ||
| mbti = "", | ||
| ) | ||
|
|
||
| fun toUser(inId: String, inPw: String) { | ||
| userInfoList.id = inId | ||
| userInfoList.pw = inPw | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,27 +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() | ||
| // val receivedUserInfoList = intent.getStringArrayListExtra("userInfoList")!! | ||
|
|
@@ -63,22 +62,43 @@ class LoginActivity : AppCompatActivity() { | |
| //νμκ°μ νμ΄μ§λ‘ μ΄λ | ||
| val intent = Intent(this, SignUpActivity::class.java) | ||
| startActivity(intent) | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| private fun initLoginClickListener() { | ||
| with(binding) { | ||
| val id = binding.etLoginId.text.toString() | ||
| val password = binding.etLoginPw.text.toString() | ||
| binding.btLogin.setOnClickListener { | ||
| checkLoginAvailableFromServer(id, password) | ||
| loginViewModel.checkLoginFromServer(id, password) | ||
| checkLoginAvailableFromServer() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun checkLoginAvailableFromServer(id: String, password: String) { | ||
| 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 -> {} | ||
|
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. μ΄ Stateλ μ΄λ€ μν μ νκ³ μλ건κ°μ? |
||
|
|
||
| else -> {} | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| authService.postLogin(RequestLoginDto(id, password)) | ||
| .enqueue(object : Callback<ResponseLoginDto> { | ||
| override fun onResponse( | ||
|
|
@@ -88,7 +108,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 +121,7 @@ class LoginActivity : AppCompatActivity() { | |
| toast("μλ² μλ¬ λ°μ") | ||
| } | ||
| }) | ||
| } | ||
| */ | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| 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() | ||
| object Unstarted : 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.Unstarted) | ||
| val loginState: StateFlow<LoginState> = _loginState.asStateFlow() | ||
|
|
||
|
|
||
| fun checkLoginFromServer(id: String, password: String) { | ||
| viewModelScope.launch { | ||
| val loginService = ApiFactory.create<AuthService>() | ||
| _loginState.value = LoginState.Loading | ||
| kotlin.runCatching { | ||
| loginService.postLogin(RequestLoginDto(id, password)) | ||
| }.onSuccess { | ||
| val body = it.body() | ||
| if (it.body() != null) { | ||
| _loginState.value = LoginState.Success(requireNotNull(body)) | ||
| UserInfo.toUser(id, 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 |
|---|---|---|
|
|
@@ -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μ κΈ°λ³Έκ°μ ""μ λλΆλΆ μ¬μ©ν΄μ