From 86325193b51e9a2b7dd00106955c2aecc1ecf2bc Mon Sep 17 00:00:00 2001 From: Gouri Panda Date: Mon, 5 Dec 2022 01:03:02 +0530 Subject: [PATCH] Added NewRecentSearchRoomDao and deprecated NewRecentSearchDao --- .../core/dao/NewRecentSearchDao.kt | 1 + .../core/dao/NewRecentSearchRoomDao.kt | 64 +++++++++++++++++++ .../dao/entities/RecentSearchRoomEntity.kt | 33 ++++++++++ 3 files changed, 98 insertions(+) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchRoomDao.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt index cd3a8411d..0093f2bb7 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt @@ -27,6 +27,7 @@ import org.kiwix.kiwixmobile.core.data.local.entity.RecentSearch import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem import javax.inject.Inject +@Deprecated(message = "Replaced with Room") class NewRecentSearchDao @Inject constructor( private val box: Box, private val flowBuilder: FlowBuilder diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchRoomDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchRoomDao.kt new file mode 100644 index 000000000..9e0043914 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchRoomDao.kt @@ -0,0 +1,64 @@ +/* + * Kiwix Android + * Copyright (c) 2022 Kiwix + * 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 . + * + */ + +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> + + fun recentSearches(zimId: String?): Flow> { + 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 + ) { + val searchRoomEntityList = box.all + searchRoomEntityList.forEach { + saveSearch(it.searchTerm, it.id) + } + } + + companion object { + private const val NUM_RECENT_RESULTS = 100 + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/RecentSearchRoomEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/RecentSearchRoomEntity.kt index 4806b7070..5e6f4fc8e 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/RecentSearchRoomEntity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/RecentSearchRoomEntity.kt @@ -1,2 +1,35 @@ +/* + * Kiwix Android + * Copyright (c) 2022 Kiwix + * 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 . + * + */ + 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 + ) +}