From 8f9c13dd365fa20b99627e3d715a2b0df4876cea Mon Sep 17 00:00:00 2001 From: ddangcong80 Date: Mon, 8 Jul 2024 22:29:34 +0900 Subject: [PATCH] feat: Implement horizontal RecyclerView to display saved places --- .../campus/tech/kakao/map/model/SavePlace.kt | 1 - .../kakao/map/repository/SearchRepository.kt | 34 ++++++++++++++++ .../tech/kakao/map/view/SavePlaceAdapter.kt | 40 +++++++++++++++++++ .../tech/kakao/map/view/SearchActivity.kt | 29 +++++++++++--- .../kakao/map/viewmodel/SearchViewModel.kt | 14 ++++++- app/src/main/res/layout/activity_search.xml | 3 +- app/src/main/res/layout/saveplace_item.xml | 5 ++- app/src/main/res/values/strings.xml | 1 + 8 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/campus/tech/kakao/map/view/SavePlaceAdapter.kt 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 index 9c3cd897..1a39ecdc 100644 --- a/app/src/main/java/campus/tech/kakao/map/model/SavePlace.kt +++ b/app/src/main/java/campus/tech/kakao/map/model/SavePlace.kt @@ -2,5 +2,4 @@ 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 fd4afcfa..f16417e6 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 @@ -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) @@ -113,4 +114,37 @@ class SearchRepository(context: Context) { db.insert(PlaceContract.SavePlaceEntry.TABLE_NAME, null, values) } + fun showSavePlace(): MutableList { + val db: SQLiteDatabase = dbHelper.readableDatabase + val savePlaces = mutableListOf() + 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 + } + } \ No newline at end of file diff --git a/app/src/main/java/campus/tech/kakao/map/view/SavePlaceAdapter.kt b/app/src/main/java/campus/tech/kakao/map/view/SavePlaceAdapter.kt new file mode 100644 index 00000000..0b3ffc6a --- /dev/null +++ b/app/src/main/java/campus/tech/kakao/map/view/SavePlaceAdapter.kt @@ -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 +) : RecyclerView.Adapter() { + 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 + } +} \ 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 3664f2d4..d0de828c 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 @@ -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) @@ -30,33 +32,47 @@ class SearchActivity : AppCompatActivity() { this, ViewModelProvider.AndroidViewModelFactory.getInstance(application) )[SearchViewModel::class.java] - editText = findViewById(R.id.searchText) - cancelBtn = findViewById(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(R.id.searchText) + cancelBtn = findViewById(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()) @@ -83,4 +99,7 @@ class SearchActivity : AppCompatActivity() { noSearchLayout.visibility = View.VISIBLE } } + + + } diff --git a/app/src/main/java/campus/tech/kakao/map/viewmodel/SearchViewModel.kt b/app/src/main/java/campus/tech/kakao/map/viewmodel/SearchViewModel.kt index 71ab6891..aca7cfe0 100644 --- a/app/src/main/java/campus/tech/kakao/map/viewmodel/SearchViewModel.kt +++ b/app/src/main/java/campus/tech/kakao/map/viewmodel/SearchViewModel.kt @@ -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> = MutableLiveData() + private val _savePlaces: MutableLiveData> = MutableLiveData() + val places: LiveData> get() = _places + val savePlaces: LiveData> get() = _savePlaces + fun insertDummyData(name: String, address: String, category: String) { searchRepo.insertPlaceDummyData(name, address, category) @@ -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() + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index 78954ab4..5316c68d 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -1,6 +1,5 @@ diff --git a/app/src/main/res/layout/saveplace_item.xml b/app/src/main/res/layout/saveplace_item.xml index 643416bc..3de9719d 100644 --- a/app/src/main/res/layout/saveplace_item.xml +++ b/app/src/main/res/layout/saveplace_item.xml @@ -1,7 +1,8 @@ + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginEnd="15dp"> 장소 카테고리 장소 + 검색기록삭제 \ No newline at end of file