Skip to content

Commit

Permalink
Merge pull request #20 from inu-appcenter/feat/spinner
Browse files Browse the repository at this point in the history
스피너(홈화면 카테고리, 필터) 수정 및 홈화면에서 검색 화면 들어가도록 구현
  • Loading branch information
jhg3410 authored Jan 14, 2023
2 parents a154eb6 + 6baaa39 commit 56f0955
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 117 deletions.
92 changes: 92 additions & 0 deletions app/src/main/java/org/inu/events/lib/spinner/UniSpinner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.inu.events.lib.spinner

import android.content.Context
import android.util.AttributeSet
import android.widget.ArrayAdapter
import android.widget.FrameLayout
import android.widget.ListPopupWindow
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import org.inu.events.R

class UniSpinner @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

private lateinit var items: Array<CharSequence>
private lateinit var selectedText: TextView
private val popUp = ListPopupWindow(context)
private lateinit var adapter: ArrayAdapter<CharSequence>

init {
inflate(context, R.layout.uni_spinner, this)

setAttrs(attrs)
setAdapter()
setPopup()
setSelectedText()
}

private fun setAttrs(attrs: AttributeSet?) {
context.obtainStyledAttributes(attrs, R.styleable.UniSpinner).run {
items = getTextArray(R.styleable.UniSpinner_items) ?: arrayOf("전체", "간식나눔", "공고")
recycle()
}
}


private fun setAdapter() {
adapter = ArrayAdapter(context, R.layout.uni_spinner_item, items)
popUp.setAdapter(adapter)
}

private fun measureWidth(): Int {
var width = 0
for (i in items.indices) {
val view = adapter.getView(i, null, FrameLayout(context))
view.measure(0, 0)
width = width.coerceAtLeast(view.measuredWidth)
}
return width
}

private fun setSelectedText() {
selectedText = findViewById<TextView>(R.id.selected_item).apply {
text = items.first()
width = popUp.width
setOnClickListener {
if (popUp.isShowing) {
popUp.dismiss()
} else {
popUp.show()
}
}
}
}

private fun setPopup() {
popUp.apply {
isModal = true
anchorView = this@UniSpinner
verticalOffset = this.height + DISTANCE_POPUP_TITLE
width = measureWidth() + MARGIN_END_POPUP_ITEM
setBackgroundDrawable(context.getDrawable(R.drawable.popup_background))
}

}

fun setOnItemClick(onClick: (pos:Int) -> Unit) {
popUp.setOnItemClickListener { _, _, position, _ ->
selectedText.text = items[position]
popUp.dismiss()
onClick(position)
}
}

