Skip to content

Commit

Permalink
[Refactor/#9] SharedPreference 전역에서 공유
Browse files Browse the repository at this point in the history
viewModel에서 사용할 수 있도록 SharedPrefernces를 Application 클래스로 옮김
  • Loading branch information
gaeun5744 committed Jun 27, 2023
1 parent 81ec58c commit af70c6c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 85 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />



<application
android:name=".util.MyApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import org.android.go.sopt.databinding.FragmentMyProfileBinding
import org.android.go.sopt.present.loginPage.LoginActivity
import org.android.go.sopt.present.loginPage.MySharedPreferences
import org.android.go.sopt.util.MySharedPreferences
import org.android.go.sopt.remote.ServicePool
import org.android.go.sopt.remote.remoteData.model.MyProfileDto
import org.android.go.sopt.util.makeToastMessage
import retrofit2.Call
import retrofit2.Response
import org.android.go.sopt.util.MyApplication

class MyProfileFragment : Fragment() {
private var _binding: FragmentMyProfileBinding? = null
Expand All @@ -26,7 +23,7 @@ class MyProfileFragment : Fragment() {
override fun onCreateView( // 뷰를 만든다 << 이때 초기화하면 좋음
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View { // 이제 반환하는 View가 Null일 수 없기 때문에, ?를 지워주셔도 됩니다.
_binding = FragmentMyProfileBinding.inflate(inflater, container, false)
return binding.root
Expand All @@ -37,7 +34,6 @@ class MyProfileFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
getProfile()
clickLogOut()

}

override fun onDestroyView() {
Expand All @@ -46,7 +42,7 @@ class MyProfileFragment : Fragment() {
}

private fun getProfile() {
myProfileService.myProfile(
/*myProfileService.myProfile(
MySharedPreferences.getUserId(requireContext())
).enqueue(object : retrofit2.Callback<MyProfileDto> {
override fun onResponse(call: Call<MyProfileDto>, response: Response<MyProfileDto>) {
Expand All @@ -61,22 +57,20 @@ class MyProfileFragment : Fragment() {
override fun onFailure(call: Call<MyProfileDto>, t: Throwable) {
requireContext().makeToastMessage("서버 실패")
}
})
})*/
}

private fun clickLogOut() {
binding.btnLogout.setOnClickListener {
val builder = AlertDialog.Builder(requireContext())
builder.setTitle("Go SOPT").setMessage("로그아웃 하시겠나요?")
.setPositiveButton("") { _, _ ->
MySharedPreferences.clearUser(requireContext())
MyApplication.mySharedPreferences.clearUser()
val intent = Intent(activity, LoginActivity::class.java)
startActivity(intent)
requireActivity().finish()

}.setNegativeButton("아니요", null).setCancelable(true)
builder.show()

}
}
}
}
56 changes: 56 additions & 0 deletions app/src/main/java/org/android/go/sopt/util/MySharedPreferences.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.android.go.sopt.util

import android.content.Context
import android.content.SharedPreferences

class MySharedPreferences(context: Context) {
private val MY_ACCOUNT: String = "account"
private val prefs: SharedPreferences =
context.getSharedPreferences(MY_ACCOUNT, Context.MODE_PRIVATE)

fun setUserId(input: String) {
val editor: SharedPreferences.Editor = prefs.edit()
editor.putString("MY_ID", input)
editor.commit()
}

fun getUserId(): String {
return prefs.getString("MY_ID", "").toString()
}

fun setUserPass(input: String) {
val editor: SharedPreferences.Editor = prefs.edit()
editor.putString("MY_PASS", input)
editor.commit()
}

fun getUserPass(): String {
return prefs.getString("MY_PASS", "").toString()
}

fun setUserName(input: String) {
val editor: SharedPreferences.Editor = prefs.edit()
editor.putString("MY_NAME", input)
editor.commit()
}

fun getUserName(): String {
return prefs.getString("MY_NAME", "").toString()
}

fun setUserSpec(input: String) {
val editor: SharedPreferences.Editor = prefs.edit()
editor.putString("MY_SPEC", input)
editor.commit()
}

fun getUserSpec(): String {
return prefs.getString("MY_SPEC", "").toString()
}

fun clearUser() {
val editor: SharedPreferences.Editor = prefs.edit()
editor.clear()
editor.commit()
}
}

0 comments on commit af70c6c

Please sign in to comment.