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 이혜원 step2 코드 #95

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# android-map-location
## 카카오맵 API 심화
## step1

## 과제 진행 요구 사항

-[ ] 저장된 검색어를 선택하면 해당 검색어의 검색 결과가 표시된다.
-[ ] 검색 결과 목록 중 하나의 항목을 선택하면 해당 항목의 위치를 지도에 표시한다.
-[ ] 앱 종료 시 마지막 위치를 저장하여 다시 앱 실행 시 해당 위치로 포커스 한다.
-[ ] 카카오지도 onMapError() 호출 시 에러 화면을 보여준다.

## 프로그래밍 요구 사항
-[ ] BottomSheet를 사용한다.
-[ ] 카카오 API 사용을 위한 앱 키를 외부에 노출하지 않는다.
-[ ] 가능한 MVVM 아키텍처 패턴을 적용하도록 한다.
25 changes: 15 additions & 10 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
import java.util.Properties

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
Expand All @@ -15,6 +18,8 @@ android {
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
buildConfigField("String", "KAKAO_REST_API_KEY", "\"1cc77fe7fb4a4c7747a562f762962ce9\"")

}

buildTypes {
Expand All @@ -36,6 +41,7 @@ android {

buildFeatures {
viewBinding = true
buildConfig = true
}
}

Expand All @@ -46,18 +52,17 @@ dependencies {
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.recyclerview:recyclerview:1.3.2")
implementation("androidx.datastore:datastore-preferences:1.0.0")
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("com.squareup.retrofit2:converter-gson:2.11.0")
implementation("com.kakao.maps.open:android:2.9.5")
implementation("com.kakao.maps.open:android:2.11.1")
implementation("androidx.activity:activity:1.8.0")
implementation("androidx.test:core-ktx:1.5.0")
testImplementation ("org.mockito:mockito-core:3.11.2")
testImplementation("junit:junit:4.13.2")
testImplementation("io.mockk:mockk-android:1.13.11")
testImplementation("io.mockk:mockk-agent:1.13.11")
testImplementation("androidx.arch.core:core-testing:2.2.0")
testImplementation("org.robolectric:robolectric:4.11.1")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation("androidx.test:rules:1.5.0")
androidTestImplementation("androidx.test.espresso:espresso-intents:3.5.1")
androidTestImplementation ("androidx.test.espresso:espresso-intents:3.5.1")
androidTestImplementation ("androidx.test:rules:1.5.0")
androidTestImplementation ("androidx.test.ext:junit:1.1.2")
androidTestImplementation ("androidx.test.ext:junit:1.1.5")
androidTestImplementation ("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation ("androidx.test:runner:1.5.2")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package campus.tech.kakao.map

import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.action.ViewActions.closeSoftKeyboard
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class MainActivityUITest {

@get:Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java)

@Test
fun testSearchTextCleared() {
onView(withId(R.id.SearchText)).perform(typeText("Test"), closeSoftKeyboard())

onView(withId(R.id.Delete)).perform(click())

onView(withId(R.id.SearchText)).check(matches(withText("")))
}

@Test
fun testRecyclerViewVisibility() {
onView(withId(R.id.SearchText)).perform(typeText(""), closeSoftKeyboard())
onView(withId(R.id.NoSearchText)).check(matches(withText("검색 결과가 없습니다.")))
onView(withId(R.id.RecyclerView)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE)))

onView(withId(R.id.SearchText)).perform(typeText("Test"), closeSoftKeyboard())
onView(withId(R.id.NoSearchText)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE)))
onView(withId(R.id.RecyclerView)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
}

@Test
fun testInsertHistory() {

onView(withId(R.id.SearchText)).perform(typeText("Seoul"), closeSoftKeyboard())
onView(withId(R.id.SearchText)).perform(click())

}
}
22 changes: 20 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
android:name=".MyApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -13,14 +19,26 @@
android:theme="@style/Theme.Map"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:name=".ErrorActivity"
android:exported="false" />
<activity
android:name=".MapActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<meta-data
android:name="com.kakao.vectormap.APP_KEY"
android:value="bfa0e5ab308b39ef7e373921d5a5e697" />

<activity
android:name=".MainActivity"
android:exported="true"
android:usesCleartextTraffic="true"></activity>
</application>

</manifest>
</manifest>
27 changes: 27 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/DBHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package campus.tech.kakao.map

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

