Skip to content

Commit

Permalink
feat: Implement horizontal RecyclerView to display saved places
Browse files Browse the repository at this point in the history
  • Loading branch information
ddangcong80 committed Jul 8, 2024
1 parent 9d4a8e2 commit 8f9c13d
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 11 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/campus/tech/kakao/map/model/SavePlace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ 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 @@ -8,6 +8,7 @@ import android.util.Log
import campus.tech.kakao.map.model.Place
import campus.tech.kakao.map.model.PlaceContract
import campus.tech.kakao.map.model.PlaceDBHelper
import campus.tech.kakao.map.model.SavePlace

class SearchRepository(context: Context) {
private val dbHelper = PlaceDBHelper(context)
Expand Down Expand Up @@ -113,4 +114,37 @@ class SearchRepository(context: Context) {
db.insert(PlaceContract.SavePlaceEntry.TABLE_NAME, null, values)
}

fun showSavePlace(): MutableList<SavePlace> {
val db: SQLiteDatabase = dbHelper.readableDatabase
val savePlaces = mutableListOf<SavePlace>()
var cursor: Cursor? = null
try {
cursor = db.query(
PlaceContract.SavePlaceEntry.TABLE_NAME,
null,
null,
null,
null,
null,
null
)

if (cursor != null) {
while (cursor.moveToNext()) {
val name =
cursor.getString(cursor.getColumnIndexOrThrow(PlaceContract.SavePlaceEntry.COLUMN_PLACE_NAME))

savePlaces.add(SavePlace(name))
}
}
} catch (e: Exception) {
Log.e("ddangcong80", "Error", e)
} finally {
cursor?.close()
db.close()
}

return savePlaces
}

}
40 changes: 40 additions & 0 deletions app/src/main/java/campus/tech/kakao/map/view/SavePlaceAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package campus.tech.kakao.map.view

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.R
import campus.tech.kakao.map.model.SavePlace

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

fun bind(savePlace: SavePlace) {
savePlaceTextView.text = savePlace.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)
}

override fun onBindViewHolder(holder: SavePlaceViewHolder, position: Int) {
val savePlace = savePlaces[position]
holder.bind(savePlace)
}

override fun getItemCount(): Int {
return savePlaces.size
}
}
29 changes: 24 additions & 5 deletions app/src/main/java/campus/tech/kakao/map/view/SearchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SearchActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var noSearchLayout: LinearLayout
private lateinit var searchAdapter: SearchAdapter
private lateinit var saveRecyclerView: RecyclerView
private lateinit var savePlaceAdapter: SavePlaceAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -30,33 +32,47 @@ class SearchActivity : AppCompatActivity() {
this,
ViewModelProvider.AndroidViewModelFactory.getInstance(application)
)[SearchViewModel::class.java]
editText = findViewById<EditText>(R.id.searchText)
cancelBtn = findViewById<ImageView>(R.id.cancelBtn)
recyclerView = findViewById(R.id.searchPlaceView)
noSearchLayout = findViewById(R.id.noSearch)

findViews()
setCancelBtnClickListener()
editTextWatcher()
viewModel.showSavePlace()

recyclerView.layoutManager = LinearLayoutManager(this)
viewModel.places.observe(this) { places ->
searchAdapter = SearchAdapter(places) {
viewModel.savePlaces(it.name)
viewModel.showSavePlace()
}
recyclerView.adapter = searchAdapter
updateViewVisibility(places.isNotEmpty())
}

saveRecyclerView.layoutManager =
LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
viewModel.savePlaces.observe(this) { savePlaces ->
savePlaceAdapter = SavePlaceAdapter(savePlaces)
saveRecyclerView.adapter = savePlaceAdapter
}

viewModel.insertDummyData("카페", "대전 유성구 궁동", "카페")
viewModel.insertDummyData("약국", "대전 유성구 봉명동", "약국")
}

private fun findViews() {
editText = findViewById<EditText>(R.id.searchText)
cancelBtn = findViewById<ImageView>(R.id.cancelBtn)
recyclerView = findViewById(R.id.searchPlaceView)
noSearchLayout = findViewById(R.id.noSearch)
saveRecyclerView = findViewById(R.id.savePlaceView)
}

private fun editTextWatcher() {
editText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if(s.isNullOrEmpty()) {
if (s.isNullOrEmpty()) {
updateViewVisibility(false)
} else {
viewModel.searchPlaces(s.toString())
Expand All @@ -83,4 +99,7 @@ class SearchActivity : AppCompatActivity() {
noSearchLayout.visibility = View.VISIBLE
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package campus.tech.kakao.map.viewmodel


import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import campus.tech.kakao.map.model.Place
import campus.tech.kakao.map.model.SavePlace
import campus.tech.kakao.map.repository.SearchRepository

class SearchViewModel(application: Application) : AndroidViewModel(application) {
private val searchRepo: SearchRepository = SearchRepository(application)
private val _places: MutableLiveData<List<Place>> = MutableLiveData()
private val _savePlaces: MutableLiveData<List<SavePlace>> = MutableLiveData()

val places: LiveData<List<Place>> get() = _places
val savePlaces: LiveData<List<SavePlace>> get() = _savePlaces


fun insertDummyData(name: String, address: String, category: String) {
searchRepo.insertPlaceDummyData(name, address, category)
Expand All @@ -21,4 +25,12 @@ class SearchViewModel(application: Application) : AndroidViewModel(application)
fun searchPlaces(placeCategory: String) {
_places.value = searchRepo.getSearchPlaces(placeCategory)
}

fun savePlaces(placeName: String) {
searchRepo.savePlaces(placeName)
}

fun showSavePlace() {
_savePlaces.value = searchRepo.showSavePlace()
}
}
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_search.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
Expand Down Expand Up @@ -33,7 +32,7 @@
</RelativeLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/savedPlaceView"
android:id="@+id/savePlaceView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/saveplace_item.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp">

<ImageView
android:id="@+id/saveCancelBtn"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<string name="placeAddress">장소</string>
<string name="placeCategory">카테고리</string>
<string name="saveplace">장소</string>
<string name="deleteSavePlace">검색기록삭제</string>
</resources>

0 comments on commit 8f9c13d

Please sign in to comment.