Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

부산대 Android_이지은 4주차 2단계(리팩토링 완료) #72

Open
wants to merge 33 commits into
base: jieunyume
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a749e88
docs: README.md 작성
JieunYume Jul 16, 2024
80c47d6
refactor: SavedLocationHolder의 뷰 변수를 by lazy로 변경
JieunYume Jul 17, 2024
3abbc9b
feat: 저장된 검색어의 제목(title)을 전달하는 콜백 함수 구현
JieunYume Jul 17, 2024
b0527b5
feat: itemView에 콜백 함수 적용
JieunYume Jul 17, 2024
adca161
feat: MainActivity에서 콜백 함수 구현해서 LocationViewModel의 searchLocationsFro…
JieunYume Jul 17, 2024
6d7449b
refactor: handleNoResultMessage() 함수를 만들어서 중복 코드 제거
JieunYume Jul 17, 2024
e533b25
feat: searchEditText의 text를 저장된 검색어의 제목(title)로 설정
JieunYume Jul 17, 2024
2eb7b23
feat: searchEditText의 커서 위치 변경
JieunYume Jul 17, 2024
0b3db81
refactor: 콜백 함수 이름을 네이밍 컨벤션에 맞게 변경
JieunYume Jul 17, 2024
193fa3c
refactor: LocationLocalDataSource에서 중복 코드를 함수로 분리
JieunYume Jul 17, 2024
681b945
feat: 키워드로 장소 검색하는 api에서 x, y 값 받아오기
JieunYume Jul 17, 2024
88faf63
feat: 검색 결과 목록 중 하나의 항목을 선택하면 MapActivity로 전환되게 구현
JieunYume Jul 17, 2024
66c2ece
feat: mapView의 좌표를 검색 결과 목록 중 선택된 항목의 좌표로 설정
JieunYume Jul 17, 2024
4f44f65
feat: 검색 결과 목록 중 선택된 항목의 좌표에 KakaoMap label을 추가
JieunYume Jul 18, 2024
97a4556
feat: SharedPreferences에 마지막 좌표 저장 및 불러오기 기능 구현
JieunYume Jul 18, 2024
e1786ed
feat: onMapError() 호출 시 에러 화면 노출
JieunYume Jul 18, 2024
c3d8a4a
feat: MapActivity에서 BottomSheet를 사용하여 위치 정보 노출
JieunYume Jul 18, 2024
038d548
refactor: 뷰 변수들을 by lazy로 정의하도록 변경
JieunYume Jul 18, 2024
116e601
refactor: onMapError()에서 에러 메세지를 띄울 때 스레드를 runOnUiThread로 변경
JieunYume Jul 18, 2024
8b2cd64
refactor: 저장된 데이터가 없을 때 null 처리로 BottomSheet가 뜨지 않게 설정
JieunYume Jul 18, 2024
b99401c
refactor: onMapReady에서 함수 분리
JieunYume Jul 18, 2024
8e0f341
docs: update README.md
JieunYume Jul 19, 2024
5bb2978
feat: mockito 의존성 주입
JieunYume Jul 19, 2024
9cd5962
test: MapAcitivty UI 테스트
JieunYume Jul 19, 2024
2a94f4b
test: MainActivity UI 테스트
JieunYume Jul 19, 2024
985f813
test: LocationLocalDataSource 단위 테스트
JieunYume Jul 19, 2024
bc7d87d
test: LocationLocalDataSource에서 위치 검색 기능 삭제
JieunYume Jul 19, 2024
322ff94
refactor: Location의 lat과 lng의 자료형을 String에서 Double로 변경
JieunYume Jul 23, 2024
f5b77ec
refactor: 2주차 과제 코드 삭제
JieunYume Jul 23, 2024
b5df635
refactor: KakaoSdk의 초기화 코드를 MapActivity에서 App 클래스로 이동
JieunYume Jul 23, 2024
1e95911
refactor: onMapReady() 내에서 runOnUiThread() 삭제
JieunYume Jul 23, 2024
5472947
refactor: SharedPreference를 싱글톤으로 사용하고, MapActivity에서 ViewModel을 통해 사…
JieunYume Jul 24, 2024
af188b9
refactor: DataSource와 Repository 이름 변경
JieunYume Jul 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/src/main/java/campus/tech/kakao/map/App.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package campus.tech.kakao.map

