Skip to content

Commit

Permalink
feat: Search Events (#25)
Browse files Browse the repository at this point in the history
* first pass at a search function refs #24

* logic to make it works is important 😅

* missed a spot

* it works!!!!@!@!@!@

just a few details to clean up

* ok no it works the way we want!

- updates list in real time as you type in search
- arrow back lets you keep search
- x button in search bar clears search
- physical back button clears search
  • Loading branch information
williscool authored Sep 27, 2024
1 parent c9125ea commit 50f5613
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class EventListAdapter(

private var events = arrayOf<EventAlertRecord>();

private var allEvents = arrayOf<EventAlertRecord>();

private var _recyclerView: RecyclerView? = null
var recyclerView: RecyclerView?
get() = _recyclerView
Expand All @@ -127,6 +129,7 @@ class EventListAdapter(
private val primaryColor: Int
private val changeString: String
private val snoozeString: String
private var searchString: String? = null

private var currentScrollPosition: Int = 0

Expand Down Expand Up @@ -372,9 +375,24 @@ class EventListAdapter(
val hasActiveEvents: Boolean
get() = events.any { it.snoozedUntil == 0L }

fun setEventsToDisplay(newEvents: Array<EventAlertRecord>)
= synchronized(this) {
events = newEvents;
fun setSearchText(query: String?) {
searchString = query
setEventsToDisplay()
}

fun setEventsToDisplay(newEvents: Array<EventAlertRecord>? = null) = synchronized(this) {

if (newEvents != null){
allEvents = newEvents
events = newEvents;
}

if (!searchString.isNullOrEmpty()){
events = allEvents.filter { ev -> searchString?.let { ev.title.lowercase().contains(it.lowercase()) } == true }.toTypedArray()
} else {
events = allEvents
}

eventsPendingRemoval.clear()
pendingEventRemoveRunnables.clear()
notifyDataSetChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.github.quarck.calnotify.ui

import android.annotation.TargetApi
import android.app.AlertDialog
import android.app.SearchManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
Expand All @@ -44,6 +45,7 @@ import android.view.View
import android.widget.ArrayAdapter
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import com.github.quarck.calnotify.*
import com.github.quarck.calnotify.app.ApplicationController
import com.github.quarck.calnotify.app.UndoManager
Expand Down Expand Up @@ -385,6 +387,43 @@ class MainActivity : AppCompatActivity(), EventListCallback {
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)

val searchMenuItem = menu.findItem(R.id.action_search)

searchMenuItem.isVisible = true
searchMenuItem.isEnabled = true

var searchView = searchMenuItem.actionView as SearchView
val manager = getSystemService(Context.SEARCH_SERVICE) as SearchManager

searchView.setSearchableInfo(manager.getSearchableInfo(componentName))

searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
adapter.setSearchText(query)
searchView.clearFocus()
searchView.setQuery(query, false)
searchMenuItem.collapseActionView()
adapter.setEventsToDisplay()
return true
}

override fun onQueryTextChange(newText: String?): Boolean {
adapter.setSearchText(newText)
adapter.setEventsToDisplay()
return true
}
})

val closebutton: View = searchView.findViewById(androidx.appcompat.R.id.search_close_btn)

closebutton.setOnClickListener {
searchView.setQuery("",false)
searchView.clearFocus()
adapter.setSearchText(null)
adapter.setEventsToDisplay()
true
}

val menuItem = menu.findItem(R.id.action_snooze_all)
if (menuItem != null) {
menuItem.isEnabled = adapter.itemCount > 0
Expand Down
22 changes: 16 additions & 6 deletions android/app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.MainActivity" >

<item
android:id="@+id/action_snooze_all"
android:orderInCategory="90"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_update_white_24dp"
android:title="@string/snooze_all" />
<item
android:id="@+id/action_snooze_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_update_white_24dp"
android:orderInCategory="91"
android:title="@string/snooze_all"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:orderInCategory="92"
android:title="@string/search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView"/>

<item
android:id="@+id/action_dismissed_events"
Expand Down
2 changes: 2 additions & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
<string name="projectPageOnGitHub">Project page on GitHub</string>
<string name="snooze_all">Snooze All</string>
<string name="change_all">Change All</string>
<string name="search">Search</string>

<string name="snooze_all_confirmation">Snooze ALL events?\nAlready snoozed would also change snooze time unless snoozed to longer period</string>
<string name="change_all_notification">This will change snooze time for all events\nContinue?</string>

Expand Down

0 comments on commit 50f5613

Please sign in to comment.