Skip to content

Commit

Permalink
📌 Search feature implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbatovK committed Apr 13, 2022
1 parent dc52e3f commit 3785387
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class QuizAdapter(
quiz?.let {
with(binding) {

card.setCardBackgroundColor(getRandomColor())
title.text = it.name
val qString = root.context.resources.getQuantityString(R.plurals.question_plurals, it.questions.size)
count.text = root.context.getString(R.string.question_size, it.questions.size, qString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import android.view.*
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.albatros.kquiz.R
import com.albatros.kquiz.databinding.ClientFragmentBinding
import com.albatros.kquiz.model.data.info.ClientInfo
Expand All @@ -24,7 +22,7 @@ class ClientFragment : Fragment(), MainActivity.IOnBackPressed {
private val viewModel: ClientViewModel by viewModel()

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.custom_menu, menu)
inflater.inflate(R.menu.empty_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class EnterFragment : Fragment(), MainActivity.IOnBackPressed {
val id = dialogBinding.idEdit.text.toString()
viewModel.enterSession(id, name)
it.cancel()
(activity as? MainActivity)?.onUserInteraction()
}
}.create()
dialog.window?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlinx.coroutines.launch

class EnterViewModel(private val api: ApiService, private val repo: ClientRepo) : ViewModel() {

private val _userId: MutableLiveData<Long?> = MutableLiveData()
private val _userId: MutableLiveData<Long?> = MutableLiveData<Long?>()

val userId: LiveData<Long?> = _userId

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class GameFragment : Fragment(), MainActivity.IOnBackPressed {
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.custom_menu, menu)
inflater.inflate(R.menu.empty_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ class HostFragment : Fragment(), MainActivity.IOnBackPressed {
dialogBinding.sessionIdTxt.text = viewModel.getSessionId().toString()
val dialog = MaterialAlertDialogBuilder(binding.root.context).apply {
setView(dialogBinding.root)
setNegativeButton("Закрыть") { it, _ -> it.cancel() }
setNegativeButton("Закрыть") { it, _ -> it.cancel()
(activity as? MainActivity)?.onUserInteraction()
}
setPositiveButton("Поделиться") { it, _ ->
val intent = Intent(Intent.ACTION_SEND)
val shareBody = dialogBinding.sessionIdTxt.text.toString()
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, shareBody)
startActivity(Intent.createChooser(intent, "Invitation to the game"))
it.cancel()
(activity as? MainActivity)?.onUserInteraction()
}
}.create()
dialog.window?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ class ListViewModel(private val api: ApiService, private val repo: ClientRepo) :
}
}

fun loadQuizList() {
viewModelScope.launch {
_quizzes.value = try {
api.getQuizzes()
} catch (e: Exception) {
null
}
}
}

fun fetchByTopics(query: String) {
viewModelScope.launch {
_quizzes.value = try {
api.getQuizzes().filter {
it.name.lowercase().contains(query, ignoreCase = true) || query.lowercase() in it.topics.map(String::lowercase)
}
} catch (e: Exception) {
null
}
}
}

val quizzes: LiveData<List<Quiz>?> = _quizzes

private val _sessionId: MutableLiveData<Long?> = MutableLiveData()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.albatros.kquiz.ui.fragments.list

import android.app.SearchManager
import android.content.Context
import android.os.Bundle
import android.transition.TransitionInflater
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.*
import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
Expand All @@ -17,6 +18,7 @@ import com.albatros.kquiz.R
import com.albatros.kquiz.databinding.NameDialogBinding
import com.albatros.kquiz.databinding.QuizListFragmentBinding
import com.albatros.kquiz.model.data.pojo.Quiz
import com.albatros.kquiz.ui.activity.MainActivity
import com.albatros.kquiz.ui.adapter.quiz.QuizAdapter
import com.albatros.kquiz.ui.adapter.quiz.QuizAdapterListener
import com.google.android.flexbox.FlexDirection
Expand Down Expand Up @@ -74,6 +76,44 @@ class QuizListFragment : Fragment() {
showNameInputDialog(it)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.quiz_list_menu, menu)

val searchView = menu.findItem(R.id.action_search).actionView as SearchView

val listener = object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
viewModel.loadQuizList()
return true
}
override fun onMenuItemActionExpand(item: MenuItem?): Boolean = true
}

menu.findItem(R.id.action_search).setOnActionExpandListener(listener)

searchView.setOnCloseListener {
viewModel.loadQuizList()
true
}

val queryListener = object: SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
query?.trim()?.lowercase()?.let { viewModel.fetchByTopics(query) }
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
return false
}
}

