Skip to content

Implement App Search #3

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

Merged
merged 1 commit into from
Mar 17, 2025
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
15 changes: 15 additions & 0 deletions FastAppSearch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions FastAppSearch/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
75 changes: 75 additions & 0 deletions FastAppSearch/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
id("com.google.devtools.ksp")
kotlin("kapt")
}

android {
namespace = "com.dezzy.fastappsearch"
compileSdk = 34

defaultConfig {
applicationId = "com.dezzy.fastappsearch"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.7"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)

implementation(libs.androidx.appsearch)
kapt(libs.androidx.appsearch.compiler)
implementation(libs.androidx.appsearch.local.storage)
}
21 changes: 21 additions & 0 deletions FastAppSearch/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.dezzy.fastappsearch

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.dezzy.fastappsearch", appContext.packageName)
}
}
28 changes: 28 additions & 0 deletions FastAppSearch/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FastAppSearch"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.FastAppSearch">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.dezzy.fastappsearch

import android.content.Context
import androidx.appsearch.app.AppSearchSession
import androidx.appsearch.app.PutDocumentsRequest
import androidx.appsearch.app.SearchSpec
import androidx.appsearch.app.SetSchemaRequest
import androidx.appsearch.localstorage.LocalStorage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class AppSearchManager(
private val appContext: Context
) {
private var session: AppSearchSession? = null
suspend fun init() {
withContext(Dispatchers.IO) {
val sessionFuture = LocalStorage.createSearchSessionAsync(
LocalStorage.SearchContext.Builder(
appContext,
"todo"
).build()
)

val setSchemaRequest = SetSchemaRequest.Builder()
.addDocumentClasses(Todo::class.java)
.build()

session = sessionFuture.get()

session?.setSchemaAsync(setSchemaRequest)
}
}

suspend fun addTodos(todos: List<Todo>): Boolean {
return withContext(Dispatchers.IO) {
session?.putAsync(
PutDocumentsRequest.Builder()
.addDocuments(todos)
.build()
)?.get()?.isSuccess == true
}
}

suspend fun searchTodos(query: String): List<Todo> {
return withContext(Dispatchers.IO) {
val searchSpec = SearchSpec.Builder()
.setSnippetCount(10)
.addFilterNamespaces("my_todos")
.setRankingStrategy(SearchSpec.RANKING_STRATEGY_USAGE_COUNT)
.build()
val result = session?.search(
query,
searchSpec
) ?: return@withContext emptyList()

val page = result.nextPageAsync.get()

page.mapNotNull {
if(it.genericDocument.schemaType == Todo::class.java.simpleName) {
it.getDocument(Todo::class.java)
} else null
}
}
}

fun closeSession() {
session?.close()
session = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.dezzy.fastappsearch

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.dezzy.fastappsearch.ui.theme.FastAppSearchTheme

class MainActivity : ComponentActivity() {

private val viewModel: MainViewModel by viewModels(
factoryProducer = {
object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return MainViewModel(AppSearchManager(applicationContext)) as T
}
}
}
)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
FastAppSearchTheme {
val state = viewModel.state
Column(modifier = Modifier.fillMaxSize()) {
TextField(
value = state.searchText,
onValueChange = viewModel::onSearchQueryChange,
modifier = Modifier
.padding(8.dp)
.fillMaxWidth(),
placeholder = {
Text(text = "enter search")
}
)
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.weight(1f),
contentPadding = PaddingValues(8.dp)
) {
items(state.todos) {todo ->
TodoItem(
todo = todo,
onDoneChange = { isDone ->
viewModel.onDoneChange(todo, isDone)
}
)
}

}
}
}
}
}

@Composable
private fun TodoItem(
todo: Todo,
onDoneChange: (Boolean) -> Unit,
modifier: Modifier = Modifier
) {
Row(
modifier = modifier
.fillMaxWidth()
.padding(8.dp)
) {
Column(modifier = Modifier.weight(1f)) {
Text(text = todo.title, fontSize = 16.sp)
Text(text = todo.description, fontSize = 10.sp)
}
Checkbox(checked = todo.isDone, onCheckedChange = onDoneChange)
}
}
}
Loading