mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 05:04:50 -04:00
Added NewRecentSearchRoomDao and deprecated NewRecentSearchDao
This commit is contained in:
parent
cc7291fcb6
commit
86325193b5
@ -27,6 +27,7 @@ import org.kiwix.kiwixmobile.core.data.local.entity.RecentSearch
|
|||||||
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
|
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@Deprecated(message = "Replaced with Room")
|
||||||
class NewRecentSearchDao @Inject constructor(
|
class NewRecentSearchDao @Inject constructor(
|
||||||
private val box: Box<RecentSearchEntity>,
|
private val box: Box<RecentSearchEntity>,
|
||||||
private val flowBuilder: FlowBuilder
|
private val flowBuilder: FlowBuilder
|
||||||
|
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Kiwix Android
|
||||||
|
* Copyright (c) 2022 Kiwix <android.kiwix.org>
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.kiwixmobile.core.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Query
|
||||||
|
import io.objectbox.Box
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity
|
||||||
|
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchRoomEntity
|
||||||
|
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
abstract class NewRecentSearchRoomDao {
|
||||||
|
|
||||||
|
@Query("SELECT * FROM RecentSearchRoomEntity WHERE id LIKE :zimId ORDER BY RecentSearchRoomEntity.id DESC")
|
||||||
|
abstract fun search(zimId: String?): Flow<List<RecentSearchRoomEntity>>
|
||||||
|
|
||||||
|
fun recentSearches(zimId: String?): Flow<List<SearchListItem.RecentSearchListItem>> {
|
||||||
|
return search(zimId = zimId).map { searchEntities ->
|
||||||
|
searchEntities.distinctBy(RecentSearchRoomEntity::searchTerm).take(NUM_RECENT_RESULTS)
|
||||||
|
.map { searchEntity -> SearchListItem.RecentSearchListItem(searchEntity.searchTerm) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Query("INSERT INTO RecentSearchRoomEntity(searchTerm, zimId) VALUES (:title , :id)")
|
||||||
|
abstract fun saveSearch(title: String, id: Long)
|
||||||
|
|
||||||
|
@Query("DELETE FROM RecentSearchRoomEntity WHERE searchTerm=:searchTerm")
|
||||||
|
abstract fun deleteSearchString(searchTerm: String)
|
||||||
|
|
||||||
|
@Query("DELETE FROM RecentSearchRoomEntity")
|
||||||
|
abstract fun deleteSearchHistory()
|
||||||
|
|
||||||
|
fun migrationToRoomInsert(
|
||||||
|
box: Box<RecentSearchEntity>
|
||||||
|
) {
|
||||||
|
val searchRoomEntityList = box.all
|
||||||
|
searchRoomEntityList.forEach {
|
||||||
|
saveSearch(it.searchTerm, it.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val NUM_RECENT_RESULTS = 100
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Kiwix Android
|
||||||
|
* Copyright (c) 2022 Kiwix <android.kiwix.org>
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
package org.kiwix.kiwixmobile.core.dao.entities
|
package org.kiwix.kiwixmobile.core.dao.entities
|
||||||
|
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
import org.kiwix.kiwixmobile.core.data.local.entity.RecentSearch
|
||||||
|
|
||||||
|
@androidx.room.Entity
|
||||||
|
data class RecentSearchRoomEntity(
|
||||||
|
@PrimaryKey var id: Long = 0L,
|
||||||
|
val searchTerm: String,
|
||||||
|
val zimId: String
|
||||||
|
) {
|
||||||
|
constructor(recentSearch: RecentSearch) : this(
|
||||||
|
0,
|
||||||
|
recentSearch.searchString,
|
||||||
|
recentSearch.zimID
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user