diff --git a/app/src/main/java/com/example/infraandroid/MainActivity.kt b/app/src/main/java/com/example/infraandroid/MainActivity.kt index ea0cf04..8317d14 100644 --- a/app/src/main/java/com/example/infraandroid/MainActivity.kt +++ b/app/src/main/java/com/example/infraandroid/MainActivity.kt @@ -9,6 +9,8 @@ import androidx.navigation.ui.NavigationUI import com.example.infraandroid.databinding.ActivityMainBinding import android.view.View +import android.widget.Toast +import com.google.android.material.snackbar.Snackbar import com.kakao.sdk.common.util.Utility class MainActivity : AppCompatActivity() { diff --git a/app/src/main/java/com/example/infraandroid/home/view/fragment/HomeFragment.kt b/app/src/main/java/com/example/infraandroid/home/view/fragment/HomeFragment.kt index 808fed4..af6e044 100644 --- a/app/src/main/java/com/example/infraandroid/home/view/fragment/HomeFragment.kt +++ b/app/src/main/java/com/example/infraandroid/home/view/fragment/HomeFragment.kt @@ -1,11 +1,6 @@ package com.example.infraandroid.home.view.fragment -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup import android.widget.Toast -import androidx.fragment.app.Fragment import androidx.navigation.findNavController import com.example.infraandroid.util.InfraApplication import com.example.infraandroid.R @@ -119,4 +114,6 @@ class HomeFragment : BaseFragment(R.layout.fragment_home) { } } } + + } \ No newline at end of file diff --git a/app/src/main/java/com/example/infraandroid/id/view/LoginFragment.kt b/app/src/main/java/com/example/infraandroid/id/view/LoginFragment.kt index 99eb0d8..df4f572 100644 --- a/app/src/main/java/com/example/infraandroid/id/view/LoginFragment.kt +++ b/app/src/main/java/com/example/infraandroid/id/view/LoginFragment.kt @@ -6,6 +6,7 @@ import android.content.IntentSender import android.util.Log import android.widget.Toast import androidx.navigation.findNavController +import androidx.navigation.fragment.findNavController import com.example.infraandroid.BuildConfig import com.example.infraandroid.util.InfraApplication import com.example.infraandroid.R @@ -40,7 +41,7 @@ class LoginFragment : BaseFragment(R.layout.fragment_login private var showOneTapUI = true override fun FragmentLoginBinding.onCreateView(){ - oneTapClient = Identity.getSignInClient(requireActivity()) +// oneTapClient = Identity.getSignInClient(requireActivity()) // signInRequest = BeginSignInRequest.builder() // .setPasswordRequestOptions(BeginSignInRequest.PasswordRequestOptions.builder() // .setSupported(true) @@ -56,6 +57,46 @@ class LoginFragment : BaseFragment(R.layout.fragment_login // // Automatically sign in when exactly one credential is retrieved. // .setAutoSelectEnabled(true) // .build() + if(InfraApplication.prefs.getUserId().isNotEmpty() + and InfraApplication.prefs.getUserPW().isNotEmpty()){ + + val requestLoginData = RequestLoginData( + userId = InfraApplication.prefs.getUserId(), + userPw = InfraApplication.prefs.getUserPW(), + ) + + val call: Call = ServiceCreator.loginService + .postLogin(requestLoginData) + + call.enqueue(object : Callback{ + override fun onResponse( + call: Call, + response: Response + ) { + if(response.isSuccessful){ + val code = response.body()?.code + when(code){ + 1000 -> { + InfraApplication.prefs.setString("jwt", response.body()?.result?.jwt.toString()) + InfraApplication.prefs.setString("refreshToken", response.body()?.result?.refreshToken.toString()) + InfraApplication.prefs.setString("userId", response.body()?.result?.userId.toString()) + InfraApplication.prefs.setString("userNickName", response.body()?.result?.userNickName.toString()) + Toast.makeText(requireActivity(),"요청에 성공하셨습니다.", Toast.LENGTH_SHORT).show() + // 로그인 버튼을 누르면 home_fragment로 이동 + findNavController().navigate(R.id.action_login_fragment_to_home_fragment) + } + 2001 -> {Toast.makeText(requireActivity(),"id가 비어있습니다.", Toast.LENGTH_SHORT).show()} + 3014 -> {Toast.makeText(requireActivity(),"없는 아이디거나 비밀번호가 틀렸습니다.", Toast.LENGTH_SHORT).show()} + 4000 -> {Toast.makeText(requireActivity(),"데이터베이스 연결에 실패하였습니다.", Toast.LENGTH_SHORT).show()} + } + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.e("login_server_test", "fail") + } + }) + } } override fun FragmentLoginBinding.onViewCreated(){ @@ -87,6 +128,8 @@ class LoginFragment : BaseFragment(R.layout.fragment_login InfraApplication.prefs.setString("refreshToken", response.body()?.result?.refreshToken.toString()) InfraApplication.prefs.setString("userId", response.body()?.result?.userId.toString()) InfraApplication.prefs.setString("userNickName", response.body()?.result?.userNickName.toString()) + InfraApplication.prefs.setUserId(response.body()?.result?.userId.toString()) + InfraApplication.prefs.setUserPW(inputPw) Toast.makeText(requireActivity(),"요청에 성공하셨습니다.", Toast.LENGTH_SHORT).show() // 로그인 버튼을 누르면 home_fragment로 이동 it.findNavController().navigate(R.id.action_login_fragment_to_home_fragment) diff --git a/app/src/main/java/com/example/infraandroid/id/viewmodel/Prefs.kt b/app/src/main/java/com/example/infraandroid/id/viewmodel/Prefs.kt index a1b2b10..40219bc 100644 --- a/app/src/main/java/com/example/infraandroid/id/viewmodel/Prefs.kt +++ b/app/src/main/java/com/example/infraandroid/id/viewmodel/Prefs.kt @@ -1,14 +1,35 @@ package com.example.infraandroid.id.viewmodel import android.content.Context +import android.content.SharedPreferences class Prefs(context: Context) { - private val prefs=context.getSharedPreferences("prefs_name", Context.MODE_PRIVATE) + private val prefs : SharedPreferences = context.getSharedPreferences("prefs_name", Context.MODE_PRIVATE) + private val editor : SharedPreferences.Editor = prefs.edit() fun getString(key: String, defValue: String): String { return prefs.getString(key, defValue).toString() } fun setString(key: String, str: String) { - prefs.edit().putString(key, str).apply() + val editor : SharedPreferences.Editor = prefs.edit() + editor.putString(key, str).apply() + } + fun setUserId(input: String){ + editor.putString("MY_ID", input) + editor.commit() + } + fun setUserPW(input: String){ + editor.putString("MY_PW", input) + editor.commit() + } + fun getUserId():String{ + return prefs.getString("MY_ID", "").toString() + } + fun getUserPW():String{ + return prefs.getString("MY_PW", "").toString() + } + fun clearUser(){ + editor.clear() + editor.commit() } } \ No newline at end of file