Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Search Events #25

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading