Skip to content

Commit

Permalink
강아지 입양 라우터 연결, petID preference에 저장 및 관리 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
otcroz committed Sep 12, 2022
1 parent a416778 commit 9933255
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -75,7 +81,30 @@ class LoginActivity : AppCompatActivity() {
})

}
//간편 로그인 연동 => api
}

private fun callGetPetID(familyID: String){
// DB에 저장된 아이디 값
val call: Call<GetPetIDRes> = MyApplication.networkServiceFamily.getPetID(
familyID = familyID
)

call?.enqueue(object : Callback<GetPetIDRes> {
override fun onResponse(call: Call<GetPetIDRes>, response: Response<GetPetIDRes>) {
if(response.isSuccessful){
Log.d("mobileApp", "$response ${response.body()}")
if(response.body()!!.success){
SaveSharedPreference.setPetID(baseContext, response.body()!!.petID)
}

}
}

override fun onFailure(call: Call<GetPetIDRes>, t: Throwable) {
Log.d("mobileApp", "onFailure $t")
Toast.makeText(baseContext, "네트워크 오류 발생", Toast.LENGTH_SHORT).show()
}
})
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -14,18 +12,23 @@ interface NetworkServiceFamily {
): Call<GetFamilyRes>


@PUT("additionPetId")
@PUT("additionPetID")
fun additionPetId(
@Body user: AdditionPetIdReq,
): Call<AdditionPetIdRes>


@GET("getPetID/{familyID}")
fun getPetID(
@Path("familyID") familyID: String
): Call<GetPetIDRes>
}


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<User>)

data class AdditionPetIdReq(val familyID: String)
data class AdditionPetIdRes(val success: String)
data class AdditionPetIdReq(val familyID: String, val petName: String)
data class AdditionPetIdRes(val success: Boolean)

data class GetPetIDRes(val success: Boolean, val petID: String)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<AdditionPetIdRes> = MyApplication.networkServiceFamily.additionPetId(
AdditionPetIdReq(familyID, petName)
)

call?.enqueue(object : Callback<AdditionPetIdRes> {
override fun onResponse(call: Call<AdditionPetIdRes>, response: Response<AdditionPetIdRes>) {
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<AdditionPetIdRes>, t: Throwable) {
Log.d("mobileApp", "onFailure $t")
Toast.makeText(baseContext, "네트워크 오류 발생! 강아지를 입양하지 못했습니다.", Toast.LENGTH_SHORT).show()
}
})
}

private fun callGetPetID(familyID: String){
// DB에 저장된 아이디 값
val call: Call<GetPetIDRes> = MyApplication.networkServiceFamily.getPetID(
familyID = familyID
)

call?.enqueue(object : Callback<GetPetIDRes> {
override fun onResponse(call: Call<GetPetIDRes>, response: Response<GetPetIDRes>) {
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<GetPetIDRes>, t: Throwable) {
Log.d("mobileApp", "onFailure $t")
Toast.makeText(baseContext, "네트워크 오류 발생", Toast.LENGTH_SHORT).show()
}
})
}
}

0 comments on commit 9933255

Please sign in to comment.