diff --git a/app/src/main/java/com/example/romanticyeojido/network/map/PinInterface.kt b/app/src/main/java/com/example/romanticyeojido/network/map/PinInterface.kt index 4b61a28..597dfb2 100644 --- a/app/src/main/java/com/example/romanticyeojido/network/map/PinInterface.kt +++ b/app/src/main/java/com/example/romanticyeojido/network/map/PinInterface.kt @@ -11,4 +11,5 @@ interface PinInterface { fun getLocations( @Path("userId") userId: Int, ): Call + } \ No newline at end of file diff --git a/app/src/main/java/com/example/romanticyeojido/network/map/PinResponse.kt b/app/src/main/java/com/example/romanticyeojido/network/map/PinResponse.kt index cd729ed..5d57162 100644 --- a/app/src/main/java/com/example/romanticyeojido/network/map/PinResponse.kt +++ b/app/src/main/java/com/example/romanticyeojido/network/map/PinResponse.kt @@ -3,7 +3,7 @@ package com.example.romanticyeojido.network.map data class PinResponse( val success: Boolean, - val locations: List + val result: List ) data class LocationData( diff --git a/app/src/main/java/com/example/romanticyeojido/network/map/PopupInterface.kt b/app/src/main/java/com/example/romanticyeojido/network/map/PopupInterface.kt index fa0c7e8..f6d6760 100644 --- a/app/src/main/java/com/example/romanticyeojido/network/map/PopupInterface.kt +++ b/app/src/main/java/com/example/romanticyeojido/network/map/PopupInterface.kt @@ -9,8 +9,6 @@ import retrofit2.http.Query interface PopupInterface { @GET("/api/v1/users/{userId}/map/memory") // 요청 경로 fun getPopupData( - @Header("Authorization") accessToken: String, // Authorization 헤더를 요청에 추가 - @Header("Content-Type") contentType: String = "application/json", @Path("userId") userId: Int, @Query("latitude") latitude: String, @Query("longitude") longitude: String diff --git a/app/src/main/java/com/example/romanticyeojido/network/map/PopupResponse.kt b/app/src/main/java/com/example/romanticyeojido/network/map/PopupResponse.kt index 88c06e6..7416332 100644 --- a/app/src/main/java/com/example/romanticyeojido/network/map/PopupResponse.kt +++ b/app/src/main/java/com/example/romanticyeojido/network/map/PopupResponse.kt @@ -3,7 +3,7 @@ package com.example.romanticyeojido.network.map data class PopupResponse( val success: Boolean, - val memory: MemoryData + val result: MemoryData ) data class MemoryData( diff --git a/app/src/main/java/com/example/romanticyeojido/network/memoryPost/MemoryInterface.kt b/app/src/main/java/com/example/romanticyeojido/network/memoryPost/MemoryInterface.kt index 8cf5ab2..55bc273 100644 --- a/app/src/main/java/com/example/romanticyeojido/network/memoryPost/MemoryInterface.kt +++ b/app/src/main/java/com/example/romanticyeojido/network/memoryPost/MemoryInterface.kt @@ -4,6 +4,7 @@ import com.example.romanticyeojido.network.map.PopupResponse import okhttp3.MultipartBody import okhttp3.ResponseBody import retrofit2.Call +import retrofit2.Response import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.Multipart @@ -38,4 +39,12 @@ interface ImageService { @Path("memoryId") memoryId : Int, @Part images: List ): Call +} + +interface MemoryContentInterface { + @GET("/api/v1/users/{userId}/locations/{locationId}/memory-content") + suspend fun getMemoryContent( + @Path("userId") userId: Int, + @Path("locationId") locationId: Int + ): Response } \ No newline at end of file diff --git a/app/src/main/java/com/example/romanticyeojido/network/memoryPost/MemoryService.kt b/app/src/main/java/com/example/romanticyeojido/network/memoryPost/MemoryService.kt index aa690ac..8a37e4c 100644 --- a/app/src/main/java/com/example/romanticyeojido/network/memoryPost/MemoryService.kt +++ b/app/src/main/java/com/example/romanticyeojido/network/memoryPost/MemoryService.kt @@ -25,4 +25,27 @@ data class MemoryResult( @SerializedName(value = "friends") val friends: String, @SerializedName(value = "content") val content: String, @SerializedName(value = "summary") val summary: String +) + +data class MemoryContentResponse( + val result: MemoryContent +) + +data class MemoryContent( + val id: Int, + val user_id: Int, + val location_id: Int, + val title: String, + val visit_date: String, + val friends: String, + val content: String, + val summary: String, + val is_deleted: Int, + val created_at: String, + val updated_at: String +) + +data class ErrorResponse( + val success: Boolean, + val message: String ) \ No newline at end of file diff --git a/app/src/main/java/com/example/romanticyeojido/ui/map/MapActivity.kt b/app/src/main/java/com/example/romanticyeojido/ui/map/MapActivity.kt index 8288215..e47ed22 100644 --- a/app/src/main/java/com/example/romanticyeojido/ui/map/MapActivity.kt +++ b/app/src/main/java/com/example/romanticyeojido/ui/map/MapActivity.kt @@ -1,11 +1,13 @@ package com.example.romanticyeojido.ui.map +import android.content.Context.MODE_PRIVATE import android.content.Intent import android.os.Bundle import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat +import androidx.lifecycle.lifecycleScope import com.example.romanticyeojido.R import com.example.romanticyeojido.databinding.ActivityMapBinding import com.example.romanticyeojido.network.map.PinInterface @@ -14,6 +16,9 @@ import com.example.romanticyeojido.network.map.NewpinInterface import com.example.romanticyeojido.network.map.NewpinRequest import com.example.romanticyeojido.network.map.NewpinResponse import com.example.romanticyeojido.network.RetrofitClient +import com.example.romanticyeojido.network.memoryPost.MemoryContentInterface +import com.example.romanticyeojido.network.memoryPost.MemoryInterface +import com.example.romanticyeojido.network.memoryPost.MemoryService import com.example.romanticyeojido.ui.memoryPost.MemoryPostActivity import com.kakao.vectormap.KakaoMap import com.kakao.vectormap.KakaoMapReadyCallback @@ -25,6 +30,7 @@ import com.kakao.vectormap.label.LabelLayer import com.kakao.vectormap.label.LabelOptions import com.kakao.vectormap.label.LabelStyle import com.kakao.vectormap.label.LabelStyles +import kotlinx.coroutines.launch import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -60,7 +66,7 @@ class MapActivity: AppCompatActivity() { sendNewPin(userId,latitude,longitude) // 서버에 핀 정보 전송 val intent = Intent(this, MemoryPostActivity::class.java).apply{} - + onPause() startActivity(intent) clearUnsavedLabel() // 라벨 초기화 } ?: Toast.makeText(this, "핀을 선택해주세요!", Toast.LENGTH_SHORT).show() @@ -71,20 +77,20 @@ class MapActivity: AppCompatActivity() { } // val locationId = intent.getIntExtra("locationId", 0) - // val title = intent.getStringExtra("title") - // val visit_date = intent.getStringExtra("visit_date") - // val content = intent.getStringExtra("content") - // val friends = intent.getStringExtra("friends") - // val summary = intent.getStringExtra("summary") - - // Log.d("MapActivity", "받은 memory_data: $locationId, $title, $visit_date, $content, $friends, $summary") - - initializeMap() +// val title = intent.getStringExtra("title") +// val visit_date = intent.getStringExtra("visit_date") +// val content = intent.getStringExtra("content") +// val friends = intent.getStringExtra("friends") +// val summary = intent.getStringExtra("summary") +// +// Log.d("MapActivity", "받은 추억 데이터: $title, $visit_date, $content, $friends, $summary") + + initializeMap(userId) fetchLocations(userId) } //API 호출 - 저장된 핀 호출 - private fun fetchLocations(userId: Int) { + fun fetchLocations(userId: Int) { val call = pinInterface.getLocations(userId) call.enqueue(object : Callback { @@ -93,44 +99,35 @@ class MapActivity: AppCompatActivity() { val pinResponse = response.body() if (pinResponse != null && pinResponse.success) { // locations가 null이 아닌지 확인 - if (pinResponse.locations != null) { - pinResponse.locations.forEachIndexed { index, location -> + if (pinResponse.result != null) { + pinResponse.result.forEachIndexed { index, location -> Log.d("Location", "위치 $index: 위도: ${location.latitude}, 경도: ${location.longitude}") - - val styles = kakaoMap?.labelManager?.addLabelStyles( - LabelStyles.from( - LabelStyle.from(R.drawable.ic_pin_gray).setAnchorPoint(0.5f, 1.0f) - ) - ) - // 지도에 위치 추가 - val options = LabelOptions.from(LatLng.from(location.latitude, location.longitude)).setStyles(styles) - val layer = kakaoMap?.labelManager?.layer - - if (layer != null) { - val label = layer.addLabel(options) - label.show() + if (location.latitude == null || location.longitude == null) { + Log.e("pinResponse", "위도/경도 값이 null입니다.") } } } else { - Log.d("API", "위치 정보가 없습니다.") + Log.d("pinResponse", "위치 정보가 없습니다.") } } else { - Log.d("API", "응답이 성공적이지 않음") + Log.d("pinResponse", "응답이 성공적이지 않음") } } else { - Log.e("API", "응답 실패: ${response.code()}") + Log.e("pinResponse", "응답 실패: ${response.code()}") } } override fun onFailure(call: Call, t: Throwable) { - Log.e("API", "API 호출 실패", t) + Log.e("pinResponse", "API 호출 실패", t) } }) } - private fun initializeMap() { + //지도 설정 함수 + private fun initializeMap(userId: Int) { binding.mapView.start(object : MapLifeCycleCallback() { override fun onMapDestroy() { + Log.d("onMapDestroy", "onMapDestroy") // 지도 API 종료 시 호출 } @@ -141,6 +138,7 @@ class MapActivity: AppCompatActivity() { override fun onMapReady(map: KakaoMap) { kakaoMap = map setupMap() // 지도 설정 + fetchLocations(userId) } override fun getPosition(): LatLng { @@ -154,9 +152,12 @@ class MapActivity: AppCompatActivity() { override fun isVisible(): Boolean { return true // 지도 표시 여부 } + }) } + + // 지도 시작 설정 private fun setupMap() { val position = LatLng.from(37.44983420181418,127.12727640486801) @@ -173,6 +174,7 @@ class MapActivity: AppCompatActivity() { private fun addLabel(position: LatLng) { // 기존 임시 라벨 제거 unsavedLabel?.remove() + val styles = kakaoMap?.labelManager?.addLabelStyles( LabelStyles.from( LabelStyle.from(R.drawable.ic_pin).setAnchorPoint(0.5f, 1.0f) @@ -183,8 +185,11 @@ class MapActivity: AppCompatActivity() { val layer = kakaoMap?.labelManager?.layer if (layer != null) { + // 새로운 라벨 추가 val label = layer.addLabel(options) label.show() + + // 라벨을 임시 라벨로 설정 unsavedLabel = label // 새로 생성된 라벨을 임시 라벨로 설정 latitude = label.position.latitude.toString() @@ -194,10 +199,13 @@ class MapActivity: AppCompatActivity() { binding.btnRegister.isEnabled = true binding.btnRegister.setBackgroundColor(ContextCompat.getColor(this, R.color.P500)) + // 핀 클릭 리스너 kakaoMap?.setOnLabelClickListener { map, labelLayer, clickedLabel -> initLabelClickListener(map, labelLayer, clickedLabel) true } + + // 위치 정보 저장 val spfLoc = getSharedPreferences("map_location", MODE_PRIVATE) val editor = spfLoc.edit() editor.putString("latitude", label.position.latitude.toString()) @@ -213,6 +221,11 @@ class MapActivity: AppCompatActivity() { val spf = getSharedPreferences("UserPrefs", MODE_PRIVATE) val userId = spf.getInt("USER_ID", 0) + val locationIdSpf = getSharedPreferences("MapPreferences", MODE_PRIVATE) + val locationId = locationIdSpf.getInt("locationId", 0) + + fetchMemoryContent(userId, locationId) + val popupActivity = PopupActivity(this) popupActivity.showPopup(binding.root, userId) { } @@ -264,10 +277,81 @@ class MapActivity: AppCompatActivity() { override fun onResume() { super.onResume() binding.mapView.resume() // MapView resume + Log.d("MapLog", "Map_resume") + + // 사용자 ID 가져오기 + val spf = getSharedPreferences("UserPrefs", MODE_PRIVATE) + val userId = spf.getInt("USER_ID", 0) + Log.d("MapActivity", "USER_ID in MainActivity: $userId") + + // 이전에 저장한 좌표 + val spfLoc = getSharedPreferences("map_location", MODE_PRIVATE) + val latitude = spfLoc.getString("latitude", null) + val longitude = spfLoc.getString("longitude", null) + +// val memoryspf = getSharedPreferences("MemoryPrefs", MODE_PRIVATE) +// val title = memoryspf.getString("title", "") +// val visitDate = memoryspf.getString("visit_date", "") +// val friends = memoryspf.getString("friends", "") +// val content = memoryspf.getString("content", "") +// val summary = memoryspf.getString("summary", "") +// +// // 추억 데이터 로그 확인 +// Log.d("MapActivity", "추억 데이터: title=$title, visit_date=$visitDate, friends=$friends, content=$content, summary=$summary") + + // 위치가 저장되어 있으면 그곳에 핀 고정 + if (latitude != null && longitude != null) { + val position = LatLng.from(latitude.toDouble(), longitude.toDouble()) + // 기존 핀 고정 (회색 핀으로) + val styles = kakaoMap?.labelManager?.addLabelStyles( + LabelStyles.from( + LabelStyle.from(R.drawable.ic_pin_gray).setAnchorPoint(0.5f, 1.0f) + ) + ) + + val options = LabelOptions.from(position).setStyles(styles) + val layer = kakaoMap?.labelManager?.layer + + if (layer != null) { + val fixedLabel = layer.addLabel(options) + fixedLabel.show() + } + } + + // 위치 목록 가져오기 + fetchLocations(userId) } + override fun onPause() { super.onPause() binding.mapView.pause() // MapView pause + Log.d("MapLog", "Map_pause") } + + private fun fetchMemoryContent(userId: Int, locationId: Int) { + // Create a Retrofit instance and API service + val apiService = RetrofitClient.instance.create(MemoryContentInterface::class.java) + + // Make the API call + lifecycleScope.launch { + try { + val response = apiService.getMemoryContent(userId, locationId) + if (response.isSuccessful) { + val memoryContent = response.body()?.result + if (memoryContent != null) { + // Memory data fetched successfully, handle it here + Log.d("MemoryData", "Memory Title: ${memoryContent.title}") + // You can display it in a Toast or another UI element + Toast.makeText(this@MapActivity, "Memory loaded: ${memoryContent.title}", Toast.LENGTH_SHORT).show() + } + } else { + Log.e("MemoryData", "Error fetching memory data: ${response.code()}") + } + } catch (e: Exception) { + Log.e("MemoryData", "API call failed", e) + } + } + } + } \ No newline at end of file diff --git a/app/src/main/java/com/example/romanticyeojido/ui/map/PopupActivity.kt b/app/src/main/java/com/example/romanticyeojido/ui/map/PopupActivity.kt index bb9e8ca..ef15020 100644 --- a/app/src/main/java/com/example/romanticyeojido/ui/map/PopupActivity.kt +++ b/app/src/main/java/com/example/romanticyeojido/ui/map/PopupActivity.kt @@ -51,18 +51,18 @@ class PopupActivity (private val context: Context) { val apiService = RetrofitClient.instance.create(PopupInterface::class.java) val accessToken = AccessTokenManager.getAccessToken() ?: "" - apiService.getPopupData(accessToken,"application/json",userId,latitude,longitude).enqueue(object : Callback { + apiService.getPopupData(userId,latitude,longitude).enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { if (response.isSuccessful && response.body()?.success == true) { - response.body()?.memory?.let { memory -> + response.body()?.result?.let { result -> // 성공 로그 추가 - Log.d("PopupData", "데이터를 성공적으로 불러왔습니다: ${memory.title}, 방문일: ${memory.visit_date}, 친구: ${memory.friends}") + Log.d("PopupData", "데이터를 성공적으로 불러왔습니다: ${result.title}, 방문일: ${result.visit_date}, 친구: ${result.friends}") - binding.tvPopupTitle.text = memory.title - binding.tvPopupDate.text = "방문일: ${memory.visit_date} / 친구: ${memory.friends}" - binding.tvPopupDescription.text = memory.gpt_summary + binding.tvPopupTitle.text = result.title + binding.tvPopupDate.text = "방문일: ${result.visit_date} / 친구: ${result.friends}" + binding.tvPopupDescription.text = result.gpt_summary Glide.with(binding.imgPopup.context) - .load(RetrofitClient.BASE_URL + memory.image_url) + .load(RetrofitClient.BASE_URL + result.image_url) .into(binding.imgPopup) } } else { diff --git a/app/src/main/java/com/example/romanticyeojido/ui/memoryPost/MemoryPostActivity.kt b/app/src/main/java/com/example/romanticyeojido/ui/memoryPost/MemoryPostActivity.kt index 483da0a..5593894 100644 --- a/app/src/main/java/com/example/romanticyeojido/ui/memoryPost/MemoryPostActivity.kt +++ b/app/src/main/java/com/example/romanticyeojido/ui/memoryPost/MemoryPostActivity.kt @@ -11,7 +11,6 @@ import android.widget.AdapterView import android.widget.ImageView import android.widget.LinearLayout import androidx.appcompat.app.AppCompatActivity -import androidx.core.app.ActivityCompat.startActivityForResult import com.example.romanticyeojido.R import com.example.romanticyeojido.databinding.ActivityMemoryPostBinding import com.example.romanticyeojido.network.RetrofitClient @@ -56,8 +55,8 @@ class MemoryPostActivity: AppCompatActivity() { // lng = intent.getDoubleExtra("lng", 0.0) val spf = getSharedPreferences("map_location", MODE_PRIVATE) - val lat = spf.getString("lat", "") - val lng = spf.getString("lng", "") + val lat = spf.getString("latitude", "") + val lng = spf.getString("longitude", "") Log.d("MemoryPostActivity", "lat: $lat") Log.d("MemoryPostActivity", "lng: $lng") @@ -153,6 +152,14 @@ class MemoryPostActivity: AppCompatActivity() { validateInputs() if (binding.postRegisterBtn.isEnabled == true) { postMemory() +// val intent = Intent(this, MapActivity::class.java).apply{} +// startActivity(intent) +// finish() + + // 맵 액티비티로 돌아가기 전에 데이터 저장을 완료한 후 + val intent = Intent(this, MapActivity::class.java) + intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT // 기존 액티비티를 재활성화 + startActivity(intent) finish() } } @@ -334,9 +341,9 @@ class MemoryPostActivity: AppCompatActivity() { val friends = binding.postPeopleEt.text.toString() val content = binding.postContentEt.text.toString() - //memoryId 선언 - val memoryId = UUID.randomUUID().toString() - Log.d("MemoryPostActivity", "memoryId: ${memoryId}") +// //memoryId 선언 +// val memoryId = UUID.randomUUID().toString() +// Log.d("MemoryPostActivity", "memoryId: ${memoryId}") val memoryRequest = MemoryRequest( title = title, @@ -351,16 +358,19 @@ class MemoryPostActivity: AppCompatActivity() { if (response.isSuccessful) { val result = response.body()?.result Log.d("MemoryPostActivity", "추억 등록 성공: $result") - // 성공 알림 또는 화면 이동 처리 - - // 추억 등록 후, 맵 액티비티로 locationId 전송 - val intent = Intent(this@MemoryPostActivity, MapActivity::class.java) - intent.putExtra("locationId", locationId) // locationId 전달 -// intent.putExtra("title", response.body()?.result?.title) -// intent.putExtra("visit_date", response.body()?.result?.visit_date) -// intent.putExtra("content", response.body()?.result?.content) -// intent.putExtra("friends", response.body()?.result?.friends) -// intent.putExtra("summary", response.body()?.result?.summary) + + // SharedPreferences에 추억 데이터 저장 + val spf = getSharedPreferences("MemoryPrefs", MODE_PRIVATE) + val editor = spf.edit() + + // 추억 등록 결과를 SharedPreferences에 저장 + editor.putString("title", result?.title) + editor.putString("visit_date", result?.visit_date) + editor.putString("friends", result?.friends) + editor.putString("content", result?.content) + editor.putString("summary", result?.summary) + editor.apply() + } else { Log.e("MemoryPostActivity", "추억 등록 실패: ${response.errorBody()?.string()}") }