From 21b34d9043dd65b5274eab4fe2786982286c8e33 Mon Sep 17 00:00:00 2001 From: sumin Date: Mon, 1 Jul 2024 14:56:28 +0900 Subject: [PATCH 01/17] =?UTF-8?q?docs:=20README.md=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index df2a6477..c4800a85 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ # android-map-keyword + +## 1단계 +- [ ] 기본 레이아웃 구현 +- [ ] 로컬 데이터베이스 구현 \ No newline at end of file From 67847f85664f4d147f9baacc68030df8797a1568 Mon Sep 17 00:00:00 2001 From: sumin Date: Mon, 1 Jul 2024 15:29:51 +0900 Subject: [PATCH 02/17] =?UTF-8?q?feat:=20=EA=B8=B0=EB=B3=B8=20=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=95=84=EC=9B=83=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- app/src/main/res/drawable/clear_24.xml | 5 +++++ app/src/main/res/layout/activity_main.xml | 26 +++++++++++++++++++++-- app/src/main/res/values/strings.xml | 3 +++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/drawable/clear_24.xml diff --git a/README.md b/README.md index c4800a85..2415b823 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # android-map-keyword ## 1단계 -- [ ] 기본 레이아웃 구현 +- [x] 기본 레이아웃 구현 - [ ] 로컬 데이터베이스 구현 \ No newline at end of file diff --git a/app/src/main/res/drawable/clear_24.xml b/app/src/main/res/drawable/clear_24.xml new file mode 100644 index 00000000..f8ca0c64 --- /dev/null +++ b/app/src/main/res/drawable/clear_24.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 24d17df2..ce582653 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,13 +7,35 @@ android:layout_height="match_parent" tools:context=".MainActivity"> + + + app:layout_constraintTop_toBottomOf="@id/search_box" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e5ba5b9c..0c8b69f2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,6 @@ Map + 검색 결과가 없습니다. + 검색어를 입력해 주세요. + 삭제 \ No newline at end of file From 99fe3618bd2abd1591d3031f0f1b2b03637068c1 Mon Sep 17 00:00:00 2001 From: sumin Date: Wed, 3 Jul 2024 12:54:34 +0900 Subject: [PATCH 03/17] =?UTF-8?q?feat:=20=EC=B4=88=EA=B8=B0=20=EB=A1=9C?= =?UTF-8?q?=EC=BB=AC=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=B2=A0=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../java/campus/tech/kakao/map/Location.kt | 14 +++++ .../campus/tech/kakao/map/MainActivity.kt | 3 ++ .../java/campus/tech/kakao/map/MapContract.kt | 12 +++++ .../java/campus/tech/kakao/map/MapDbHelper.kt | 51 +++++++++++++++++++ .../java/campus/tech/kakao/map/MapModel.kt | 45 ++++++++++++++++ .../campus/tech/kakao/map/MapViewModel.kt | 18 +++++++ 7 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/campus/tech/kakao/map/Location.kt create mode 100644 app/src/main/java/campus/tech/kakao/map/MapContract.kt create mode 100644 app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt create mode 100644 app/src/main/java/campus/tech/kakao/map/MapModel.kt create mode 100644 app/src/main/java/campus/tech/kakao/map/MapViewModel.kt diff --git a/README.md b/README.md index 2415b823..1bbaabde 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ ## 1단계 - [x] 기본 레이아웃 구현 -- [ ] 로컬 데이터베이스 구현 \ No newline at end of file +- [x] 로컬 데이터베이스 구현 \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/Location.kt b/app/src/main/java/campus/tech/kakao/map/Location.kt new file mode 100644 index 00000000..0d893493 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/Location.kt @@ -0,0 +1,14 @@ +package campus.tech.kakao.map + +data class Location( + val name: String, + val category: String, + val address: String +) { + companion object { + const val CAFE: String = "카페" + const val PHARMACY: String = "약국" + const val RESTAURANT: String = "식당" + const val NORMAL: String = "일반" + } +} \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt index 95b43803..68d55e3a 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -1,5 +1,6 @@ package campus.tech.kakao.map +import android.content.ContentValues import android.os.Bundle import androidx.appcompat.app.AppCompatActivity @@ -7,5 +8,7 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + + val viewModel = MapViewModel(this) } } diff --git a/app/src/main/java/campus/tech/kakao/map/MapContract.kt b/app/src/main/java/campus/tech/kakao/map/MapContract.kt new file mode 100644 index 00000000..eb27b7cf --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/MapContract.kt @@ -0,0 +1,12 @@ +package campus.tech.kakao.map + +import android.provider.BaseColumns + +object MapContract { + object MapEntry : BaseColumns { + const val TABLE_NAME = "map" + const val COLUMN_NAME_NAME = "name" + const val COLUMN_NAME_CATEGORY = "category" + const val COLUMN_NAME_ADDRESS = "address" + } +} \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt b/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt new file mode 100644 index 00000000..73f7bf02 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt @@ -0,0 +1,51 @@ +package campus.tech.kakao.map + +import android.content.ContentValues +import android.content.Context +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper +import android.provider.BaseColumns +import android.util.Log + +class MapDbHelper(mContext: Context) : SQLiteOpenHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION) { + override fun onCreate(db: SQLiteDatabase?) { + db?.execSQL(SQL_CREATE_ENTRIES) + initializeDb(db) + } + + override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { + db?.execSQL(SQL_DELETE_ENTRIES) + onCreate(db) + } + + private fun initializeDb(db: SQLiteDatabase?) { + for (idx in 1..10) { + val exampleCafeValue = ContentValues() + exampleCafeValue.put(MapContract.MapEntry.COLUMN_NAME_NAME, "카페$idx") + exampleCafeValue.put(MapContract.MapEntry.COLUMN_NAME_CATEGORY, Location.CAFE) + exampleCafeValue.put(MapContract.MapEntry.COLUMN_NAME_ADDRESS, "서울 성동구 성수동 $idx") + db?.insert(MapContract.MapEntry.TABLE_NAME, null, exampleCafeValue) + + val examplePharValue = ContentValues() + examplePharValue.put(MapContract.MapEntry.COLUMN_NAME_NAME, "약국$idx") + examplePharValue.put(MapContract.MapEntry.COLUMN_NAME_CATEGORY, Location.PHARMACY) + examplePharValue.put(MapContract.MapEntry.COLUMN_NAME_ADDRESS, "서울 성동구 성수동 $idx") + db?.insert(MapContract.MapEntry.TABLE_NAME, null, examplePharValue) + } + } + + companion object { + const val DATABASE_NAME = "map.db" + const val DATABASE_VERSION = 1 + + private const val SQL_CREATE_ENTRIES = + "CREATE TABLE ${MapContract.MapEntry.TABLE_NAME} (" + + "${BaseColumns._ID} INTEGER PRIMARY KEY AUTOINCREMENT," + + "${MapContract.MapEntry.COLUMN_NAME_NAME} TEXT," + + "${MapContract.MapEntry.COLUMN_NAME_CATEGORY} TEXT," + + "${MapContract.MapEntry.COLUMN_NAME_ADDRESS} TEXT" + + ");" + private const val SQL_DELETE_ENTRIES = + "DROP TABLE IF EXISTS ${MapContract.MapEntry.TABLE_NAME}" + } +} \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MapModel.kt b/app/src/main/java/campus/tech/kakao/map/MapModel.kt new file mode 100644 index 00000000..c0cc5155 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/MapModel.kt @@ -0,0 +1,45 @@ +package campus.tech.kakao.map + +import android.content.ContentValues +import android.content.Context +import android.util.Log + +class MapModel(mContext: Context) { + private val helper: MapDbHelper = MapDbHelper(mContext) + + init { + helper.readableDatabase + } + + fun insertLocation(location: Location) { + val writableDb = helper.writableDatabase + val content = ContentValues() + content.put(MapContract.MapEntry.COLUMN_NAME_NAME, location.name) + content.put(MapContract.MapEntry.COLUMN_NAME_CATEGORY, location.category) + content.put(MapContract.MapEntry.COLUMN_NAME_ADDRESS, location.address) + + writableDb.insert(MapContract.MapEntry.TABLE_NAME, null, content) + } + fun getAllLocation() : List { + val readableDb = helper.readableDatabase + val cursor = readableDb.query( + MapContract.MapEntry.TABLE_NAME, + null, + null, + null, + null, + null, + null + ) + val res = mutableListOf() + while (cursor.moveToNext()) { + val name = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_NAME)) + val category = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_CATEGORY)) + val address = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_ADDRESS)) + + res.add(Location(name, category, address)) + } + cursor.close() + return res + } +} \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt new file mode 100644 index 00000000..7fe671ca --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt @@ -0,0 +1,18 @@ +package campus.tech.kakao.map + +import android.content.Context +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel + +class MapViewModel(mContext: Context) : ViewModel() { + private val model = MapModel(mContext) + + fun insertLocation(location: Location) { + model.insertLocation(location) + } + + fun getAllLocation(): List { + return model.getAllLocation() + } +} \ No newline at end of file From de54a468ff45a1820bfdf586f8536409e2b861ca Mon Sep 17 00:00:00 2001 From: sumin Date: Wed, 3 Jul 2024 14:07:45 +0900 Subject: [PATCH 04/17] =?UTF-8?q?docs:=20README.md=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1bbaabde..06951057 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,7 @@ ## 1단계 - [x] 기본 레이아웃 구현 -- [x] 로컬 데이터베이스 구현 \ No newline at end of file +- [x] 로컬 데이터베이스 구현 +- [ ] 검색 결과 표시 레이아웃 구현 + - [ ] 결과 목록 레이아웃 + - [ ] 결과 아이템 레이아웃 \ No newline at end of file From b6d5fbe2c3cbe857f3eaef2a7627be710f5fe1a6 Mon Sep 17 00:00:00 2001 From: sumin Date: Wed, 3 Jul 2024 14:12:36 +0900 Subject: [PATCH 05/17] =?UTF-8?q?feat:=20=EA=B2=80=EC=83=89=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=EB=A5=BC=20=ED=91=9C=EC=8B=9C=ED=95=A0=20RecyclerView?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/res/layout/activity_main.xml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ce582653..06cb4595 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -38,4 +38,13 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/search_box" /> + + From 0183943bcffa34d9682fdcaf65af7165c8b29c2b Mon Sep 17 00:00:00 2001 From: sumin Date: Wed, 3 Jul 2024 14:27:30 +0900 Subject: [PATCH 06/17] =?UTF-8?q?feat:=20=EA=B2=B0=EA=B3=BC=EC=97=90=20?= =?UTF-8?q?=ED=91=9C=EC=8B=9C=EB=90=A0=20=EC=95=84=EC=9D=B4=ED=85=9C=20?= =?UTF-8?q?=EB=A0=88=EC=9D=B4=EC=95=84=EC=9B=83=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +-- app/src/main/res/drawable/location_24.xml | 5 +++ .../main/res/layout/item_search_result.xml | 40 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/drawable/location_24.xml create mode 100644 app/src/main/res/layout/item_search_result.xml diff --git a/README.md b/README.md index 06951057..c0c8ca44 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,6 @@ ## 1단계 - [x] 기본 레이아웃 구현 - [x] 로컬 데이터베이스 구현 -- [ ] 검색 결과 표시 레이아웃 구현 - - [ ] 결과 목록 레이아웃 - - [ ] 결과 아이템 레이아웃 \ No newline at end of file +- [x] 검색 결과 표시 레이아웃 구현 + - [x] 결과 목록 레이아웃 + - [x] 결과 아이템 레이아웃 \ No newline at end of file diff --git a/app/src/main/res/drawable/location_24.xml b/app/src/main/res/drawable/location_24.xml new file mode 100644 index 00000000..d7f69831 --- /dev/null +++ b/app/src/main/res/drawable/location_24.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/layout/item_search_result.xml b/app/src/main/res/layout/item_search_result.xml new file mode 100644 index 00000000..c5e7fa17 --- /dev/null +++ b/app/src/main/res/layout/item_search_result.xml @@ -0,0 +1,40 @@ + + + + + + + \ No newline at end of file From 844f3591fe62eef437c4cc6d578b9e0509f07f4e Mon Sep 17 00:00:00 2001 From: sumin Date: Wed, 3 Jul 2024 14:54:29 +0900 Subject: [PATCH 07/17] =?UTF-8?q?docs:=20README.md=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit step2 기능구현 정리 --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c0c8ca44..8dfa5ae1 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,13 @@ - [x] 로컬 데이터베이스 구현 - [x] 검색 결과 표시 레이아웃 구현 - [x] 결과 목록 레이아웃 - - [x] 결과 아이템 레이아웃 \ No newline at end of file + - [x] 결과 아이템 레이아웃 + +## 2단계 +- [ ] 검색어를 입력하면 검색 결과 목록 표시 +- [ ] 검색 결과 목록 스크롤 가능 +- [ ] 입력한 검색어 X 버튼을 통해 삭제 가능 +- [ ] 검색 결과에서 항목 선택 시 검색어 저장 목록에 추가 +- [ ] 저장된 검색어 가로 스크롤 가능 +- [ ] 저장된 검색어 X 버튼을 통해 삭제 가능 +- [ ] 저장된 검색어 DB에 저장 \ No newline at end of file From 5b9082504a0bb62995e74d713e933dee330b346f Mon Sep 17 00:00:00 2001 From: sumin Date: Wed, 3 Jul 2024 23:43:14 +0900 Subject: [PATCH 08/17] =?UTF-8?q?feat:=20=EA=B2=80=EC=83=89=EC=96=B4?= =?UTF-8?q?=EB=A5=BC=20=EC=9E=85=EB=A0=A5=ED=96=88=EC=9D=84=20=EB=95=8C=20?= =?UTF-8?q?=EA=B2=80=EC=83=89=20=EA=B2=B0=EA=B3=BC=20=ED=91=9C=EC=8B=9C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RecyclerView를 사용했기 때문에 스크롤기능까지 추가됨 --- README.md | 4 +- app/src/main/AndroidManifest.xml | 1 + .../campus/tech/kakao/map/MainActivity.kt | 36 ++++++++++++++++- .../java/campus/tech/kakao/map/MapModel.kt | 40 ++++++++++++++++--- .../tech/kakao/map/MapRecyclerAdapter.kt | 36 +++++++++++++++++ .../campus/tech/kakao/map/MapViewModel.kt | 3 ++ .../main/res/layout/item_search_result.xml | 4 +- 7 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt diff --git a/README.md b/README.md index 8dfa5ae1..03034a05 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ - [x] 결과 아이템 레이아웃 ## 2단계 -- [ ] 검색어를 입력하면 검색 결과 목록 표시 -- [ ] 검색 결과 목록 스크롤 가능 +- [x] 검색어를 입력하면 검색 결과 목록 표시 +- [x] 검색 결과 목록 스크롤 가능 - [ ] 입력한 검색어 X 버튼을 통해 삭제 가능 - [ ] 검색 결과에서 항목 선택 시 검색어 저장 목록에 추가 - [ ] 저장된 검색어 가로 스크롤 가능 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6bca2f54..9f4da0b8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,6 +14,7 @@ tools:targetApi="31"> diff --git a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt index 68d55e3a..2ba75112 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -2,13 +2,47 @@ package campus.tech.kakao.map import android.content.ContentValues import android.os.Bundle +import android.util.Log +import android.widget.EditText +import android.widget.TextView import androidx.appcompat.app.AppCompatActivity +import androidx.core.view.isVisible +import androidx.core.widget.doAfterTextChanged +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView class MainActivity : AppCompatActivity() { + private lateinit var viewModel: MapViewModel + private lateinit var searchBox: EditText + private lateinit var searchResultView: RecyclerView + private lateinit var message: TextView + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - val viewModel = MapViewModel(this) + viewModel = MapViewModel(this) + searchBox = findViewById(R.id.search_box) + searchResultView = findViewById(R.id.search_result) + message = findViewById(R.id.message) + + searchBox.doAfterTextChanged { + it?.let { + search(it.toString(), false) + } + } + } + + fun search(locName: String, isExactMatch: Boolean) { + val searchResult = viewModel.searchLocation(locName, isExactMatch) + searchResultView.adapter = MapRecyclerAdapter(searchResult, layoutInflater, this@MainActivity) + if (searchResult.isNotEmpty() && locName.isNotEmpty()) { + searchResultView.isVisible = true + message.isVisible = false + } else { + searchResultView.isVisible = false + message.isVisible = true + } + searchResultView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false) } } diff --git a/app/src/main/java/campus/tech/kakao/map/MapModel.kt b/app/src/main/java/campus/tech/kakao/map/MapModel.kt index c0cc5155..9ae67a6c 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapModel.kt @@ -2,6 +2,7 @@ package campus.tech.kakao.map import android.content.ContentValues import android.content.Context +import android.database.Cursor import android.util.Log class MapModel(mContext: Context) { @@ -20,6 +21,30 @@ class MapModel(mContext: Context) { writableDb.insert(MapContract.MapEntry.TABLE_NAME, null, content) } + + fun searchLocation(locName: String, isExactMatch: Boolean): List { + val readableDb = helper.readableDatabase + + val selection = "${MapContract.MapEntry.COLUMN_NAME_NAME} LIKE ?" + val selectionArgs = arrayOf("%${locName}%") + val cursor = readableDb.query( + MapContract.MapEntry.TABLE_NAME, + null, + selection, + selectionArgs, + null, + null, + null + ) + + val res = mutableListOf() + while (cursor.moveToNext()) { + res.add(createLocationFromData(cursor)) + } + cursor.close() + return res + } + fun getAllLocation() : List { val readableDb = helper.readableDatabase val cursor = readableDb.query( @@ -31,15 +56,20 @@ class MapModel(mContext: Context) { null, null ) + val res = mutableListOf() while (cursor.moveToNext()) { - val name = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_NAME)) - val category = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_CATEGORY)) - val address = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_ADDRESS)) - - res.add(Location(name, category, address)) + res.add(createLocationFromData(cursor)) } cursor.close() return res } + + private fun createLocationFromData(cursor: Cursor): Location { + val name = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_NAME)) + val category = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_CATEGORY)) + val address = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_ADDRESS)) + + return Location(name, category, address) + } } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt b/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt new file mode 100644 index 00000000..ede615f3 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt @@ -0,0 +1,36 @@ +package campus.tech.kakao.map + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.ViewHolder + +class MapRecyclerAdapter( + val locationList: List, + val layoutInflater: LayoutInflater, + val mContext: Context +) : RecyclerView.Adapter() { + inner class MapViewHolder(itemView: View) : ViewHolder(itemView) { + val name: TextView = itemView.findViewById(R.id.location_name) + val category: TextView = itemView.findViewById(R.id.location_category) + val address: TextView = itemView.findViewById(R.id.location_address) + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MapViewHolder { + val view = layoutInflater.inflate(R.layout.item_search_result, parent, false) + return MapViewHolder(view) + } + + override fun getItemCount(): Int { + return locationList.size + } + + override fun onBindViewHolder(holder: MapViewHolder, position: Int) { + holder.name.text = locationList[position].name + holder.category.text = locationList[position].category + holder.address.text = locationList[position].address + } +} \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt index 7fe671ca..5a26860d 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt @@ -12,6 +12,9 @@ class MapViewModel(mContext: Context) : ViewModel() { model.insertLocation(location) } + fun searchLocation(locName: String, isExactMatch: Boolean): List { + return model.searchLocation(locName, isExactMatch) + } fun getAllLocation(): List { return model.getAllLocation() } diff --git a/app/src/main/res/layout/item_search_result.xml b/app/src/main/res/layout/item_search_result.xml index c5e7fa17..39fc6cf5 100644 --- a/app/src/main/res/layout/item_search_result.xml +++ b/app/src/main/res/layout/item_search_result.xml @@ -16,16 +16,18 @@ android:id="@+id/location_name" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:textSize="14sp" + android:textSize="16sp" android:textColor="@color/black" android:gravity="center_vertical" app:layout_constraintStart_toEndOf="@id/location_image" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/location_address"/> From ce89e3e9d4b31bdf31d87d1fc886c9626ac1bd18 Mon Sep 17 00:00:00 2001 From: sumin Date: Wed, 3 Jul 2024 23:59:53 +0900 Subject: [PATCH 09/17] =?UTF-8?q?feat:=20=EA=B2=80=EC=83=89=EC=96=B4?= =?UTF-8?q?=EB=A5=BC=20=EC=A7=80=EC=9A=B0=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- app/src/main/java/campus/tech/kakao/map/MainActivity.kt | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 03034a05..9f0a1142 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ## 2단계 - [x] 검색어를 입력하면 검색 결과 목록 표시 - [x] 검색 결과 목록 스크롤 가능 -- [ ] 입력한 검색어 X 버튼을 통해 삭제 가능 +- [x] 입력한 검색어 X 버튼을 통해 삭제 가능 - [ ] 검색 결과에서 항목 선택 시 검색어 저장 목록에 추가 - [ ] 저장된 검색어 가로 스크롤 가능 - [ ] 저장된 검색어 X 버튼을 통해 삭제 가능 diff --git a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt index 2ba75112..e26c23b9 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -4,6 +4,7 @@ import android.content.ContentValues import android.os.Bundle import android.util.Log import android.widget.EditText +import android.widget.ImageButton import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.core.view.isVisible @@ -16,6 +17,7 @@ class MainActivity : AppCompatActivity() { private lateinit var searchBox: EditText private lateinit var searchResultView: RecyclerView private lateinit var message: TextView + private lateinit var clear: ImageButton override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -25,12 +27,17 @@ class MainActivity : AppCompatActivity() { searchBox = findViewById(R.id.search_box) searchResultView = findViewById(R.id.search_result) message = findViewById(R.id.message) + clear = findViewById(R.id.clear) searchBox.doAfterTextChanged { it?.let { search(it.toString(), false) } } + + clear.setOnClickListener { + searchBox.text.clear() + } } fun search(locName: String, isExactMatch: Boolean) { From 3794349bec7dff6583432bce90fb901bd8081b60 Mon Sep 17 00:00:00 2001 From: sumin Date: Thu, 4 Jul 2024 15:15:41 +0900 Subject: [PATCH 10/17] =?UTF-8?q?feat:=20=EA=B2=80=EC=83=89=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=EC=9D=84=20DB=EC=97=90=20=EC=A0=80=EC=9E=A5=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../tech/kakao/map/HistoryRecyclerAdapter.kt | 30 +++++++++++++++++++ .../campus/tech/kakao/map/MainActivity.kt | 9 +++++- .../java/campus/tech/kakao/map/MapContract.kt | 1 + .../java/campus/tech/kakao/map/MapDbHelper.kt | 12 ++++++++ .../java/campus/tech/kakao/map/MapModel.kt | 8 +++++ .../tech/kakao/map/MapRecyclerAdapter.kt | 12 +++++++- .../campus/tech/kakao/map/MapViewModel.kt | 4 +++ app/src/main/res/layout/activity_main.xml | 10 ++++++- .../main/res/layout/item_search_history.xml | 28 +++++++++++++++++ 10 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt create mode 100644 app/src/main/res/layout/item_search_history.xml diff --git a/README.md b/README.md index 9f0a1142..8be62b99 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,4 @@ - [ ] 검색 결과에서 항목 선택 시 검색어 저장 목록에 추가 - [ ] 저장된 검색어 가로 스크롤 가능 - [ ] 저장된 검색어 X 버튼을 통해 삭제 가능 -- [ ] 저장된 검색어 DB에 저장 \ No newline at end of file +- [x] 저장된 검색어 DB에 저장 \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt b/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt new file mode 100644 index 00000000..95a5ee3e --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt @@ -0,0 +1,30 @@ +package campus.tech.kakao.map + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.ViewHolder + +class HistoryRecyclerAdapter( + val history: List, + val layoutInflater: LayoutInflater +) : RecyclerView.Adapter() { + inner class HistoryViewHolder(itemView: View) : ViewHolder(itemView) { + val name: TextView = itemView.findViewById(R.id.history_name) + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryViewHolder { + val view = layoutInflater.inflate(R.layout.item_search_history, parent, false) + return HistoryViewHolder(view) + } + + override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) { + holder.name.text = history[position] + } + + override fun getItemCount(): Int { + return history.size + } +} \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt index e26c23b9..40b4af6d 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -15,6 +15,7 @@ import androidx.recyclerview.widget.RecyclerView class MainActivity : AppCompatActivity() { private lateinit var viewModel: MapViewModel private lateinit var searchBox: EditText + private lateinit var searchHistoryView: RecyclerView private lateinit var searchResultView: RecyclerView private lateinit var message: TextView private lateinit var clear: ImageButton @@ -25,6 +26,7 @@ class MainActivity : AppCompatActivity() { viewModel = MapViewModel(this) searchBox = findViewById(R.id.search_box) + searchHistoryView = findViewById(R.id.search_history) searchResultView = findViewById(R.id.search_result) message = findViewById(R.id.message) clear = findViewById(R.id.clear) @@ -38,11 +40,12 @@ class MainActivity : AppCompatActivity() { clear.setOnClickListener { searchBox.text.clear() } + } fun search(locName: String, isExactMatch: Boolean) { val searchResult = viewModel.searchLocation(locName, isExactMatch) - searchResultView.adapter = MapRecyclerAdapter(searchResult, layoutInflater, this@MainActivity) + searchResultView.adapter = MapRecyclerAdapter(searchResult, layoutInflater, this@MainActivity, ::writeHistory) if (searchResult.isNotEmpty() && locName.isNotEmpty()) { searchResultView.isVisible = true message.isVisible = false @@ -52,4 +55,8 @@ class MainActivity : AppCompatActivity() { } searchResultView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false) } + fun writeHistory(name: String): Unit { + viewModel.writeHistory(name) + } + } diff --git a/app/src/main/java/campus/tech/kakao/map/MapContract.kt b/app/src/main/java/campus/tech/kakao/map/MapContract.kt index eb27b7cf..dd2a18c2 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapContract.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapContract.kt @@ -5,6 +5,7 @@ import android.provider.BaseColumns object MapContract { object MapEntry : BaseColumns { const val TABLE_NAME = "map" + const val TABLE_NAME_HISTORY = "history" const val COLUMN_NAME_NAME = "name" const val COLUMN_NAME_CATEGORY = "category" const val COLUMN_NAME_ADDRESS = "address" diff --git a/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt b/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt index 73f7bf02..537cf439 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt @@ -10,11 +10,13 @@ import android.util.Log class MapDbHelper(mContext: Context) : SQLiteOpenHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION) { override fun onCreate(db: SQLiteDatabase?) { db?.execSQL(SQL_CREATE_ENTRIES) + db?.execSQL(SQL_CREATE_ENTRIES_HISTORY) initializeDb(db) } override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { db?.execSQL(SQL_DELETE_ENTRIES) + db?.execSQL(SQL_DELETE_ENTRIES_HISTORY) onCreate(db) } @@ -47,5 +49,15 @@ class MapDbHelper(mContext: Context) : SQLiteOpenHelper(mContext, DATABASE_NAME, ");" private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${MapContract.MapEntry.TABLE_NAME}" + +// const val DATABASE_NAME_HISTORY = "history.db" +// const val DATABASE_VERSION_HISTORY = 1 + private const val SQL_CREATE_ENTRIES_HISTORY = + "CREATE TABLE ${MapContract.MapEntry.TABLE_NAME_HISTORY} (" + + "${BaseColumns._ID} INTEGER PRIMARY KEY AUTOINCREMENT," + + "${MapContract.MapEntry.COLUMN_NAME_NAME} TEXT" + + ");" + private const val SQL_DELETE_ENTRIES_HISTORY = + "DROP TABLE IF EXISTS ${MapContract.MapEntry.TABLE_NAME_HISTORY}" } } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MapModel.kt b/app/src/main/java/campus/tech/kakao/map/MapModel.kt index 9ae67a6c..901aeb1e 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapModel.kt @@ -72,4 +72,12 @@ class MapModel(mContext: Context) { return Location(name, category, address) } + + fun writeHistory(name: String) { + val writeableDb = helper.writableDatabase + val content = ContentValues() + content.put(MapContract.MapEntry.COLUMN_NAME_NAME, name) + + writeableDb.insert(MapContract.MapEntry.TABLE_NAME_HISTORY, null, content) + } } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt b/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt index ede615f3..c14e26f8 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt @@ -1,6 +1,8 @@ package campus.tech.kakao.map import android.content.Context +import android.content.Intent +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -11,12 +13,20 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder class MapRecyclerAdapter( val locationList: List, val layoutInflater: LayoutInflater, - val mContext: Context + val mContext: Context, + val writeHistory: (String) -> Unit ) : RecyclerView.Adapter() { inner class MapViewHolder(itemView: View) : ViewHolder(itemView) { val name: TextView = itemView.findViewById(R.id.location_name) val category: TextView = itemView.findViewById(R.id.location_category) val address: TextView = itemView.findViewById(R.id.location_address) + + init { + itemView.setOnClickListener { + if (bindingAdapterPosition != RecyclerView.NO_POSITION) + writeHistory(name.text.toString()) + } + } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MapViewHolder { diff --git a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt index 5a26860d..53303a1b 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt @@ -15,6 +15,10 @@ class MapViewModel(mContext: Context) : ViewModel() { fun searchLocation(locName: String, isExactMatch: Boolean): List { return model.searchLocation(locName, isExactMatch) } + + fun writeHistory(name: String) { + model.writeHistory(name) + } fun getAllLocation(): List { return model.getAllLocation() } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 06cb4595..51f2c582 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -38,13 +38,21 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/search_box" /> + diff --git a/app/src/main/res/layout/item_search_history.xml b/app/src/main/res/layout/item_search_history.xml new file mode 100644 index 00000000..c4a0e191 --- /dev/null +++ b/app/src/main/res/layout/item_search_history.xml @@ -0,0 +1,28 @@ + + + + + \ No newline at end of file From e1af1c601f3c275e08fb22373970e57d3c9878b2 Mon Sep 17 00:00:00 2001 From: sumin Date: Thu, 4 Jul 2024 15:29:00 +0900 Subject: [PATCH 11/17] =?UTF-8?q?feat:=20=EA=B2=80=EC=83=89=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=EC=9D=84=20=EA=B2=80=EC=83=89=20=EC=B0=BD=20=ED=95=98?= =?UTF-8?q?=EB=8B=A8=EC=97=90=20=ED=91=9C=EC=8B=9C=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- .../campus/tech/kakao/map/MainActivity.kt | 7 +++++++ .../java/campus/tech/kakao/map/MapModel.kt | 21 +++++++++++++++++++ .../campus/tech/kakao/map/MapViewModel.kt | 9 ++++++-- app/src/main/res/layout/activity_main.xml | 3 +-- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8be62b99..8ddbef99 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - [x] 검색어를 입력하면 검색 결과 목록 표시 - [x] 검색 결과 목록 스크롤 가능 - [x] 입력한 검색어 X 버튼을 통해 삭제 가능 -- [ ] 검색 결과에서 항목 선택 시 검색어 저장 목록에 추가 -- [ ] 저장된 검색어 가로 스크롤 가능 +- [x] 검색 결과에서 항목 선택 시 검색어 저장 목록에 추가 +- [x] 저장된 검색어 가로 스크롤 가능 - [ ] 저장된 검색어 X 버튼을 통해 삭제 가능 - [x] 저장된 검색어 DB에 저장 \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt index 40b4af6d..a9b9ede1 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -40,6 +40,10 @@ class MainActivity : AppCompatActivity() { clear.setOnClickListener { searchBox.text.clear() } + val history = viewModel.getAllHistory() + searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater) + searchHistoryView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.HORIZONTAL, false) + searchHistoryView.isVisible = true } @@ -55,8 +59,11 @@ class MainActivity : AppCompatActivity() { } searchResultView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false) } + fun writeHistory(name: String): Unit { viewModel.writeHistory(name) + val history = viewModel.getAllHistory() + searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater) } } diff --git a/app/src/main/java/campus/tech/kakao/map/MapModel.kt b/app/src/main/java/campus/tech/kakao/map/MapModel.kt index 901aeb1e..b3ed141f 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapModel.kt @@ -80,4 +80,25 @@ class MapModel(mContext: Context) { writeableDb.insert(MapContract.MapEntry.TABLE_NAME_HISTORY, null, content) } + + fun getAllHistory(): List { + val readableDb = helper.readableDatabase + val cursor = readableDb.query( + MapContract.MapEntry.TABLE_NAME_HISTORY, + null, + null, + null, + null, + null, + null + ) + + val res = mutableListOf() + while (cursor.moveToNext()) { + res.add(cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_NAME))) + } + cursor.close() + + return res + } } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt index 53303a1b..31bd213a 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt @@ -16,10 +16,15 @@ class MapViewModel(mContext: Context) : ViewModel() { return model.searchLocation(locName, isExactMatch) } + fun getAllLocation(): List { + return model.getAllLocation() + } + fun writeHistory(name: String) { model.writeHistory(name) } - fun getAllLocation(): List { - return model.getAllLocation() + + fun getAllHistory(): List { + return model.getAllHistory() } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 51f2c582..2c081f01 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -42,8 +42,7 @@ android:id="@+id/search_history" android:layout_width="match_parent" android:layout_height="wrap_content" - app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" - android:orientation="horizontal" + android:visibility="gone" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/search_box"/> Date: Thu, 4 Jul 2024 20:29:41 +0900 Subject: [PATCH 12/17] =?UTF-8?q?feat:=20=EA=B2=80=EC=83=89=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=EC=9D=B4=20=EC=A4=91=EB=B3=B5=EB=90=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EA=B3=A0=20=EC=83=88=EB=A1=AD=EA=B2=8C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=EB=90=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tech/kakao/map/HistoryRecyclerAdapter.kt | 8 +++++ .../java/campus/tech/kakao/map/MapModel.kt | 35 ++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt b/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt index 95a5ee3e..7b3f83f6 100644 --- a/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt @@ -3,6 +3,7 @@ package campus.tech.kakao.map import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageButton import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.ViewHolder @@ -13,6 +14,13 @@ class HistoryRecyclerAdapter( ) : RecyclerView.Adapter() { inner class HistoryViewHolder(itemView: View) : ViewHolder(itemView) { val name: TextView = itemView.findViewById(R.id.history_name) + val clear: ImageButton = itemView.findViewById(R.id.history_clear) + + init { + clear.setOnClickListener { + notifyItemRemoved(bindingAdapterPosition) + } + } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryViewHolder { diff --git a/app/src/main/java/campus/tech/kakao/map/MapModel.kt b/app/src/main/java/campus/tech/kakao/map/MapModel.kt index b3ed141f..08f64e5b 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapModel.kt @@ -73,14 +73,42 @@ class MapModel(mContext: Context) { return Location(name, category, address) } - fun writeHistory(name: String) { + fun writeHistory(locName: String) { + + if (isHistoryExist(locName)) + deleteHistory(locName) val writeableDb = helper.writableDatabase val content = ContentValues() - content.put(MapContract.MapEntry.COLUMN_NAME_NAME, name) - + content.put(MapContract.MapEntry.COLUMN_NAME_NAME, locName) writeableDb.insert(MapContract.MapEntry.TABLE_NAME_HISTORY, null, content) } + private fun isHistoryExist(locName: String): Boolean { + val readableDb = helper.readableDatabase + val selection = "${MapContract.MapEntry.COLUMN_NAME_NAME} = ?" + val selectionArgs = arrayOf(locName) + val cursor = readableDb.query( + MapContract.MapEntry.TABLE_NAME_HISTORY, + null, + selection, + selectionArgs, + null, + null, + null + ) + val isExist: Boolean = cursor.moveToNext() + cursor.close() + return isExist + } + + fun deleteHistory(locName: String) { + val writeableDb = helper.writableDatabase + val selection = "${MapContract.MapEntry.COLUMN_NAME_NAME} = ?" + val selectionArgs = arrayOf(locName) + + writeableDb.delete(MapContract.MapEntry.TABLE_NAME_HISTORY, selection, selectionArgs) + } + fun getAllHistory(): List { val readableDb = helper.readableDatabase val cursor = readableDb.query( @@ -98,7 +126,6 @@ class MapModel(mContext: Context) { res.add(cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_NAME))) } cursor.close() - return res } } \ No newline at end of file From ac7775fcb964cf43076fee3c2f9e8e4e0e0d4806 Mon Sep 17 00:00:00 2001 From: sumin Date: Thu, 4 Jul 2024 23:21:04 +0900 Subject: [PATCH 13/17] =?UTF-8?q?feat:=20X=20=EB=B2=84=ED=8A=BC=20?= =?UTF-8?q?=EB=88=84=EB=A5=BC=20=EC=8B=9C=20=EA=B2=80=EC=83=89=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../campus/tech/kakao/map/DatabaseListener.kt | 6 ++++++ .../tech/kakao/map/HistoryRecyclerAdapter.kt | 9 +++++++-- .../java/campus/tech/kakao/map/MainActivity.kt | 18 ++++++++++++------ .../tech/kakao/map/MapRecyclerAdapter.kt | 5 ++--- .../java/campus/tech/kakao/map/MapViewModel.kt | 12 ++++++++---- 6 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/campus/tech/kakao/map/DatabaseListener.kt diff --git a/README.md b/README.md index 8ddbef99..62f52f72 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,5 @@ - [x] 입력한 검색어 X 버튼을 통해 삭제 가능 - [x] 검색 결과에서 항목 선택 시 검색어 저장 목록에 추가 - [x] 저장된 검색어 가로 스크롤 가능 -- [ ] 저장된 검색어 X 버튼을 통해 삭제 가능 +- [x] 저장된 검색어 X 버튼을 통해 삭제 가능 - [x] 저장된 검색어 DB에 저장 \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/DatabaseListener.kt b/app/src/main/java/campus/tech/kakao/map/DatabaseListener.kt new file mode 100644 index 00000000..e3b9e9be --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/DatabaseListener.kt @@ -0,0 +1,6 @@ +package campus.tech.kakao.map + +interface DatabaseListener { + fun deleteHistory(historyName: String) + fun writeHistory(historyName: String) +} \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt b/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt index 7b3f83f6..c126e9b4 100644 --- a/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt @@ -10,7 +10,8 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder class HistoryRecyclerAdapter( val history: List, - val layoutInflater: LayoutInflater + val layoutInflater: LayoutInflater, + val databaseListener: DatabaseListener ) : RecyclerView.Adapter() { inner class HistoryViewHolder(itemView: View) : ViewHolder(itemView) { val name: TextView = itemView.findViewById(R.id.history_name) @@ -18,7 +19,11 @@ class HistoryRecyclerAdapter( init { clear.setOnClickListener { - notifyItemRemoved(bindingAdapterPosition) + if (bindingAdapterPosition != RecyclerView.NO_POSITION) { + databaseListener.deleteHistory(name.text.toString()) + notifyItemRemoved(bindingAdapterPosition) + } +// notifyDataSetChanged() } } } diff --git a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt index a9b9ede1..95288a49 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -12,7 +12,7 @@ import androidx.core.widget.doAfterTextChanged import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView -class MainActivity : AppCompatActivity() { +class MainActivity : AppCompatActivity(), DatabaseListener { private lateinit var viewModel: MapViewModel private lateinit var searchBox: EditText private lateinit var searchHistoryView: RecyclerView @@ -41,7 +41,7 @@ class MainActivity : AppCompatActivity() { searchBox.text.clear() } val history = viewModel.getAllHistory() - searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater) + searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater, this) searchHistoryView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.HORIZONTAL, false) searchHistoryView.isVisible = true @@ -49,7 +49,7 @@ class MainActivity : AppCompatActivity() { fun search(locName: String, isExactMatch: Boolean) { val searchResult = viewModel.searchLocation(locName, isExactMatch) - searchResultView.adapter = MapRecyclerAdapter(searchResult, layoutInflater, this@MainActivity, ::writeHistory) + searchResultView.adapter = MapRecyclerAdapter(searchResult, layoutInflater, this) if (searchResult.isNotEmpty() && locName.isNotEmpty()) { searchResultView.isVisible = true message.isVisible = false @@ -60,10 +60,16 @@ class MainActivity : AppCompatActivity() { searchResultView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false) } - fun writeHistory(name: String): Unit { - viewModel.writeHistory(name) + override fun deleteHistory(historyName: String) { + viewModel.deleteHistory(historyName) val history = viewModel.getAllHistory() - searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater) + searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater, this) + } + + override fun writeHistory(historyName: String): Unit { + viewModel.writeHistory(historyName) + val history = viewModel.getAllHistory() + searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater, this) } } diff --git a/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt b/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt index c14e26f8..eba53324 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt @@ -13,8 +13,7 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder class MapRecyclerAdapter( val locationList: List, val layoutInflater: LayoutInflater, - val mContext: Context, - val writeHistory: (String) -> Unit + val databaseListener: DatabaseListener ) : RecyclerView.Adapter() { inner class MapViewHolder(itemView: View) : ViewHolder(itemView) { val name: TextView = itemView.findViewById(R.id.location_name) @@ -24,7 +23,7 @@ class MapRecyclerAdapter( init { itemView.setOnClickListener { if (bindingAdapterPosition != RecyclerView.NO_POSITION) - writeHistory(name.text.toString()) + databaseListener.writeHistory(name.text.toString()) } } } diff --git a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt index 31bd213a..61ca3a6f 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt @@ -20,11 +20,15 @@ class MapViewModel(mContext: Context) : ViewModel() { return model.getAllLocation() } - fun writeHistory(name: String) { - model.writeHistory(name) - } - fun getAllHistory(): List { return model.getAllHistory() } + + fun deleteHistory(historyName: String) { + model.deleteHistory(historyName) + } + + fun writeHistory(historyName: String) { + model.writeHistory(historyName) + } } \ No newline at end of file From 48c92b35695e645dbcff4cc670ff6edd7d04ef9c Mon Sep 17 00:00:00 2001 From: sumin Date: Fri, 5 Jul 2024 07:12:00 +0900 Subject: [PATCH 14/17] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=84=B0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MainActivity: - 기능을 메서드로 분리 - LiveData를 통해 observe하도록 변경 MapViewModel: - LiveData로 View가 필요한 데이터를 관리하도록 변경 MapModel: - 함수명을 알아보기 쉽도록 변경 - 공통되는 기능을 메서드로 분리하여 재사용 ResultRecyclerAdapter: - 클래스 명을 더 적합한 이름으로 변경 --- .../campus/tech/kakao/map/DatabaseListener.kt | 4 +- .../tech/kakao/map/HistoryRecyclerAdapter.kt | 12 +++- .../campus/tech/kakao/map/MainActivity.kt | 64 +++++++++++++------ .../java/campus/tech/kakao/map/MapDbHelper.kt | 3 - .../java/campus/tech/kakao/map/MapModel.kt | 20 +++--- .../campus/tech/kakao/map/MapViewModel.kt | 34 +++++++--- ...lerAdapter.kt => ResultRecyclerAdapter.kt} | 27 ++++---- app/src/main/res/layout/activity_main.xml | 1 - 8 files changed, 104 insertions(+), 61 deletions(-) rename app/src/main/java/campus/tech/kakao/map/{MapRecyclerAdapter.kt => ResultRecyclerAdapter.kt} (66%) diff --git a/app/src/main/java/campus/tech/kakao/map/DatabaseListener.kt b/app/src/main/java/campus/tech/kakao/map/DatabaseListener.kt index e3b9e9be..863926cc 100644 --- a/app/src/main/java/campus/tech/kakao/map/DatabaseListener.kt +++ b/app/src/main/java/campus/tech/kakao/map/DatabaseListener.kt @@ -2,5 +2,7 @@ package campus.tech.kakao.map interface DatabaseListener { fun deleteHistory(historyName: String) - fun writeHistory(historyName: String) + fun insertHistory(historyName: String) + fun updateSearchResult() + fun updateSearchHistory() } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt b/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt index c126e9b4..a0b135a3 100644 --- a/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/HistoryRecyclerAdapter.kt @@ -1,15 +1,19 @@ package campus.tech.kakao.map +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageButton import android.widget.TextView +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.ViewHolder class HistoryRecyclerAdapter( - val history: List, + var history: List, val layoutInflater: LayoutInflater, val databaseListener: DatabaseListener ) : RecyclerView.Adapter() { @@ -21,9 +25,7 @@ class HistoryRecyclerAdapter( clear.setOnClickListener { if (bindingAdapterPosition != RecyclerView.NO_POSITION) { databaseListener.deleteHistory(name.text.toString()) - notifyItemRemoved(bindingAdapterPosition) } -// notifyDataSetChanged() } } } @@ -40,4 +42,8 @@ class HistoryRecyclerAdapter( override fun getItemCount(): Int { return history.size } + + fun refreshList() { + notifyDataSetChanged() + } } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt index 95288a49..cdbafa01 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -1,14 +1,13 @@ package campus.tech.kakao.map -import android.content.ContentValues import android.os.Bundle -import android.util.Log import android.widget.EditText import android.widget.ImageButton import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.core.view.isVisible import androidx.core.widget.doAfterTextChanged +import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -20,11 +19,14 @@ class MainActivity : AppCompatActivity(), DatabaseListener { private lateinit var message: TextView private lateinit var clear: ImageButton + private lateinit var searchResultAdapter: ResultRecyclerAdapter + private lateinit var searchHistoryAdapter: HistoryRecyclerAdapter + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - viewModel = MapViewModel(this) + viewModel = MapViewModel(this, this) searchBox = findViewById(R.id.search_box) searchHistoryView = findViewById(R.id.search_history) searchResultView = findViewById(R.id.search_result) @@ -40,36 +42,58 @@ class MainActivity : AppCompatActivity(), DatabaseListener { clear.setOnClickListener { searchBox.text.clear() } - val history = viewModel.getAllHistory() - searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater, this) - searchHistoryView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.HORIZONTAL, false) - searchHistoryView.isVisible = true + initSearchResultView() + initSearchHistoryView() + observeData() } - fun search(locName: String, isExactMatch: Boolean) { - val searchResult = viewModel.searchLocation(locName, isExactMatch) - searchResultView.adapter = MapRecyclerAdapter(searchResult, layoutInflater, this) - if (searchResult.isNotEmpty() && locName.isNotEmpty()) { + override fun deleteHistory(historyName: String) { + viewModel.deleteHistory(historyName) + } + + override fun insertHistory(historyName: String) { + viewModel.insertHistory(historyName) + } + + override fun updateSearchResult() { + val searchResult = viewModel.searchResult.value!! + searchResultAdapter.refreshList() + + if (searchResult.isNotEmpty() && searchBox.text.isNotEmpty()) { searchResultView.isVisible = true message.isVisible = false } else { searchResultView.isVisible = false message.isVisible = true } - searchResultView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false) } - override fun deleteHistory(historyName: String) { - viewModel.deleteHistory(historyName) - val history = viewModel.getAllHistory() - searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater, this) + override fun updateSearchHistory() { + searchHistoryAdapter.refreshList() + } + private fun search(locName: String, isExactMatch: Boolean) { + viewModel.searchLocation(locName, isExactMatch) } - override fun writeHistory(historyName: String): Unit { - viewModel.writeHistory(historyName) - val history = viewModel.getAllHistory() - searchHistoryView.adapter = HistoryRecyclerAdapter(history, layoutInflater, this) + private fun initSearchResultView() { + searchResultAdapter = ResultRecyclerAdapter(viewModel.searchResult.value!!, layoutInflater, this) + searchResultView.adapter = searchResultAdapter + searchResultView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false) } + private fun initSearchHistoryView() { + searchHistoryAdapter = HistoryRecyclerAdapter(viewModel.getAllHistory(), layoutInflater, this) + searchHistoryView.adapter = searchHistoryAdapter + searchHistoryView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.HORIZONTAL, false) + } + + private fun observeData() { + viewModel.searchHistory.observe(this, Observer { + searchHistoryAdapter.history = it + }) + viewModel.searchResult.observe(this, Observer { + searchResultAdapter.searchResult = it + }) + } } diff --git a/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt b/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt index 537cf439..efc66244 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt @@ -5,7 +5,6 @@ import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import android.provider.BaseColumns -import android.util.Log class MapDbHelper(mContext: Context) : SQLiteOpenHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION) { override fun onCreate(db: SQLiteDatabase?) { @@ -50,8 +49,6 @@ class MapDbHelper(mContext: Context) : SQLiteOpenHelper(mContext, DATABASE_NAME, private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${MapContract.MapEntry.TABLE_NAME}" -// const val DATABASE_NAME_HISTORY = "history.db" -// const val DATABASE_VERSION_HISTORY = 1 private const val SQL_CREATE_ENTRIES_HISTORY = "CREATE TABLE ${MapContract.MapEntry.TABLE_NAME_HISTORY} (" + "${BaseColumns._ID} INTEGER PRIMARY KEY AUTOINCREMENT," + diff --git a/app/src/main/java/campus/tech/kakao/map/MapModel.kt b/app/src/main/java/campus/tech/kakao/map/MapModel.kt index 08f64e5b..abcd5651 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapModel.kt @@ -3,7 +3,6 @@ package campus.tech.kakao.map import android.content.ContentValues import android.content.Context import android.database.Cursor -import android.util.Log class MapModel(mContext: Context) { private val helper: MapDbHelper = MapDbHelper(mContext) @@ -22,7 +21,7 @@ class MapModel(mContext: Context) { writableDb.insert(MapContract.MapEntry.TABLE_NAME, null, content) } - fun searchLocation(locName: String, isExactMatch: Boolean): List { + fun getSearchedLocation(locName: String, isExactMatch: Boolean): List { val readableDb = helper.readableDatabase val selection = "${MapContract.MapEntry.COLUMN_NAME_NAME} LIKE ?" @@ -36,13 +35,7 @@ class MapModel(mContext: Context) { null, null ) - - val res = mutableListOf() - while (cursor.moveToNext()) { - res.add(createLocationFromData(cursor)) - } - cursor.close() - return res + return getLocationResult(cursor) } fun getAllLocation() : List { @@ -56,16 +49,19 @@ class MapModel(mContext: Context) { null, null ) + return getLocationResult(cursor) + } + private fun getLocationResult(cursor: Cursor): List { val res = mutableListOf() while (cursor.moveToNext()) { - res.add(createLocationFromData(cursor)) + res.add(getLocation(cursor)) } cursor.close() return res } - private fun createLocationFromData(cursor: Cursor): Location { + private fun getLocation(cursor: Cursor): Location { val name = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_NAME)) val category = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_CATEGORY)) val address = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_ADDRESS)) @@ -73,7 +69,7 @@ class MapModel(mContext: Context) { return Location(name, category, address) } - fun writeHistory(locName: String) { + fun insertHistory(locName: String) { if (isHistoryExist(locName)) deleteHistory(locName) diff --git a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt index 61ca3a6f..57cf1b26 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapViewModel.kt @@ -5,30 +5,46 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel -class MapViewModel(mContext: Context) : ViewModel() { +class MapViewModel(mContext: Context, val databaseListener: DatabaseListener) : ViewModel() { private val model = MapModel(mContext) + private val _searchResult: MutableLiveData> + val searchResult: LiveData> + private val _searchHistory: MutableLiveData> + val searchHistory: LiveData> + + init { + _searchResult = MutableLiveData(model.getSearchedLocation("", false)) + searchResult = _searchResult + _searchHistory = MutableLiveData(model.getAllHistory()) + searchHistory = _searchHistory + } fun insertLocation(location: Location) { model.insertLocation(location) } - fun searchLocation(locName: String, isExactMatch: Boolean): List { - return model.searchLocation(locName, isExactMatch) + fun searchLocation(locName: String, isExactMatch: Boolean) { + _searchResult.value = model.getSearchedLocation(locName, isExactMatch) + databaseListener.updateSearchResult() } fun getAllLocation(): List { return model.getAllLocation() } - fun getAllHistory(): List { - return model.getAllHistory() - } - fun deleteHistory(historyName: String) { model.deleteHistory(historyName) + _searchHistory.value = model.getAllHistory() + databaseListener.updateSearchHistory() } - fun writeHistory(historyName: String) { - model.writeHistory(historyName) + fun insertHistory(historyName: String) { + model.insertHistory(historyName) + _searchHistory.value = model.getAllHistory() + databaseListener.updateSearchHistory() + } + + fun getAllHistory(): List { + return model.getAllHistory() } } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt b/app/src/main/java/campus/tech/kakao/map/ResultRecyclerAdapter.kt similarity index 66% rename from app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt rename to app/src/main/java/campus/tech/kakao/map/ResultRecyclerAdapter.kt index eba53324..d09abe99 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapRecyclerAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/ResultRecyclerAdapter.kt @@ -1,8 +1,5 @@ package campus.tech.kakao.map -import android.content.Context -import android.content.Intent -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -10,11 +7,11 @@ import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.ViewHolder -class MapRecyclerAdapter( - val locationList: List, +class ResultRecyclerAdapter( + var searchResult: List, val layoutInflater: LayoutInflater, val databaseListener: DatabaseListener -) : RecyclerView.Adapter() { +) : RecyclerView.Adapter() { inner class MapViewHolder(itemView: View) : ViewHolder(itemView) { val name: TextView = itemView.findViewById(R.id.location_name) val category: TextView = itemView.findViewById(R.id.location_category) @@ -22,8 +19,10 @@ class MapRecyclerAdapter( init { itemView.setOnClickListener { - if (bindingAdapterPosition != RecyclerView.NO_POSITION) - databaseListener.writeHistory(name.text.toString()) + if (bindingAdapterPosition != RecyclerView.NO_POSITION) { + databaseListener.insertHistory(name.text.toString()) + databaseListener.updateSearchHistory() + } } } } @@ -34,12 +33,16 @@ class MapRecyclerAdapter( } override fun getItemCount(): Int { - return locationList.size + return searchResult.size } override fun onBindViewHolder(holder: MapViewHolder, position: Int) { - holder.name.text = locationList[position].name - holder.category.text = locationList[position].category - holder.address.text = locationList[position].address + holder.name.text = searchResult[position].name + holder.category.text = searchResult[position].category + holder.address.text = searchResult[position].address + } + + fun refreshList() { + notifyDataSetChanged() } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2c081f01..1c8be102 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -42,7 +42,6 @@ android:id="@+id/search_history" android:layout_width="match_parent" android:layout_height="wrap_content" - android:visibility="gone" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/search_box"/> Date: Fri, 5 Jul 2024 07:28:14 +0900 Subject: [PATCH 15/17] =?UTF-8?q?style:=20=EA=B2=80=EC=83=89=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EA=B0=84=20=EA=B5=AC=EB=B6=84=EC=9D=84=20=EC=9C=84?= =?UTF-8?q?=ED=95=9C=20=EB=94=94=EC=9E=90=EC=9D=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/res/drawable/background_item_search_history.xml | 6 ++++++ app/src/main/res/layout/item_search_history.xml | 2 ++ app/src/main/res/values/colors.xml | 1 + 3 files changed, 9 insertions(+) create mode 100644 app/src/main/res/drawable/background_item_search_history.xml diff --git a/app/src/main/res/drawable/background_item_search_history.xml b/app/src/main/res/drawable/background_item_search_history.xml new file mode 100644 index 00000000..d927da07 --- /dev/null +++ b/app/src/main/res/drawable/background_item_search_history.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_search_history.xml b/app/src/main/res/layout/item_search_history.xml index c4a0e191..4dd7bdfc 100644 --- a/app/src/main/res/layout/item_search_history.xml +++ b/app/src/main/res/layout/item_search_history.xml @@ -3,6 +3,8 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" + android:layout_margin="5dp" + android:background="@drawable/background_item_search_history" xmlns:app="http://schemas.android.com/apk/res-auto"> #FF000000 #FFFFFFFF + #998BC34A From 8ad627a862723c062fd8e9cd19475a5d5869c421 Mon Sep 17 00:00:00 2001 From: sumin Date: Fri, 5 Jul 2024 07:38:13 +0900 Subject: [PATCH 16/17] style: code style reformat --- .../campus/tech/kakao/map/MainActivity.kt | 13 +++++--- .../java/campus/tech/kakao/map/MapDbHelper.kt | 3 +- .../java/campus/tech/kakao/map/MapModel.kt | 11 ++++--- app/src/main/res/drawable/clear_24.xml | 13 ++++++-- .../main/res/layout/item_search_history.xml | 22 +++++++------ .../main/res/layout/item_search_result.xml | 32 +++++++++++-------- 6 files changed, 58 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt index cdbafa01..460324b8 100644 --- a/app/src/main/java/campus/tech/kakao/map/MainActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/MainActivity.kt @@ -72,20 +72,25 @@ class MainActivity : AppCompatActivity(), DatabaseListener { override fun updateSearchHistory() { searchHistoryAdapter.refreshList() } + private fun search(locName: String, isExactMatch: Boolean) { viewModel.searchLocation(locName, isExactMatch) } private fun initSearchResultView() { - searchResultAdapter = ResultRecyclerAdapter(viewModel.searchResult.value!!, layoutInflater, this) + searchResultAdapter = + ResultRecyclerAdapter(viewModel.searchResult.value!!, layoutInflater, this) searchResultView.adapter = searchResultAdapter - searchResultView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false) + searchResultView.layoutManager = + LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false) } private fun initSearchHistoryView() { - searchHistoryAdapter = HistoryRecyclerAdapter(viewModel.getAllHistory(), layoutInflater, this) + searchHistoryAdapter = + HistoryRecyclerAdapter(viewModel.getAllHistory(), layoutInflater, this) searchHistoryView.adapter = searchHistoryAdapter - searchHistoryView.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.HORIZONTAL, false) + searchHistoryView.layoutManager = + LinearLayoutManager(this@MainActivity, LinearLayoutManager.HORIZONTAL, false) } private fun observeData() { diff --git a/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt b/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt index efc66244..71774fea 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapDbHelper.kt @@ -6,7 +6,8 @@ import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import android.provider.BaseColumns -class MapDbHelper(mContext: Context) : SQLiteOpenHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION) { +class MapDbHelper(mContext: Context) : + SQLiteOpenHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION) { override fun onCreate(db: SQLiteDatabase?) { db?.execSQL(SQL_CREATE_ENTRIES) db?.execSQL(SQL_CREATE_ENTRIES_HISTORY) diff --git a/app/src/main/java/campus/tech/kakao/map/MapModel.kt b/app/src/main/java/campus/tech/kakao/map/MapModel.kt index abcd5651..df01d68f 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapModel.kt @@ -38,7 +38,7 @@ class MapModel(mContext: Context) { return getLocationResult(cursor) } - fun getAllLocation() : List { + fun getAllLocation(): List { val readableDb = helper.readableDatabase val cursor = readableDb.query( MapContract.MapEntry.TABLE_NAME, @@ -62,9 +62,12 @@ class MapModel(mContext: Context) { } private fun getLocation(cursor: Cursor): Location { - val name = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_NAME)) - val category = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_CATEGORY)) - val address = cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_ADDRESS)) + val name = + cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_NAME)) + val category = + cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_CATEGORY)) + val address = + cursor.getString(cursor.getColumnIndexOrThrow(MapContract.MapEntry.COLUMN_NAME_ADDRESS)) return Location(name, category, address) } diff --git a/app/src/main/res/drawable/clear_24.xml b/app/src/main/res/drawable/clear_24.xml index f8ca0c64..8f89359b 100644 --- a/app/src/main/res/drawable/clear_24.xml +++ b/app/src/main/res/drawable/clear_24.xml @@ -1,5 +1,12 @@ - - - + + + diff --git a/app/src/main/res/layout/item_search_history.xml b/app/src/main/res/layout/item_search_history.xml index 4dd7bdfc..a5c6c465 100644 --- a/app/src/main/res/layout/item_search_history.xml +++ b/app/src/main/res/layout/item_search_history.xml @@ -1,30 +1,32 @@ + android:padding="5dp"> + + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + app:layout_constraintTop_toTopOf="parent" /> \ No newline at end of file diff --git a/app/src/main/res/layout/item_search_result.xml b/app/src/main/res/layout/item_search_result.xml index 39fc6cf5..07f57fd4 100644 --- a/app/src/main/res/layout/item_search_result.xml +++ b/app/src/main/res/layout/item_search_result.xml @@ -1,42 +1,46 @@ + android:layout_height="60dp"> + + app:layout_constraintTop_toTopOf="parent" /> + + app:layout_constraintTop_toTopOf="parent" /> + + app:layout_constraintTop_toTopOf="parent" /> + + app:layout_constraintTop_toBottomOf="@id/location_name" /> \ No newline at end of file From 6dbd82d8b346e1386f336db2c40abaa85d408d6f Mon Sep 17 00:00:00 2001 From: sumin Date: Mon, 8 Jul 2024 14:29:39 +0900 Subject: [PATCH 17/17] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=B4=88=EA=B8=B0=ED=99=94=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/campus/tech/kakao/map/MapModel.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/src/main/java/campus/tech/kakao/map/MapModel.kt b/app/src/main/java/campus/tech/kakao/map/MapModel.kt index df01d68f..e8d18b69 100644 --- a/app/src/main/java/campus/tech/kakao/map/MapModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/MapModel.kt @@ -7,10 +7,6 @@ import android.database.Cursor class MapModel(mContext: Context) { private val helper: MapDbHelper = MapDbHelper(mContext) - init { - helper.readableDatabase - } - fun insertLocation(location: Location) { val writableDb = helper.writableDatabase val content = ContentValues()