Skip to content

Commit

Permalink
#19 로그인 시 achieve 배열 프리퍼런스 저장, data class 생성, array.xml에 업적 목록 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
otcroz committed Sep 13, 2022
1 parent a938f6c commit 21fbf58
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 26 deletions.
5 changes: 5 additions & 0 deletions SHAPEUP2022/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.DialogInterface
import android.content.Intent
import com.example.shape_up_2022.adapter.AchieveAdapter
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.example.shape_up_2022.*
Expand All @@ -12,10 +14,15 @@ import com.example.shape_up_2022.common.MyPageActivity
import com.example.shape_up_2022.common.SaveSharedPreference
import com.example.shape_up_2022.databinding.ActivityAchieveBinding
import com.example.shape_up_2022.common.SimulationActivity
import com.example.shape_up_2022.retrofit.GetPetInfoRes
import com.example.shape_up_2022.retrofit.MyApplication
import com.example.shape_up_2022.simulation.TestActivity
import com.example.shape_up_2022.todo.TodoActivity
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class AchieveActivity : AppCompatActivity() {

Expand All @@ -40,7 +47,10 @@ class AchieveActivity : AppCompatActivity() {
binding.pbAchieveTodo.progress = 50
}
*/


/* 반려견 이름 업데이트 */
getPetInfo()

binding.tabs.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
override fun onTabSelected(tab: TabLayout.Tab?) {

Expand All @@ -60,10 +70,9 @@ class AchieveActivity : AppCompatActivity() {
TabLayoutMediator(binding.tabs, binding.achievePager) {
tab,position->
when(position){
0->tab.text = "종합"
1->tab.text = "진행도"
2->tab.text = "성실도"
3->tab.text = "호감도"
0->tab.text = "진행도"
1->tab.text = "성실도"
2->tab.text = "호감도"
}
}.attach()

Expand Down Expand Up @@ -103,7 +112,7 @@ class AchieveActivity : AppCompatActivity() {
override fun onStart() {
super.onStart()


/* 가족 그룹에 가입되어 있지 않은 상태일 때 */
val eventHandler = object : DialogInterface.OnClickListener {
override fun onClick(p0: DialogInterface?, p1: Int) {
if(p1 == DialogInterface.BUTTON_POSITIVE) {
Expand All @@ -120,8 +129,36 @@ class AchieveActivity : AppCompatActivity() {
.setPositiveButton("확인", eventHandler)
.setCancelable(false)

/* if(SaveSharedPreference.getFamliyID(this)!! == ""){

if(SaveSharedPreference.getFamliyID(this)!! == ""){
builder.show()
}*/
}
}

/* 강아지 정보 업데이트 */
private fun getPetInfo(){
val petID = SaveSharedPreference.getPetID(this)!!
Log.d("mobileApp", "$petID")

val call: Call<GetPetInfoRes> = MyApplication.networkServicePet.getPetInfo(
petID = petID
)

call?.enqueue(object : Callback<GetPetInfoRes> {
override fun onResponse(call: Call<GetPetInfoRes>, response: Response<GetPetInfoRes>) {
if(response.isSuccessful){
Log.d("mobileApp", "$response ${response.body()}")
if(response.body()!!.success){
// 강아지 이름 업데이트
binding.achievePetName.text = response.body()?.petInfo?.name
}
}
}

override fun onFailure(call: Call<GetPetInfoRes>, t: Throwable) {
Log.d("mobileApp", "onFailure $t")
Toast.makeText(baseContext, "네트워크 오류 발생", Toast.LENGTH_SHORT).show()
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.shape_up_2022.R
import com.example.shape_up_2022.adapter.AchieveProgressAdapter
import com.example.shape_up_2022.common.SaveSharedPreference
import com.example.shape_up_2022.databinding.AchieveFragment1Binding
import com.example.shape_up_2022.simulation.SimWalkSearchAdapter

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
Expand All @@ -21,6 +27,7 @@ class AchieveFragment1 : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
lateinit var binding : AchieveFragment1Binding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -36,7 +43,12 @@ class AchieveFragment1 : Fragment() {
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.achieve_fragment_1, container, false)
binding = AchieveFragment1Binding.inflate(inflater, container, false)

// 리사이클러 뷰
achieveList()

return binding.root
}

companion object {
Expand All @@ -58,4 +70,26 @@ class AchieveFragment1 : Fragment() {
}
}
}

/* 업적 리사이클러 뷰 */
private fun achieveList(){
val achieveAdapter = AchieveProgressAdapter(requireContext(), addAchieveList())
binding.walkSearchRecyclerView.layoutManager = LinearLayoutManager(requireContext())
binding.walkSearchRecyclerView.adapter = achieveAdapter // data array
}

/* 업적 목록 추가 */
data class AchieveProgress(val content: String, val checked: Boolean)

private fun addAchieveList(): ArrayList<AchieveProgress>{
var data = arrayListOf<AchieveProgress>()
// 배열 가져오기
val achieveArray: Array<String> = resources.getStringArray(R.array.achieve_progress) // 업적 데이터
val achieveChecked : ArrayList<Boolean> = SaveSharedPreference.getAchieve(requireContext())!! // 달성 여부

// 목록 추가
for(i in achieveArray.indices) AchieveProgress(achieveArray[i], achieveChecked[i])

return data
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import com.example.shape_up_2022.achieve.AchieveFragment4


class AchieveAdapter(fragment: FragmentActivity): FragmentStateAdapter(fragment) {
override fun getItemCount(): Int =4
override fun getItemCount(): Int =3

override fun createFragment(position: Int): Fragment {
return when (position){
0-> AchieveFragment1()
1-> AchieveFragment2()
2-> AchieveFragment3()
else -> AchieveFragment4()

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.shape_up_2022.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.shape_up_2022.achieve.AchieveFragment1
import com.example.shape_up_2022.databinding.ItemAchieveBinding
import com.example.shape_up_2022.retrofit.myItem


class ViewHolderAchieve(val binding: ItemAchieveBinding): RecyclerView.ViewHolder(binding.root)
class AchieveProgressAdapter(val context: Context, val datas:ArrayList<AchieveFragment1.AchieveProgress>?):
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return ViewHolderAchieve(ItemAchieveBinding.inflate(LayoutInflater.from(parent.context), parent,false ))
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val binding = (holder as ViewHolderAchieve).binding
val model = datas!![position]

}

override fun getItemCount(): Int {
return datas?.size ?: 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ class LoginActivity : AppCompatActivity() {
if(response.body()?.loginSuccess == "true"){ // 로그인 성공

// 프리퍼런스에 값 저장
SaveSharedPreference.setUserEmail(baseContext, inputID)
SaveSharedPreference.setUserName(baseContext, response.body()!!.userName)
SaveSharedPreference.setUserID(baseContext, response.body()!!.userID)
SaveSharedPreference.setFamliyID(baseContext, response.body()!!.familyID?:null)
SaveSharedPreference.setUserTested(baseContext, response.body()!!.tested)
SaveSharedPreference.setUserEmail(baseContext, inputID) // 이메일
SaveSharedPreference.setUserName(baseContext, response.body()!!.user.name) // 이름
SaveSharedPreference.setUserID(baseContext, response.body()!!.user._id) // userID
SaveSharedPreference.setFamliyID(baseContext, response.body()!!.user.familyID?:null) // familyID
SaveSharedPreference.setUserTested(baseContext, response.body()!!.user.tested) // 성향점검 여부
SaveSharedPreference.setAchieve(baseContext, response.body()!!.user.achieve) // 업적 달성 여부

// petID 프리퍼런스에 저장
if(response.body()!!.familyID!! != null){
callGetPetID(response.body()!!.familyID!!)
if(response.body()!!.user.familyID!! != null){
callGetPetID(response.body()!!.user.familyID!!)
}

} else { // 로그인 실패 or null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.example.shape_up_2022.common
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class SaveSharedPreference {
companion object{
Expand Down Expand Up @@ -67,6 +69,24 @@ class SaveSharedPreference {
}
fun getPetID(ctx: Context?): String? { return getSharedPreferences(ctx!!)!!.getString("petID", "") }

// User의 achieve
fun setAchieve(ctx: Context, achieve: ArrayList<Boolean>?) {
if (achieve==null) return
val gson = Gson()
val json = gson.toJson(achieve)
val editor = getSharedPreferences(ctx)!!.edit()
editor.putString("achieve", json)
editor.commit()
}
fun getAchieve(ctx: Context?): ArrayList<Boolean>? {
val json = getSharedPreferences(ctx!!)!!.getString("achieve", "")
val gson = Gson()
return gson.fromJson(
json,
object : TypeToken<ArrayList<Boolean?>>() {}.type
)
}


// 저장된 SharedPreference 값 전체 삭제
fun clearAll(ctx: Context?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class MyApplication: Application() {
var client = OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor()).build()

/* 서버 요청용 */
val apiserver = "http://ec2-13-124-250-65.ap-northeast-2.compute.amazonaws.com:5000/"
// http://192.168.219.113:5000/
val apiserver = "http://192.168.219.113:5000/"
// http://ec2-13-124-250-65.ap-northeast-2.compute.amazonaws.com:5000/

var networkServiceUsers: NetworkServiceUsers
val retrofitUsers: Retrofit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ interface NetworkServiceUsers {

}

// req, res
data class RegisterReq(val name: String, val email: String, val password: String)
data class RegisterRes(val success: String)

data class LoginReq(val email: String, val password: String)
data class LoginRes(val loginSuccess: String, val userID: String, val userName: String, val familyID: String?, val tested: Boolean)
data class LoginRes(val loginSuccess: String, val user: UserInfo)

data class AddFamilyReq(val userID: String)
data class AddFamilyRes(val success: String, val message: String, val familyID: String)
Expand All @@ -45,3 +46,6 @@ data class JoinFamilyRes(val success: String, val message: String, val familyID:

data class CompleteTestReq(val userID: String)
data class CompleteTestRes(val success: String, val message: String)

// data class
data class UserInfo(val _id: String, val name: String, val email: String, val tested: Boolean, val familyID: String, val achieve: ArrayList<Boolean>)
5 changes: 5 additions & 0 deletions SHAPEUP2022/app/src/main/res/layout/achieve_fragment_1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@


<!--아래 항목들은 추후 item_achieve.xml로 대체할 예정-->
<!--리사이클러 뷰-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/walk_search_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<LinearLayout
android:layout_width="match_parent"
Expand Down
8 changes: 3 additions & 5 deletions SHAPEUP2022/app/src/main/res/layout/activity_achieve.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
android:src="@drawable/ic_icon_dog_orange" />
<!--반려견 이름 id 추가-->
<TextView
android:id="@+id/achieve_pet_name"
android:layout_marginLeft="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -100,11 +101,8 @@
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="종합" />
android:layout_height="wrap_content"
app:tabGravity="fill">

<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
Expand Down
16 changes: 16 additions & 0 deletions SHAPEUP2022/app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,20 @@
<item>의료비</item>
</string-array>

<string-array name="achieve_progress">
<item>반려견을 입양했어요</item>
<item>우리가족의 반려견 호감도를 점검했어요</item>
<item>반려견을 쓰다듬었어요</item>
<item>투두리스트 100% 3일 달성</item>
<item>예산서에서 지출 항목을 추가했어요</item>
<item>강아지에게 사료를 줬어요</item>
<item>산책을 하고 산책 리뷰를 작성했어요</item>
<item>강아지의 장난감을 세척했어요</item>
<item>강아지의 건강 상태를 확인했어요</item>
<item>강아지에게 이름을 가르쳤어요</item>
<item>강아지 산책을 위한 산책길을 3번 탐색했어요</item>
<item>강아지가 짖는 소리를 들었어요</item>
<item>가족들의 성실도가 50% 넘었어요</item>
<item>반려견 선호도 점검을 진행했어요</item>
</string-array>
</resources>

0 comments on commit 21fbf58

Please sign in to comment.