diff --git a/app/src/main/java/org/inu/events/lib/spinner/UniSpinner.kt b/app/src/main/java/org/inu/events/lib/spinner/UniSpinner.kt new file mode 100644 index 0000000..22e621f --- /dev/null +++ b/app/src/main/java/org/inu/events/lib/spinner/UniSpinner.kt @@ -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 + private lateinit var selectedText: TextView + private val popUp = ListPopupWindow(context) + private lateinit var adapter: ArrayAdapter + + 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(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 + } +} \ No newline at end of file diff --git a/app/src/main/java/org/inu/events/ui/binding/UniSpinnerBinding.kt b/app/src/main/java/org/inu/events/ui/binding/UniSpinnerBinding.kt new file mode 100644 index 0000000..ff53c13 --- /dev/null +++ b/app/src/main/java/org/inu/events/ui/binding/UniSpinnerBinding.kt @@ -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) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/inu/events/ui/home/HomeViewModel.kt b/app/src/main/java/org/inu/events/ui/home/HomeViewModel.kt index eb1749e..962b3b4 100644 --- a/app/src/main/java/org/inu/events/ui/home/HomeViewModel.kt +++ b/app/src/main/java/org/inu/events/ui/home/HomeViewModel.kt @@ -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 @@ -40,6 +39,11 @@ class HomeViewModel : ViewModel(), KoinComponent { val likeClickEvent = SingleLiveEvent() val shouldRefresh = SingleLiveEvent() 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) @@ -54,6 +58,10 @@ class HomeViewModel : ViewModel(), KoinComponent { } } + init { + getHomeData(categoryId, eventStatus) + } + fun refresh() { shouldRefresh.call() } @@ -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) } diff --git a/app/src/main/java/org/inu/events/ui/home/MainActivity.kt b/app/src/main/java/org/inu/events/ui/home/MainActivity.kt index c11c376..9af7ecd 100644 --- a/app/src/main/java/org/inu/events/ui/home/MainActivity.kt +++ b/app/src/main/java/org/inu/events/ui/home/MainActivity.kt @@ -36,7 +36,6 @@ class MainActivity : AppCompatActivity(), LoginDialog.LoginDialog { setupRecyclerView() setUpSwipeRefresh() setupRefreshEvent() - setupSpinner() } override fun onResume() { @@ -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) diff --git a/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt b/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt index 89f3f26..182fc26 100644 --- a/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt +++ b/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt @@ -25,6 +25,7 @@ class SearchActivity : AppCompatActivity() { } initRecyclerView() + initBackButton() observeSearch() setContentView(binding.root) @@ -44,4 +45,10 @@ class SearchActivity : AppCompatActivity() { } } } + + private fun initBackButton() { + binding.tvBack.setOnClickListener { + finish() + } + } } \ No newline at end of file diff --git a/app/src/main/java/org/inu/events/ui/home/SpinnerAdapter.kt b/app/src/main/java/org/inu/events/ui/home/SpinnerAdapter.kt index 5ff05be..8ab0ae9 100644 --- a/app/src/main/java/org/inu/events/ui/home/SpinnerAdapter.kt +++ b/app/src/main/java/org/inu/events/ui/home/SpinnerAdapter.kt @@ -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, - private val spinnerTitle: String -) : ArrayAdapter(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 - } -} \ No newline at end of file +//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, +// private val spinnerTitle: String +//) : ArrayAdapter(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 +// } +//} \ No newline at end of file diff --git a/app/src/main/java/org/inu/events/ui/util/toolbar/ToolbarListener.kt b/app/src/main/java/org/inu/events/ui/util/toolbar/ToolbarListener.kt index b160ef1..a44b2d4 100644 --- a/app/src/main/java/org/inu/events/ui/util/toolbar/ToolbarListener.kt +++ b/app/src/main/java/org/inu/events/ui/util/toolbar/ToolbarListener.kt @@ -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) } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_arrow_down.xml b/app/src/main/res/drawable/ic_arrow_down.xml index 983a153..ac804f1 100644 --- a/app/src/main/res/drawable/ic_arrow_down.xml +++ b/app/src/main/res/drawable/ic_arrow_down.xml @@ -1,5 +1,5 @@ - + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e508c2b..89e3e66 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -22,42 +22,27 @@ app:layout_constraintTop_toTopOf="parent" app:listener="@{mainViewModel.toolbarListener}" /> - + app:layout_constraintTop_toBottomOf="@id/include" + app:setOnItemClick="@{mainViewModel.onCategoryItemClick}" /> - + 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}" /> + - + + + + - + app:contentInsetStart="0dp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + android:layout_marginStart="@dimen/horizontal_margin" + android:src="@drawable/ic_toolbar_logo" /> + android:src="@drawable/ic_person" + android:visibility="@{listener != null ? View.VISIBLE : View.GONE }" /> + + + diff --git a/app/src/main/res/layout/uni_spinner.xml b/app/src/main/res/layout/uni_spinner.xml index 707ae7e..57324a3 100644 --- a/app/src/main/res/layout/uni_spinner.xml +++ b/app/src/main/res/layout/uni_spinner.xml @@ -1,29 +1,24 @@ - - - + tools:text="카테고리" /> + \ No newline at end of file diff --git a/app/src/main/res/layout/uni_spinner_popup.xml b/app/src/main/res/layout/uni_spinner_item.xml similarity index 51% rename from app/src/main/res/layout/uni_spinner_popup.xml rename to app/src/main/res/layout/uni_spinner_item.xml index 577010d..a87fef8 100644 --- a/app/src/main/res/layout/uni_spinner_popup.xml +++ b/app/src/main/res/layout/uni_spinner_item.xml @@ -1,12 +1,13 @@ \ No newline at end of file + app:layout_constraintTop_toTopOf="parent" + tools:text="학생회" /> \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index f5e7e4a..f91b4b9 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -4,4 +4,9 @@ + + + + + \ No newline at end of file