Skip to content

Commit

Permalink
feat: Implement feature to save touched location to the database
Browse files Browse the repository at this point in the history
  • Loading branch information
ddangcong80 committed Jul 8, 2024
1 parent 537b649 commit 9d4a8e2
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 22 deletions.
4 changes: 1 addition & 3 deletions app/src/main/java/campus/tech/kakao/map/model/Place.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ data class Place(
val address: String,
val category: String
)
data class SavePlace (
val savePlace: String
)

10 changes: 6 additions & 4 deletions app/src/main/java/campus/tech/kakao/map/model/PlaceContract.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/model/SavePlace.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package campus.tech.kakao.map.model

data class SavePlace (
val savePlace: String,
val time: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
32 changes: 21 additions & 11 deletions app/src/main/java/campus/tech/kakao/map/view/SearchAdapter.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,29 +9,40 @@ import campus.tech.kakao.map.R
import campus.tech.kakao.map.model.Place

class SearchAdapter(
private val places: List<Place>
private val places: List<Place>,
private val onItemClickListener: (Place) -> Unit
) :
RecyclerView.Adapter<SearchAdapter.SearchViewHolder>() {

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(
parent: ViewGroup,
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 {
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/res/layout/saveplace_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<TextView
android:id="@+id/savePlace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text="@string/saveplace"
android:textSize="20sp"
android:id="@+id/savePlace"/>
android:textSize="20sp" />

</androidx.appcompat.widget.LinearLayoutCompat>

0 comments on commit 9d4a8e2

Please sign in to comment.