searchView.setOnQueryTextListener(queryListener)
val manager = context?.getSystemService(Context.SEARCH_SERVICE) as SearchManager
val info = manager.getSearchableInfo(activity?.componentName)
searchView.setSearchableInfo(info)

return super.onCreateOptionsMenu(menu, inflater)
}

private fun showNameInputDialog(id: Long) {
val dialogBinding = NameDialogBinding.inflate(layoutInflater)
val dialog = MaterialAlertDialogBuilder(binding.root.context).apply {
Expand All @@ -82,6 +122,7 @@ class QuizListFragment : Fragment() {
val name = dialogBinding.nameEdit.text.toString().ifBlank { "NoName" }
viewModel.enterSession(id, name)
it.cancel()
(activity as? MainActivity)?.onUserInteraction()
}
}.create()
dialog.window?.let {
Expand All @@ -102,6 +143,8 @@ class QuizListFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)

postponeEnterTransition()
sharedElementEnterTransition = TransitionInflater.from(context)
.inflateTransition(android.R.transition.move)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ResultFragment : Fragment(), MainActivity.IOnBackPressed {
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.custom_menu, menu)
inflater.inflate(R.menu.empty_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable-v24/ic_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="32dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="32dp" 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>
3 changes: 2 additions & 1 deletion app/src/main/res/layout/answer_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
android:foregroundGravity="center"
android:layout_height="wrap_content"
android:background="@color/white"
android:elevation="6dp"
android:elevation="0dp"
app:cardMaxElevation="0dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="8dp"
app:contentPadding="16dp"
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/layout/game_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
android:background="@color/white"
android:id="@+id/card"
android:elevation="6dp"
android:paddingStart="8dp"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingBottom="16dp"
app:cardElevation="12dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="24dp"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -29,8 +28,9 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"

android:autoSizeTextType="uniform"
android:layout_marginBottom="16dp"
android:autoSizeMinTextSize="20sp"
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/res/layout/quiz_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_marginBottom="4dp"
android:padding="8dp"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:visibility="invisible">

<androidx.cardview.widget.CardView
android:id="@+id/card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/dimgrey"
android:elevation="6dp"
android:elevation="8dp"
android:layout_margin="8dp"
app:cardBackgroundColor="@color/dimgrey"
app:cardCornerRadius="8dp"
app:contentPadding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:cardElevation="8dp"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:clipChildren="false"
android:gravity="start"
android:orientation="vertical">

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/quiz_list_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
tools:context=".ui.fragments.list.QuizListFragment">

<androidx.core.widget.NestedScrollView
android:paddingTop="30dp"
android:id="@+id/scroll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions app/src/main/res/menu/quiz_list_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:title="@string/search_hint"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
2 changes: 1 addition & 1 deletion app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<fragment
android:id="@+id/QuizListFragment"
android:name="com.albatros.kquiz.ui.fragments.list.QuizListFragment"
android:label="Выбор варианта"
android:label=""
tools:layout="@layout/quiz_list_fragment">

<action
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 @@ -2,4 +2,5 @@
<string name="app_name">KQuiz</string>
<string name="question_size">%d %s</string>
<string name="place">%d место</string>
<string name="search_hint">Поиск</string>
</resources>

0 comments on commit 3785387

Please sign in to comment.