Skip to content

Commit

Permalink
[impro] Improves search widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderPaddyS committed Mar 14, 2024
1 parent 48b443e commit 501a306
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
*/
package org.secuso.privacyfriendlynotes.ui.main

import android.content.Context
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.preference.PreferenceManager
import android.util.Log
import android.util.TypedValue
import android.view.ContextThemeWrapper
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
Expand Down Expand Up @@ -63,6 +69,7 @@ import org.secuso.privacyfriendlynotes.ui.notes.SketchActivity
import org.secuso.privacyfriendlynotes.ui.notes.TextNoteActivity
import java.util.Collections


/**
* The MainActivity includes the functionality of the primary screen.
* It provides the possibility to access existing notes and add new ones.
Expand Down Expand Up @@ -185,7 +192,14 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
return true
}
})

searchView.setOnClickListener { searchView.onActionViewExpanded() }
searchView.setOnQueryTextFocusChangeListener { _, focus ->
TypedValue().apply {
Log.d("Focus", focus.toString())
this@MainActivity.theme.resolveAttribute(if (focus) R.attr.colorSurfaceVariant else R.attr.colorBackground, this, true)
searchView.setBackgroundColor(this.data)
}
}

/*
* Handels when a note is clicked.
Expand Down Expand Up @@ -214,6 +228,24 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
AppCompatDelegate.setDefaultNightMode(theme!!.toInt())
}

// taken from https://dev.to/ahmmedrejowan/hide-the-soft-keyboard-and-remove-focus-from-edittext-in-android-ehp on 14/03/2024
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
val v = currentFocus
if (v is EditText) {
Rect().apply {
v.getGlobalVisibleRect(this)
if (!this.contains(event.rawX.toInt(), event.rawY.toInt())) {
v.clearFocus()
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(v.getWindowToken(), 0)
}
}
}
}
return super.dispatchTouchEvent(event)
}

override fun onResume() {
super.onResume()
buildDrawerMenu()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica
}

fun categoryColor(category: Int, consumer: Consumer<String?>) {
viewModelScope.launch(Dispatchers.Default) {
viewModelScope.launch(Dispatchers.Main) {
consumer.accept(repository.categoryDao().getCategoryColor(category))
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
android:layout_height="wrap_content"
android:actionViewClass="androidx.appcompat.widget.SearchView"
android:theme="@style/AppTheme.SearchView"
app:iconifiedByDefault="false"
app:queryBackground="@color/transparent"
android:imeOptions="flagNoExtractUi" />

<androidx.recyclerview.widget.RecyclerView
Expand Down

0 comments on commit 501a306

Please sign in to comment.