From 9d4a8e2af2fbf3ff08cf5563fc30f6e521b565ae Mon Sep 17 00:00:00 2001 From: ddangcong80 Date: Mon, 8 Jul 2024 20:48:59 +0900 Subject: [PATCH] feat: Implement feature to save touched location to the database --- .../java/campus/tech/kakao/map/model/Place.kt | 4 +-- .../tech/kakao/map/model/PlaceContract.kt | 10 +++--- .../campus/tech/kakao/map/model/SavePlace.kt | 6 ++++ .../kakao/map/repository/SearchRepository.kt | 28 ++++++++++++++++ .../tech/kakao/map/view/SearchActivity.kt | 4 ++- .../tech/kakao/map/view/SearchAdapter.kt | 32 ++++++++++++------- app/src/main/res/layout/saveplace_item.xml | 8 +++-- 7 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/campus/tech/kakao/map/model/SavePlace.kt diff --git a/app/src/main/java/campus/tech/kakao/map/model/Place.kt b/app/src/main/java/campus/tech/kakao/map/model/Place.kt index e0b65a4d..98c4fcf1 100644 --- a/app/src/main/java/campus/tech/kakao/map/model/Place.kt +++ b/app/src/main/java/campus/tech/kakao/map/model/Place.kt @@ -5,6 +5,4 @@ data class Place( val address: String, val category: String ) -data class SavePlace ( - val savePlace: String -) + diff --git a/app/src/main/java/campus/tech/kakao/map/model/PlaceContract.kt b/app/src/main/java/campus/tech/kakao/map/model/PlaceContract.kt index 67b3f2be..265bf150 100644 --- a/app/src/main/java/campus/tech/kakao/map/model/PlaceContract.kt +++ b/app/src/main/java/campus/tech/kakao/map/model/PlaceContract.kt @@ -10,18 +10,20 @@ object PlaceContract { const val COLUMN_PLACE_CATEGORY = "category" const val CREATE_QUERY = "CREATE TABLE IF NOT EXISTS $TABLE_NAME (" + "${BaseColumns._ID} INTEGER PRIMARY KEY," + - "${PlaceEntry.COLUMN_PLACE_NAME} varchar(30)," + - "${PlaceEntry.COLUMN_PLACE_ADDRESS} varchar(255)," + - "${PlaceEntry.COLUMN_PLACE_CATEGORY} varchar(30))" + "$COLUMN_PLACE_NAME varchar(30)," + + "$COLUMN_PLACE_ADDRESS varchar(255)," + + "$COLUMN_PLACE_CATEGORY varchar(30))" const val DROP_QUERY = "DROP TABLE IF EXISTS $TABLE_NAME" } object SavePlaceEntry : BaseColumns { const val TABLE_NAME = "savePlace" const val COLUMN_PLACE_NAME = "savePlaceName" + private const val COLUMN_TIMESTAMP = "timestamp" const val CREATE_QUERY = "CREATE TABLE IF NOT EXISTS $TABLE_NAME (" + "${BaseColumns._ID} INTEGER PRIMARY KEY," + - "${SavePlaceEntry.COLUMN_PLACE_NAME} varchar(30))" + "$COLUMN_PLACE_NAME varchar(30)," + + "$COLUMN_TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP)" const val DROP_QUERY = "DROP TABLE IF EXISTS $TABLE_NAME" } } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/model/SavePlace.kt b/app/src/main/java/campus/tech/kakao/map/model/SavePlace.kt new file mode 100644 index 00000000..9c3cd897 --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/model/SavePlace.kt @@ -0,0 +1,6 @@ +package campus.tech.kakao.map.model + +data class SavePlace ( + val savePlace: String, + val time: String +) \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/repository/SearchRepository.kt b/app/src/main/java/campus/tech/kakao/map/repository/SearchRepository.kt index b932a1a2..fd4afcfa 100644 --- a/app/src/main/java/campus/tech/kakao/map/repository/SearchRepository.kt +++ b/app/src/main/java/campus/tech/kakao/map/repository/SearchRepository.kt @@ -85,4 +85,32 @@ class SearchRepository(context: Context) { return places } + + fun savePlaces(placeName: String) { + val db: SQLiteDatabase = dbHelper.writableDatabase + val values = ContentValues() + values.put(PlaceContract.SavePlaceEntry.COLUMN_PLACE_NAME, placeName) + + val cursor = db.query( + PlaceContract.SavePlaceEntry.TABLE_NAME, + arrayOf(PlaceContract.SavePlaceEntry.COLUMN_PLACE_NAME), + "${PlaceContract.SavePlaceEntry.COLUMN_PLACE_NAME} = ?", + arrayOf(placeName), + null, + null, + null + ) + + if (cursor.moveToFirst()) { + db.delete( + PlaceContract.SavePlaceEntry.TABLE_NAME, + "${PlaceContract.SavePlaceEntry.COLUMN_PLACE_NAME} = ?", + arrayOf(placeName) + ) + } + cursor.close() + + db.insert(PlaceContract.SavePlaceEntry.TABLE_NAME, null, values) + } + } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/view/SearchActivity.kt b/app/src/main/java/campus/tech/kakao/map/view/SearchActivity.kt index 481d3d04..3664f2d4 100644 --- a/app/src/main/java/campus/tech/kakao/map/view/SearchActivity.kt +++ b/app/src/main/java/campus/tech/kakao/map/view/SearchActivity.kt @@ -40,7 +40,9 @@ class SearchActivity : AppCompatActivity() { recyclerView.layoutManager = LinearLayoutManager(this) viewModel.places.observe(this) { places -> - searchAdapter = SearchAdapter(places) + searchAdapter = SearchAdapter(places) { + viewModel.savePlaces(it.name) + } recyclerView.adapter = searchAdapter updateViewVisibility(places.isNotEmpty()) } diff --git a/app/src/main/java/campus/tech/kakao/map/view/SearchAdapter.kt b/app/src/main/java/campus/tech/kakao/map/view/SearchAdapter.kt index 80c15fef..70ae0c0f 100644 --- a/app/src/main/java/campus/tech/kakao/map/view/SearchAdapter.kt +++ b/app/src/main/java/campus/tech/kakao/map/view/SearchAdapter.kt @@ -1,6 +1,5 @@ package campus.tech.kakao.map.view -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -10,14 +9,27 @@ import campus.tech.kakao.map.R import campus.tech.kakao.map.model.Place class SearchAdapter( - private val places: List + private val places: List, + private val onItemClickListener: (Place) -> Unit ) : RecyclerView.Adapter() { - class SearchViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { - val nameTextView: TextView = itemView.findViewById(R.id.placeName) - val addressTextView: TextView = itemView.findViewById(R.id.placeAddress) - val categoryTextView: TextView = itemView.findViewById(R.id.placeCategory) + class SearchViewHolder( + itemView: View, + private val onItemClickListener: (Place) -> Unit + ) : RecyclerView.ViewHolder(itemView) { + private val nameTextView: TextView = itemView.findViewById(R.id.placeName) + private val addressTextView: TextView = itemView.findViewById(R.id.placeAddress) + private val categoryTextView: TextView = itemView.findViewById(R.id.placeCategory) + + fun bind(place: Place) { + nameTextView.text = place.name + addressTextView.text = place.address + categoryTextView.text = place.category + itemView.setOnClickListener { + onItemClickListener(place) + } + } } override fun onCreateViewHolder( @@ -25,14 +37,12 @@ class SearchAdapter( viewType: Int ): SearchViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.place_item, parent, false) - return SearchViewHolder(view) + return SearchViewHolder(view, onItemClickListener) } - override fun onBindViewHolder(holder: SearchAdapter.SearchViewHolder, position: Int) { + override fun onBindViewHolder(holder: SearchViewHolder, position: Int) { val place = places[position] - holder.nameTextView.text = place.name - holder.addressTextView.text = place.address - holder.categoryTextView.text = place.category + holder.bind(place) } override fun getItemCount(): Int { diff --git a/app/src/main/res/layout/saveplace_item.xml b/app/src/main/res/layout/saveplace_item.xml index b0356c3b..643416bc 100644 --- a/app/src/main/res/layout/saveplace_item.xml +++ b/app/src/main/res/layout/saveplace_item.xml @@ -7,13 +7,15 @@ android:id="@+id/saveCancelBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:src="@drawable/cancel_btn" /> + android:src="@drawable/cancel_btn" + android:contentDescription="@string/deleteSavePlace" /> + + android:textSize="20sp" /> \ No newline at end of file