Skip to content

Commit

Permalink
feat: Implement feature to delete saved location search history
Browse files Browse the repository at this point in the history
  • Loading branch information
ddangcong80 committed Jul 8, 2024
1 parent 8f9c13d commit 7782ef9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,26 @@ class SearchRepository(context: Context) {
return savePlaces
}

fun deleteSavedPlace(savedPlaceName: String) {
val db: SQLiteDatabase = dbHelper.writableDatabase

val cursor = db.query(
PlaceContract.SavePlaceEntry.TABLE_NAME,
arrayOf(PlaceContract.SavePlaceEntry.COLUMN_PLACE_NAME),
"${PlaceContract.SavePlaceEntry.COLUMN_PLACE_NAME} = ?",
arrayOf(savedPlaceName),
null,
null,
null
)

if (cursor.moveToFirst()) {
db.delete(
PlaceContract.SavePlaceEntry.TABLE_NAME,
"${PlaceContract.SavePlaceEntry.COLUMN_PLACE_NAME} = ?",
arrayOf(savedPlaceName)
)
}
cursor.close()
}
}
14 changes: 11 additions & 3 deletions app/src/main/java/campus/tech/kakao/map/view/SavePlaceAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@ package campus.tech.kakao.map.view
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import campus.tech.kakao.map.R
import campus.tech.kakao.map.model.SavePlace

class SavePlaceAdapter(
private val savePlaces: List<SavePlace>
private val savePlaces: List<SavePlace>,
private val onItemClickListener: (SavePlace) -> Unit
) : RecyclerView.Adapter<SavePlaceAdapter.SavePlaceViewHolder>() {
class SavePlaceViewHolder(
itemView: View,
private val onItemClickListener: (SavePlace) -> Unit
) : RecyclerView.ViewHolder(itemView) {
private val savePlaceTextView: TextView = itemView.findViewById(R.id.savePlace)
private val savePlaceDeleteBtn: ImageView = itemView.findViewById(R.id.saveCancelBtn)

fun bind(savePlace: SavePlace) {
savePlaceTextView.text = savePlace.savePlace
savePlaceDeleteBtn.setOnClickListener {
onItemClickListener(savePlace)
}
}
}

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

override fun onBindViewHolder(holder: SavePlaceViewHolder, position: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class SearchActivity : AppCompatActivity() {
saveRecyclerView.layoutManager =
LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
viewModel.savePlaces.observe(this) { savePlaces ->
savePlaceAdapter = SavePlaceAdapter(savePlaces)
savePlaceAdapter = SavePlaceAdapter(savePlaces) {
viewModel.deleteSavedPlace(it.savePlace)
viewModel.showSavePlace()
}
saveRecyclerView.adapter = savePlaceAdapter
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ class SearchViewModel(application: Application) : AndroidViewModel(application)
fun showSavePlace() {
_savePlaces.value = searchRepo.showSavePlace()
}

fun deleteSavedPlace(savedPlaceName: String) {
searchRepo.deleteSavedPlace(savedPlaceName)
}
}

0 comments on commit 7782ef9

Please sign in to comment.