class DBHelper(
context: Context?,
name: String?,
factory: SQLiteDatabase.CursorFactory?,
version: Int
) : SQLiteOpenHelper(context, name, factory, version) {


override fun onCreate(db: SQLiteDatabase?) {
val CREATE_TABLE = "CREATE TABLE ${PlaceEntry.TABLE_NAME} (" +
"${PlaceEntry.COLUMN_NAME} TEXT, " +
"${PlaceEntry.COLUMN_ADDRESS} TEXT, " +
"${PlaceEntry.COLUMN_CATEGORY} TEXT)"
db?.execSQL(CREATE_TABLE)
}

override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db?.execSQL("DROP TABLE IF EXISTS ${PlaceEntry.TABLE_NAME}")
onCreate(db)
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/ErrorActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package campus.tech.kakao.map
import android.content.Intent
import android.os.Bundle
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat

class ErrorActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_error)

// Edge-to-Edge 화면 지원 설정
WindowCompat.setDecorFitsSystemWindows(window, false)

val retryButton: ImageButton = findViewById(R.id.robot)

retryButton.setOnClickListener {
// 다시 MapActivity로 이동
val intent = Intent(this, MapActivity::class.java)
startActivity(intent)
finish() // 현재 액티비티 종료
}

}
}
29 changes: 29 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/HistoryDBHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package campus.tech.kakao.map

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

class HistoryDBHelper(
context: Context?,
name: String?,
factory: SQLiteDatabase.CursorFactory?,
version: Int
) : SQLiteOpenHelper(context, name, factory, version) {


override fun onCreate(db: SQLiteDatabase?) {
val CREATE_TABLE = "CREATE TABLE ${HistoryEntry.TABLE_NAME} (" +
"${HistoryEntry.COLUMN_NAME} TEXT," +
"${HistoryEntry.COLUMN_X} TEXT," +
"${HistoryEntry.COLUMN_Y} TEXT," +
"${HistoryEntry.COLUMN_ADDRESS} TEXT" +
")"
db?.execSQL(CREATE_TABLE)
}

override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db?.execSQL("DROP TABLE IF EXISTS ${PlaceEntry.TABLE_NAME}")
onCreate(db)
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/HistoryEntry.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package campus.tech.kakao.map

object HistoryEntry {
const val TABLE_NAME = "History"
const val COLUMN_NAME = "name"
const val COLUMN_X = "x"
const val COLUMN_Y = "y"
const val COLUMN_ADDRESS = "address"
}
62 changes: 62 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/HorRecycleAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package campus.tech.kakao.map

import android.database.Cursor
import android.provider.Settings.Global.getString
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import campus.tech.kakao.map.databinding.HoritemRecyclerviewBinding


class HorRecycleAdapter(private val onClick : (String, String, String, String) -> Unit, private val onDeleteClick: (String) -> Unit ) : RecyclerView.Adapter<HorRecycleAdapter.Holder>() {


private var cursor:Cursor? = null
Copy link

Choose a reason for hiding this comment

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

Adapter에 DB의 데이터를 쿼리해오는 Cursor가 직접 들어오는건 좋은구조가 아니라고 생각해요.


inner class Holder(private val binding: HoritemRecyclerviewBinding) :
RecyclerView.ViewHolder(binding.root) {

fun bind(name: String, x: String, y: String, address: String) {
binding.Name.text = name

// 전체 항목(root)을 클릭했을 때의 동작
binding.root.setOnClickListener {
onClick(name, x, y,address)
}

// Delete 버튼을 클릭했을 때의 동작
binding.Delete.setOnClickListener {
onDeleteClick(name)
}
}

}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HorRecycleAdapter.Holder {
val binding = HoritemRecyclerviewBinding.inflate(LayoutInflater.from(parent.context))
return Holder(binding)
}

override fun getItemCount(): Int {
return cursor?.count ?: 0
}

override fun onBindViewHolder(holder: HorRecycleAdapter.Holder, position: Int) {
cursor?.apply {
moveToPosition(position)
val name = getString(getColumnIndexOrThrow(HistoryEntry.COLUMN_NAME))
val x = getString(getColumnIndexOrThrow(HistoryEntry.COLUMN_X))
val y = getString(getColumnIndexOrThrow(HistoryEntry.COLUMN_Y))
val address = getString(getColumnIndexOrThrow(HistoryEntry.COLUMN_ADDRESS))
holder.bind(name, x, y,address)
}
}

fun SubmitCursor(Cursor : Cursor?){
cursor = Cursor
notifyDataSetChanged()

}
}
14 changes: 14 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/KakaoAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package campus.tech.kakao.map

import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Query

interface KakaoAPI {
@GET("v2/local/search/keyword.json")
fun getSearchKeyword(
@Header("Authorization") key: String,
@Query ("query") query: String
): Call<ResultSearch>
}
9 changes: 9 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/ListLayout.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package campus.tech.kakao.map

class ListLayout(
val name: String,
val address: String,
val category: String,
val x : String,
val y : String
)
Loading