From 9933255d6015610d962cffd54a8c0a1aecad4194 Mon Sep 17 00:00:00 2001 From: otcroz Date: Mon, 12 Sep 2022 17:43:28 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B0=95=EC=95=84=EC=A7=80=20=EC=9E=85?= =?UTF-8?q?=EC=96=91=20=EB=9D=BC=EC=9A=B0=ED=84=B0=20=EC=97=B0=EA=B2=B0,?= =?UTF-8?q?=20petID=20preference=EC=97=90=20=EC=A0=80=EC=9E=A5=20=EB=B0=8F?= =?UTF-8?q?=20=EA=B4=80=EB=A6=AC=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shape_up_2022/auth/LoginActivity.kt | 31 ++++- .../retrofit/NetworkServiceFamily.kt | 17 +-- .../simulation/SimStartNamingActivity.kt | 108 ++++++++++++++++-- 3 files changed, 140 insertions(+), 16 deletions(-) diff --git a/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/auth/LoginActivity.kt b/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/auth/LoginActivity.kt index c06d278..2585c9a 100644 --- a/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/auth/LoginActivity.kt +++ b/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/auth/LoginActivity.kt @@ -13,6 +13,7 @@ import com.example.shape_up_2022.retrofit.LoginRes import com.example.shape_up_2022.retrofit.MyApplication import com.example.shape_up_2022.common.SaveSharedPreference import com.example.shape_up_2022.databinding.SignInBinding +import com.example.shape_up_2022.retrofit.GetPetIDRes import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -56,6 +57,11 @@ class LoginActivity : AppCompatActivity() { SaveSharedPreference.setFamliyID(baseContext, response.body()!!.familyID?:null) SaveSharedPreference.setUserTested(baseContext, response.body()!!.tested) + // petID 프리퍼런스에 저장 + if(response.body()!!.familyID!! != null){ + callGetPetID(response.body()!!.familyID!!) + } + // 메인 페이지로 이동 val intent = Intent(baseContext, MainActivity::class.java) startActivity(intent) @@ -75,7 +81,30 @@ class LoginActivity : AppCompatActivity() { }) } - //간편 로그인 연동 => api + } + + private fun callGetPetID(familyID: String){ + // DB에 저장된 아이디 값 + val call: Call = MyApplication.networkServiceFamily.getPetID( + familyID = familyID + ) + + call?.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if(response.isSuccessful){ + Log.d("mobileApp", "$response ${response.body()}") + if(response.body()!!.success){ + SaveSharedPreference.setPetID(baseContext, response.body()!!.petID) + } + + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.d("mobileApp", "onFailure $t") + Toast.makeText(baseContext, "네트워크 오류 발생", Toast.LENGTH_SHORT).show() + } + }) } diff --git a/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/retrofit/NetworkServiceFamily.kt b/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/retrofit/NetworkServiceFamily.kt index 55ca8f2..4e289b1 100644 --- a/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/retrofit/NetworkServiceFamily.kt +++ b/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/retrofit/NetworkServiceFamily.kt @@ -3,9 +3,7 @@ package com.example.shape_up_2022.retrofit import com.example.shape_up_2022.data.Family import com.example.shape_up_2022.data.User import retrofit2.Call -import retrofit2.http.Body -import retrofit2.http.POST -import retrofit2.http.PUT +import retrofit2.http.* interface NetworkServiceFamily { @POST("getFamily") @@ -14,12 +12,15 @@ interface NetworkServiceFamily { ): Call - @PUT("additionPetId") + @PUT("additionPetID") fun additionPetId( @Body user: AdditionPetIdReq, ): Call - + @GET("getPetID/{familyID}") + fun getPetID( + @Path("familyID") familyID: String + ): Call } @@ -27,5 +28,7 @@ data class GetFamilyReq(val familyID: String) data class GetFamilyRes(val success: String, val family: FamilyBody, val familyCode: String) data class FamilyBody(val familyCode: String, val userGroup: Array) -data class AdditionPetIdReq(val familyID: String) -data class AdditionPetIdRes(val success: String) \ No newline at end of file +data class AdditionPetIdReq(val familyID: String, val petName: String) +data class AdditionPetIdRes(val success: Boolean) + +data class GetPetIDRes(val success: Boolean, val petID: String) diff --git a/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/simulation/SimStartNamingActivity.kt b/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/simulation/SimStartNamingActivity.kt index f70d236..24faf76 100644 --- a/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/simulation/SimStartNamingActivity.kt +++ b/SHAPEUP2022/app/src/main/java/com/example/shape_up_2022/simulation/SimStartNamingActivity.kt @@ -1,17 +1,30 @@ package com.example.shape_up_2022.simulation +import android.content.DialogInterface import android.content.Intent import android.net.Uri import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.util.Log import android.view.View +import android.widget.Toast +import androidx.appcompat.app.AlertDialog +import com.example.shape_up_2022.common.MainActivity +import com.example.shape_up_2022.common.SaveSharedPreference import com.example.shape_up_2022.databinding.ActivitySimStartNamingBinding +import com.example.shape_up_2022.retrofit.* import com.example.shape_up_2022.todo.TodoActivity +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response class SimStartNamingActivity : AppCompatActivity() { + + lateinit var binding : ActivitySimStartNamingBinding + var petName = "" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val binding = ActivitySimStartNamingBinding.inflate(layoutInflater) + binding = ActivitySimStartNamingBinding.inflate(layoutInflater) setContentView(binding.root) binding.nameStep1.visibility = View.VISIBLE @@ -33,24 +46,103 @@ class SimStartNamingActivity : AppCompatActivity() { binding.nameStep4.visibility = View.GONE } - binding.btnNamingFinish.setOnClickListener { - binding.nameStep1.visibility = View.GONE - binding.nameStep2.visibility = View.GONE - binding.nameStep3.visibility = View.GONE - binding.nameStep4.visibility = View.VISIBLE - } - binding.btnRegisterInfoLink.setOnClickListener { val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.animal.go.kr/front/community/show.do;jsessionid=Lg5YgpnbEoP5liudNsFMSicezdue41UcTvIrz0zd46ptK48F7PERl09jE3i0g3a8.aniwas2_servlet_front?boardId=contents&seq=66&menuNo=2000000016")) startActivity(browserIntent) } binding.nameBtn4.setOnClickListener { + // To-do 페이지로 이동 val intent = Intent(this, TodoActivity::class.java) startActivity(intent) finish() } + /* 강아지 입양 및 등록 */ + + + // 사용자에게 강아지 이름 확인: alert 창 + val eventHandler = object : DialogInterface.OnClickListener { + override fun onClick(p0: DialogInterface?, p1: Int) { + if(p1== DialogInterface.BUTTON_POSITIVE) { + // 강아지 입양 및 이름 등록 + postRegisterPet(petName) + + binding.nameStep1.visibility = View.GONE + binding.nameStep2.visibility = View.GONE + binding.nameStep3.visibility = View.GONE + binding.nameStep4.visibility = View.VISIBLE + } + } + } + + + binding.btnNamingFinish.setOnClickListener { + petName = binding.inputPetName.text.toString() + Log.d("mobileApp", petName) + + var builder = AlertDialog.Builder(this) + .setTitle("강아지 이름 결정") + .setMessage("반려견의 이름을 ${petName}(으)로 지을건가요? 강아지의 이름은 변경할 수 없으니 신중하게 생각해주세요.") + .setPositiveButton("네!", eventHandler) + .setNegativeButton("아니요", null) + .setCancelable(false) + + builder.show() + } + } + + // call + private fun postRegisterPet(petName: String){ + val familyID = SaveSharedPreference.getFamliyID(this)!! + + // DB에 저장된 아이디 값 + val call: Call = MyApplication.networkServiceFamily.additionPetId( + AdditionPetIdReq(familyID, petName) + ) + + call?.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if(response.isSuccessful){ + Log.d("mobileApp", "$response ${response.body()}") + if(response.body()!!.success){ + Toast.makeText(baseContext, "강아지 이름 짓기 성공!", Toast.LENGTH_SHORT).show() + + // Preference에 petID 저장 + Log.d("mobileApp", "${SaveSharedPreference.getFamliyID(baseContext)!!}") + callGetPetID(SaveSharedPreference.getFamliyID(baseContext)!!) + } + else Toast.makeText(baseContext, "강아지 이름 짓기를 실패했습니다.", Toast.LENGTH_SHORT).show() + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.d("mobileApp", "onFailure $t") + Toast.makeText(baseContext, "네트워크 오류 발생! 강아지를 입양하지 못했습니다.", Toast.LENGTH_SHORT).show() + } + }) + } + + private fun callGetPetID(familyID: String){ + // DB에 저장된 아이디 값 + val call: Call = MyApplication.networkServiceFamily.getPetID( + familyID = familyID + ) + + call?.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if(response.isSuccessful){ + Log.d("mobileApp", "$response ${response.body()}") + Log.d("mobileApp", "$response ${response.body()!!.petID}") + // Preference에 PetID 저장 + SaveSharedPreference.setPetID(baseContext, response.body()!!.petID) + } + } + override fun onFailure(call: Call, t: Throwable) { + Log.d("mobileApp", "onFailure $t") + Toast.makeText(baseContext, "네트워크 오류 발생", Toast.LENGTH_SHORT).show() + } + }) } } \ No newline at end of file