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주차 step2 과제 #74

Open
wants to merge 18 commits into
base: leeghy
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# android-map-search
# android-map-location

0단계 ✅
- 코드 복사

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

2단계
- 테스트코드 작성
47 changes: 47 additions & 0 deletions app/src/androidTest/java/campus/tech/kakao/map/MainActivityTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package campus.tech.kakao.map

import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import campus.tech.kakao.map.view.MainActivity
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@LargeTest
class MainActivityTest {
@Test
fun testUIElementsAreDisplayed() {
ActivityScenario.launch(MainActivity::class.java).use {
onView(ViewMatchers.withId(R.id.inputText))
onView(ViewMatchers.withId(R.id.cancelBtn))
onView(ViewMatchers.withId(R.id.recyclerView))
onView(ViewMatchers.withId(R.id.searchView))
}
}

@Test
fun testCancelButtonClearsInputText() {
ActivityScenario.launch(MainActivity::class.java).use {
onView(withId(R.id.inputText)).perform(typeText("Test Input"))
onView(withId(R.id.cancelBtn)).perform(click())
onView(withId(R.id.inputText)).check(matches(withText("")))
}
}

@Test
fun testSearchFunctionality() {
ActivityScenario.launch(MainActivity::class.java).use {
onView(withId(R.id.inputText)).perform(typeText("Test Location"))
onView(withId(R.id.resultView)).check(matches(isDisplayed()))
}
}
}
65 changes: 65 additions & 0 deletions app/src/androidTest/java/campus/tech/kakao/map/MapActivityTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package campus.tech.kakao.map

import android.content.Intent
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import campus.tech.kakao.map.Model.LocationData
import campus.tech.kakao.map.view.MapActivity
import com.google.gson.Gson
import com.kakao.vectormap.MapView
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@LargeTest
class MapActivityTest {
private lateinit var selectedLocation: LocationData

@Before
fun setUp() {
selectedLocation = LocationData("Test Location", "시청", "시청", 37.566, 126.978)
}

@Test
fun testMapViewDisplayed() {
val intent = Intent(ApplicationProvider.getApplicationContext(), MapActivity::class.java).apply {
putExtra("selectedLocation", Gson().toJson(selectedLocation))
}
ActivityScenario.launch<MapActivity>(intent).use {
onView(ViewMatchers.withId(R.id.map_view))
}
}

@Test
fun testInputTextDisplayed() {
ActivityScenario.launch(MapActivity::class.java).use {
onView(ViewMatchers.withId(R.id.MapinputText))
}
}

@Test
fun testRecyclerViewDisplayed() {
ActivityScenario.launch(MapActivity::class.java).use {
onView(ViewMatchers.withId(R.id.recyclerView))
}
}

@Test
fun testMapViewInitializedWithSelectedLocation() {
val intent = Intent(ApplicationProvider.getApplicationContext(), MapActivity::class.java).apply {
putExtra("selectedLocation", Gson().toJson(selectedLocation))
}
ActivityScenario.launch<MapActivity>(intent).use { scenario ->
scenario.onActivity { activity ->
val mapView = activity.findViewById<MapView>(R.id.map_view)
assertNotNull(mapView)
}
}
}
}
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
android:name=".Model.MyApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -15,8 +16,10 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Map"
android:name=".Model.MyApplication"
tools:targetApi="31">
<activity
android:name=".view.ErrorActivity"
android:exported="false" />
<activity
android:name=".view.MainActivity"
android:exported="false" />
Expand All @@ -32,7 +35,7 @@

<meta-data
android:name="com.kakao.vectormap.AppKey"
android:value="b6f4b70a2090ca9cfca380ec1ebc9c5f"/>
android:value="b6f4b70a2090ca9cfca380ec1ebc9c5f" />
</application>

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

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import campus.tech.kakao.map.Model.LocationData
import campus.tech.kakao.map.R

class LocationAdapter(
private val locationList: List<LocationData>,
private val onItemViewClick: (LocationData) -> Unit
) : RecyclerView.Adapter<LocationAdapter.ViewHolder>() {

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val nameTextView: TextView = itemView.findViewById(R.id.name)
val locationTextView: TextView = itemView.findViewById(R.id.location)
val categoryTextView: TextView = itemView.findViewById(R.id.category)

init {
itemView.setOnClickListener {
val position = adapterPosition
if (position != RecyclerView.NO_POSITION) {
onItemViewClick(locationList[position])
}
}
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.item_view, parent, false)
return ViewHolder(view)
}

override fun getItemCount(): Int = locationList.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val location = locationList[position]
holder.nameTextView.text = location.name
holder.locationTextView.text = location.location
holder.categoryTextView.text = location.category

holder.itemView.setOnClickListener {
onItemViewClick(location)
}
}
}
39 changes: 39 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/Adapter/MapViewAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package campus.tech.kakao.map.Adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import campus.tech.kakao.map.Model.LocationData
import campus.tech.kakao.map.R

class MapViewAdapter(
private var information: List<LocationData>,
) : RecyclerView.Adapter<MapViewAdapter.ViewHolder>() {

fun updateData(newInformation: List<LocationData>) {
information = newInformation
notifyDataSetChanged()
}
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val nameTextView: TextView = itemView.findViewById(R.id.place_name)
val locationTextView: TextView = itemView.findViewById(R.id.place_address)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.item_bottom_sheet, parent, false)
return ViewHolder(view)
}

override fun getItemCount(): Int = information.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val info = information[position]
holder.nameTextView.text = info.name
holder.locationTextView.text = info.location
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package campus.tech.kakao.map.Adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import campus.tech.kakao.map.Model.LocationData
import campus.tech.kakao.map.R

class SearchViewAdapter(
private val searchList: ArrayList<LocationData>,
private val onRemoveClick: (LocationData) -> Unit, // 콜백 함수
) : RecyclerView.Adapter<SearchViewAdapter.ViewHolder>() {

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val cancelBtn: Button = view.findViewById(R.id.searchViewCancelBtn)
val nameText: TextView = view.findViewById(R.id.searchViewName)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.search_item_view, parent, false)
return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = searchList[position]
holder.nameText.text = item.name
holder.cancelBtn.setOnClickListener {
onRemoveClick(item)
}
}

override fun getItemCount() = searchList.size
}
15 changes: 12 additions & 3 deletions app/src/main/java/campus/tech/kakao/map/Model/LocationData.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package campus.tech.kakao.map.Model

data class LocationData (
import com.kakao.vectormap.LatLng

data class LocationData(
val name: String,
val location: String,
val category: String
)
val category: String,
val latitude: Double,
val longitude: Double
) {
fun getLatLng(): LatLng {
return LatLng.from(latitude, longitude)
}
}

2 changes: 2 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/Model/SearchResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ data class Place(
val place_name: String,
val address_name: String,
val category_group_name: String,
val x: String, // longitude
val y: String // latitude
)
22 changes: 22 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/view/ErrorActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package campus.tech.kakao.map.view

import android.os.Bundle
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import campus.tech.kakao.map.R

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

val errorMessage = intent.getStringExtra("errorMessage")

val errortxt: TextView = findViewById(R.id.error_text)
errortxt.text = errorMessage ?: "Unknown error occurred"
}
}
Loading