import android.app.Application
import campus.tech.kakao.map.model.datasource.SharedPreferences
import com.kakao.vectormap.KakaoMapSdk

class App : Application(){
companion object{
lateinit var sharedPreferencesManager : SharedPreferences
}
override fun onCreate() {
sharedPreferencesManager = SharedPreferences(applicationContext)
KakaoMapSdk.init(this, BuildConfig.KAKAO_API_KEY)
super.onCreate()
KakaoMapSdk.init(this, BuildConfig.KAKAO_API_KEY);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package campus.tech.kakao.map.model.datasource

import campus.tech.kakao.map.App
import campus.tech.kakao.map.model.Location

class LastLocationLocalDataSource() {

fun putLastLocation(location: Location) {
if (location != null) {
App.sharedPreferencesManager.putString("longitude", location.longitude.toString())
App.sharedPreferencesManager.putString("latitude", location.latitude.toString())
App.sharedPreferencesManager.putString("title", location.title.toString())
App.sharedPreferencesManager.putString("address", location.address.toString())
App.sharedPreferencesManager.putString("category", location.category.toString())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분은 추후에 hilt 적용해보면서 의존성 주입받을 수 있도록 변경해보죠!

}
}

fun getLastLocation(): Location? {
val title = App.sharedPreferencesManager.getString("title", "")
if(title == "") return null
val longitude = App.sharedPreferencesManager.getString("longitude", "").toString().toDouble()
val latitude = App.sharedPreferencesManager.getString("latitude", "").toString().toDouble()
val address = App.sharedPreferencesManager.getString("address", "").toString()
val category = App.sharedPreferencesManager.getString("category", "").toString()
return Location(title, address, category, longitude, latitude)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package campus.tech.kakao.map.model.datasource

import android.content.ContentValues
import android.database.Cursor
import android.util.Log
import campus.tech.kakao.map.model.Contract.LocationEntry
import campus.tech.kakao.map.App
import campus.tech.kakao.map.model.Contract.SavedLocationEntry
import campus.tech.kakao.map.model.Location
import campus.tech.kakao.map.model.LocationDbHelper
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package campus.tech.kakao.map.model.datasource

import android.content.Context

class SharedPreferences(context: Context) {
private val prefs = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)

fun getString(key: String, defValue: String): String {
return prefs.getString(key, defValue).toString()
}

fun putString(key: String, value: String) {
prefs.edit().putString(key, value).apply()

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package campus.tech.kakao.map.model.repository

import campus.tech.kakao.map.model.Location
import campus.tech.kakao.map.model.datasource.LastLocationLocalDataSource

class LastLocationRepository(
private val lastLocationLocalDataSource: LastLocationLocalDataSource
) {
fun putLastLocation(location: Location){
lastLocationLocalDataSource.putLastLocation(location)
}

fun getLastLocation(): Location? {
return lastLocationLocalDataSource.getLastLocation()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import campus.tech.kakao.map.model.datasource.LocationLocalDataSource
import campus.tech.kakao.map.model.datasource.LocationRemoteDataSource

class LocationRepository(
private val locationRemoteRepository: LocationRemoteDataSource
private val locationRemoteDataSource: LocationRemoteDataSource
) {
suspend fun getLocationRemote(query: String): List<Location> {
return locationRemoteRepository.getLocations(query)
return locationRemoteDataSource.getLocations(query)
}
}

This file was deleted.

77 changes: 28 additions & 49 deletions app/src/main/java/campus/tech/kakao/map/view/map/MapActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package campus.tech.kakao.map.view.map

import android.app.Activity
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Color
import android.os.Bundle
import android.util.Log
Expand All @@ -11,13 +9,14 @@ import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import campus.tech.kakao.map.BuildConfig
import campus.tech.kakao.map.R
import campus.tech.kakao.map.model.Location
import campus.tech.kakao.map.model.datasource.LastLocationLocalDataSource
import campus.tech.kakao.map.model.repository.LastLocationRepository
import campus.tech.kakao.map.view.search.MainActivity
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.kakao.vectormap.KakaoMap
import com.kakao.vectormap.KakaoMapReadyCallback
import com.kakao.vectormap.KakaoMapSdk
import com.kakao.vectormap.LatLng
import com.kakao.vectormap.MapLifeCycleCallback
import com.kakao.vectormap.MapView
Expand All @@ -35,6 +34,9 @@ class MapActivity : AppCompatActivity() {
private val errorMessageTextView by lazy { findViewById<TextView>(R.id.errorMessageTextView) }
private val bottomSheetBehavior: BottomSheetBehavior<ConstraintLayout> by lazy { BottomSheetBehavior.from(bottomSheetLayout) }

private val lastLocationLocalDataSource: LastLocationLocalDataSource by lazy { LastLocationLocalDataSource() }
private val lastLocationRepository: LastLocationRepository by lazy { LastLocationRepository(lastLocationLocalDataSource) }

companion object{
private val DEFAULT_LONGITUDE = 127.115587
private val DEFAULT_LATITUDE = 37.406960
Expand Down Expand Up @@ -76,13 +78,13 @@ class MapActivity : AppCompatActivity() {
showErrorMessage(error)
}
}, object : KakaoMapReadyCallback() {
val coordinates = getCoordinates()
val location = getCoordinates()
override fun onMapReady(kakaoMap: KakaoMap) { // 인증 후 API 가 정상적으로 실행될 때 호출됨
Log.d("jieun", "onMapReady coordinates: " + coordinates.toString())
if (coordinates != null) {
showLabel(coordinates, kakaoMap)
showBottomSheet(coordinates)
setSharedData("pref", coordinates)
Log.d("jieun", "onMapReady coordinates: " + location.toString())
if (location != null) {
showLabel(location, kakaoMap)
showBottomSheet(location)
lastLocationRepository.putLastLocation(location)
// Log.d("jieun", "onMapReady setSharedData: " + getSharedData("pref"))
} else{
hideBottomSheet()
Expand All @@ -91,8 +93,8 @@ class MapActivity : AppCompatActivity() {

override fun getPosition(): LatLng {
// Log.d("jieun", "getPosition coordinates: " + coordinates.toString())
if (coordinates != null) {
return LatLng.from(coordinates.latitude, coordinates.longitude)
if (location != null) {
return LatLng.from(location.latitude, location.longitude)
} else{
return LatLng.from(DEFAULT_LATITUDE, DEFAULT_LONGITUDE)
}
Expand All @@ -110,76 +112,53 @@ class MapActivity : AppCompatActivity() {
}

private fun showLabel(
coordinates: Coordinates,
location: Location,
kakaoMap: KakaoMap
) {
val labelStyles: LabelStyles = LabelStyles.from(
LabelStyle.from(R.drawable.location_red_icon_resized).setZoomLevel(8),
LabelStyle.from(R.drawable.location_red_icon_resized)
.setTextStyles(32, Color.BLACK, 1, Color.GRAY).setZoomLevel(15)
)
val position = LatLng.from(coordinates.latitude, coordinates.longitude)
val position = LatLng.from(location.latitude, location.longitude)
kakaoMap.labelManager?.getLayer()?.addLabel(
LabelOptions.from(position)
.setStyles(labelStyles)
.setTexts(coordinates.title)
.setTexts(location.title)
)
}

private fun hideBottomSheet() {
bottomSheetLayout.visibility = View.GONE
}

private fun showBottomSheet(coordinates: Coordinates) {
private fun showBottomSheet(location: Location) {
bottomSheetLayout.visibility = View.VISIBLE
bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
bottom_sheet_title.text = coordinates.title
bottom_sheet_address.text = coordinates.address
bottom_sheet_title.text = location.title
bottom_sheet_address.text = location.address
}

private fun getCoordinates(): Coordinates? {
var coordinates = getCoordinatesByIntent()
if(coordinates == null) {
coordinates = getCoordinatedBySharedPreference("pref")
private fun getCoordinates(): Location? {
var location = getCoordinatesByIntent()
if(location == null) {
location = lastLocationRepository.getLastLocation()
}
return coordinates
return location

}

private fun getCoordinatesByIntent(): Coordinates? {
private fun getCoordinatesByIntent(): Location? {
if (intent.hasExtra("title") && intent.hasExtra("longitude")
&& intent.hasExtra("latitude") && intent.hasExtra("address")) {
val title = intent.getStringExtra("title")
val longitude = intent.getDoubleExtra("longitude", 0.0)
val latitude = intent.getDoubleExtra("latitude", 0.0)
val address = intent.getStringExtra("address").toString()
val category = intent.getStringExtra("category").toString()
if (title != null) {
return Coordinates(title, longitude, latitude, address)
return Location(title, address, category, longitude, latitude)
} else return null
} else return null
}

fun setSharedData(name: String, coordinates: Coordinates?) {
if (coordinates != null) {
var pref: SharedPreferences = getSharedPreferences(name, Activity.MODE_PRIVATE)
var editor: SharedPreferences.Editor = pref.edit()
editor.putString("longitude", coordinates.longitude.toString())
editor.putString("latitude", coordinates.latitude.toString())
editor.putString("title", coordinates.title.toString())
editor.putString("address", coordinates.address.toString())
editor.apply()
}
}

fun getCoordinatedBySharedPreference(name: String): Coordinates? {
var pref: SharedPreferences = getSharedPreferences(name, Activity.MODE_PRIVATE)
if(pref.getString("title", "") == ""){
return null
}
val title = pref.getString("title", "").toString()
val longitude = pref.getString("longitude", "").toString().toDouble()
val latitude = pref.getString("latitude", "").toString().toDouble()
val address = pref.getString("address", "").toString()
return Coordinates(title, longitude, latitude, address)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LocationAdapter(
init {
itemView.setOnClickListener {
val location = getItem(bindingAdapterPosition)
itemSelectedListener.onLocationViewClicked(location.title, location.longitude, location.latitude, location.address)
itemSelectedListener.onLocationViewClicked(location)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.recyclerview.widget.RecyclerView
import campus.tech.kakao.map.model.datasource.LocationLocalDataSource
import campus.tech.kakao.map.model.datasource.LocationRemoteDataSource
import campus.tech.kakao.map.R
import campus.tech.kakao.map.model.Location
import campus.tech.kakao.map.model.SavedLocation
import campus.tech.kakao.map.model.LocationDbHelper
import campus.tech.kakao.map.model.repository.LocationRepository
Expand Down Expand Up @@ -120,14 +121,15 @@ class MainActivity : AppCompatActivity(), OnItemSelectedListener {
savedLocationRecyclerView.adapter = savedLocationAdapter
}

override fun onLocationViewClicked(title: String, longitude: Double, latitude: Double, address:String) {
savedLocationViewModel.addSavedLocation(title)
override fun onLocationViewClicked(location: Location) {
savedLocationViewModel.addSavedLocation(location.title)

val intent = Intent(this@MainActivity, MapActivity::class.java)
intent.putExtra("title", title)
intent.putExtra("longitude", longitude)
intent.putExtra("latitude", latitude)
intent.putExtra("address", address)
intent.putExtra("title", location.title)
intent.putExtra("address", location.address)
intent.putExtra("category", location.category)
intent.putExtra("longitude", location.longitude)
intent.putExtra("latitude", location.latitude)
startActivity(intent)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package campus.tech.kakao.map.view.search

import campus.tech.kakao.map.model.Location
import campus.tech.kakao.map.model.SavedLocation

interface OnItemSelectedListener {
fun onLocationViewClicked(title: String, longitude: Double, latitude: Double, address:String)
fun onLocationViewClicked(location: Location)
fun onSavedLocationXButtonClicked(item: SavedLocation)
fun onSavedLocationViewClicked(title: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ class LocationViewModel(
handleNoResultMessage(getSearchedLocationsSize())
}
}

}