1 /* 2 * Copyright 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.appfunctions.internal 18 19 import android.content.Context 20 import android.os.Build 21 import androidx.annotation.RequiresApi 22 import androidx.appsearch.app.GlobalSearchSession 23 import androidx.appsearch.app.SearchResult 24 import androidx.appsearch.app.SearchResults 25 import androidx.appsearch.platformstorage.PlatformStorage 26 import kotlinx.coroutines.guava.await 27 28 /** 29 * Create a [GlobalSearchSession] asynchronously. 30 * 31 * @param context the [Context] for the application. 32 * @return a new [GlobalSearchSession]. 33 */ 34 @RequiresApi(Build.VERSION_CODES.S) createSearchSessionnull35internal suspend fun createSearchSession(context: Context): GlobalSearchSession { 36 return PlatformStorage.createGlobalSearchSessionAsync( 37 PlatformStorage.GlobalSearchContext.Builder(context) 38 .setWorkerExecutor(Runnable::run) 39 .build() 40 ) 41 .await() 42 } 43 44 /** 45 * Reads all [SearchResults] and returns a list of [T] by transforming the [SearchResult] to [T] or 46 * null. 47 * 48 * @param T the type of document to transform the [SearchResult] to. 49 * @param transformToDocumentClassOrNull a function to transform the [SearchResult] to [T] or `null` 50 * if it can't. 51 * @return a list of [T] instances or null which contains the results from the query. Returns an 52 * empty list if nothing is found. 53 */ readAllnull54internal suspend fun <T : Any> SearchResults.readAll( 55 transformToDocumentClassOrNull: (SearchResult) -> T? 56 ): List<T?> { 57 return buildList<T?> { 58 var nextPage = nextPageAsync.await() 59 while (nextPage.isNotEmpty()) { 60 nextPage.map(transformToDocumentClassOrNull).forEach(::add) 61 nextPage = nextPageAsync.await() 62 } 63 } 64 } 65