companion object {
const val DISTANCE_POPUP_TITLE = 8 * 3
const val MARGIN_END_POPUP_ITEM = 16 * 3
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/org/inu/events/ui/binding/UniSpinnerBinding.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.inu.events.ui.binding

import androidx.databinding.BindingAdapter
import org.inu.events.lib.spinner.UniSpinner

@BindingAdapter("setOnItemClick")
fun bindSetOnItemClick(view: UniSpinner, onClick: (position: Int) -> Unit) {
view.setOnItemClick { position ->
onClick(position)
}
}
14 changes: 11 additions & 3 deletions app/src/main/java/org/inu/events/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.inu.events.ui.home

import android.content.Intent
import android.view.View
import android.view.ViewParent
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
Expand Down Expand Up @@ -40,6 +39,11 @@ class HomeViewModel : ViewModel(), KoinComponent {
val likeClickEvent = SingleLiveEvent<Any>()
val shouldRefresh = SingleLiveEvent<Any>()
val toolbarListener = object : ToolbarListener {
override fun onClickSearch(view: View) {
val intent = Intent(view.context, SearchActivity::class.java)
view.context.startActivity(intent)
}

override fun onClickMyPage(view: View) {
if (loginService.isLoggedIn) {
val intent = Intent(view.context, MyPageActivity::class.java)
Expand All @@ -54,6 +58,10 @@ class HomeViewModel : ViewModel(), KoinComponent {
}
}

init {
getHomeData(categoryId, eventStatus)
}

fun refresh() {
shouldRefresh.call()
}
Expand Down Expand Up @@ -100,12 +108,12 @@ class HomeViewModel : ViewModel(), KoinComponent {
}
}

fun onCategoryItemClick(parent: ViewParent, view: View, position: Int, id: Long) {
val onCategoryItemClick = fun(position: Int) {
categoryId = position
getHomeData(categoryId, eventStatus)
}

fun onFilterItemClick(parent: ViewParent, view: View, position: Int, id: Long) {
val onFilterItemClick = fun(position: Int) {
eventStatus = position == 1
getHomeData(categoryId, eventStatus)
}
Expand Down
21 changes: 10 additions & 11 deletions app/src/main/java/org/inu/events/ui/home/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class MainActivity : AppCompatActivity(), LoginDialog.LoginDialog {
setupRecyclerView()
setUpSwipeRefresh()
setupRefreshEvent()
setupSpinner()
}

override fun onResume() {
Expand Down Expand Up @@ -102,16 +101,16 @@ class MainActivity : AppCompatActivity(), LoginDialog.LoginDialog {
}
}

private fun setupSpinner() {
val categoryItems = resources.getStringArray(R.array.classification)
val filterItems = resources.getStringArray(R.array.filter)

val categoryAdapter = SpinnerAdapter(this, R.layout.uni_spinner_popup, categoryItems.toMutableList(), "카테고리")
val filterAdapter = SpinnerAdapter(this, R.layout.uni_spinner_popup, filterItems.toMutableList(), "전체")

binding.spinnerClassification.adapter = categoryAdapter
binding.spinnerFilter.adapter = filterAdapter
}
// private fun setupSpinner() {
// val categoryItems = resources.getStringArray(R.array.classification)
// val filterItems = resources.getStringArray(R.array.filter)
//
// val categoryAdapter = SpinnerAdapter(this, R.layout.uni_spinner_popup, categoryItems.toMutableList(), "카테고리")
// val filterAdapter = SpinnerAdapter(this, R.layout.uni_spinner_popup, filterItems.toMutableList(), "전체")
//
// binding.spinnerClassification.adapter = categoryAdapter
// binding.spinnerFilter.adapter = filterAdapter
// }

private fun askUserForLogin() {
LoginDialog().show(this, ::onOk, ::onCancel)
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/org/inu/events/ui/home/SearchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SearchActivity : AppCompatActivity() {
}

initRecyclerView()
initBackButton()
observeSearch()

setContentView(binding.root)
Expand All @@ -44,4 +45,10 @@ class SearchActivity : AppCompatActivity() {
}
}
}

private fun initBackButton() {
binding.tvBack.setOnClickListener {
finish()
}
}
}
70 changes: 35 additions & 35 deletions app/src/main/java/org/inu/events/ui/home/SpinnerAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package org.inu.events.ui.home

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.annotation.LayoutRes
import org.inu.events.databinding.UniSpinnerBinding
import org.inu.events.databinding.UniSpinnerPopupBinding

class SpinnerAdapter(
context: Context,
@LayoutRes private val resId: Int,
private val list: MutableList<String>,
private val spinnerTitle: String
) : ArrayAdapter<String>(context, resId, list) {
override fun getCount(): Int = list.size
override fun getItem(position: Int): String = list[position]

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val binding = UniSpinnerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
binding.selectedItem.text = if (position == 0) spinnerTitle else list[position]

return binding.root
}

override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
val binding = UniSpinnerPopupBinding.inflate(LayoutInflater.from(parent.context), parent, false)
val model = list[position]
binding.item.text = model

return binding.root
}
}
//package org.inu.events.ui.home
//
//import android.content.Context
//import android.view.LayoutInflater
//import android.view.View
//import android.view.ViewGroup
//import android.widget.ArrayAdapter
//import androidx.annotation.LayoutRes
//import org.inu.events.databinding.UniSpinnerBinding
//import org.inu.events.databinding.UniSpinnerPopupBinding
//
//class SpinnerAdapter(
// context: Context,
// @LayoutRes private val resId: Int,
// private val list: MutableList<String>,
// private val spinnerTitle: String
//) : ArrayAdapter<String>(context, resId, list) {
// override fun getCount(): Int = list.size
// override fun getItem(position: Int): String = list[position]
//
// override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// val binding = UniSpinnerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
// binding.selectedItem.text = if (position == 0) spinnerTitle else list[position]
//
// return binding.root
// }
//
// override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
// val binding = UniSpinnerPopupBinding.inflate(LayoutInflater.from(parent.context), parent, false)
// val model = list[position]
// binding.item.text = model
//
// return binding.root
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package org.inu.events.ui.util.toolbar
import android.view.View

interface ToolbarListener {
fun onClickSearch(view: View)
fun onClickMyPage(view: View)
}
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/ic_arrow_down.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<vector android:height="24dp" android:viewportHeight="16"
android:viewportWidth="16" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<vector android:height="12dp" android:viewportHeight="16"
android:viewportWidth="16" android:width="12dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#00000000"
android:pathData="M13.333,5.333L8,10.667L2.667,5.333"
android:strokeColor="#808080" android:strokeLineCap="round"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>
45 changes: 15 additions & 30 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,27 @@
app:layout_constraintTop_toTopOf="parent"
app:listener="@{mainViewModel.toolbarListener}" />

<Spinner
android:id="@+id/spinner_classification"
<org.inu.events.lib.spinner.UniSpinner
android:id="@+id/us_category"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
android:background="@null"
android:entries="@array/classification"
android:onItemSelected="@{(parent, view, position, id)->mainViewModel.onCategoryItemClick(parent, view, position, id)}"
android:popupBackground="@drawable/popup_background"
android:popupElevation="4dp"
android:spinnerMode="dropdown"
android:textColor="@color/black1"
android:textSize="12sp"
android:dropDownVerticalOffset="40dp"
app:items="@array/classification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include" />
app:layout_constraintTop_toBottomOf="@id/include"
app:setOnItemClick="@{mainViewModel.onCategoryItemClick}" />

<Spinner
android:id="@+id/spinner_filter"
<org.inu.events.lib.spinner.UniSpinner
android:id="@+id/us_filter"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
android:background="@null"
android:entries="@array/filter"
android:onItemSelected="@{(parent, view, position, id)->mainViewModel.onFilterItemClick(parent, view, position, id)}"
android:popupBackground="@drawable/popup_background"
android:popupElevation="4dp"
android:spinnerMode="dropdown"
android:textColor="@color/black1"
android:textSize="12sp"
android:dropDownVerticalOffset="40dp"
app:layout_goneMarginEnd="4dp"
app:layout_constraintEnd_toStartOf="@id/spinner_classification"
app:layout_constraintTop_toBottomOf="@+id/include" />
android:layout_marginEnd="4dp"
app:items="@array/filter"
app:layout_constraintEnd_toStartOf="@id/us_category"
app:layout_constraintTop_toBottomOf="@id/include"
app:setOnItemClick="@{mainViewModel.onFilterItemClick}" />

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_layout"
Expand All @@ -68,7 +53,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spinner_classification"
app:layout_constraintTop_toBottomOf="@id/us_category"
tools:context=".ui.home.MainActivity">

<androidx.recyclerview.widget.RecyclerView
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_register_events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@
android:layout_height="@dimen/exitText_height"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="4dp"
android:background="@drawable/spinner_background"
android:background="@drawable/drawable_edit_text_background"
android:entries="@array/classification"
android:paddingHorizontal="16dp"
android:selectedItemPosition="@={registerViewModel.selectedItemPosition}"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_register2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
android:layout_height="@dimen/exitText_height"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="4dp"
android:background="@drawable/spinner_background"
android:background="@drawable/drawable_edit_text_background"
android:entries="@array/classification"
android:paddingHorizontal="8dp"
android:paddingVertical="10dp"
Expand Down
Loading

0 comments on commit 56f0955

Please sign in to comment.