Annual refactor
This commit is contained in:
@@ -45,12 +45,7 @@ dependencies {
|
||||
implementation(project(":data:calendar"))
|
||||
implementation(project(":core:database"))
|
||||
implementation(project(":core:preferences"))
|
||||
implementation(project(":data:applications"))
|
||||
implementation(project(":data:appshortcuts"))
|
||||
implementation(project(":data:contacts"))
|
||||
implementation(project(":core:ktx"))
|
||||
implementation(project(":data:files"))
|
||||
implementation(project(":data:websites"))
|
||||
implementation(project(":data:wikipedia"))
|
||||
implementation(project(":services:badges"))
|
||||
implementation(project(":core:crashreporter"))
|
||||
|
||||
@@ -6,6 +6,8 @@ import de.mm20.launcher2.icons.ColorLayer
|
||||
import de.mm20.launcher2.icons.StaticLauncherIcon
|
||||
import de.mm20.launcher2.icons.TextLayer
|
||||
import de.mm20.launcher2.search.SavableSearchable
|
||||
import de.mm20.launcher2.search.SearchableSerializer
|
||||
import de.mm20.launcher2.searchable.TagSerializer
|
||||
|
||||
data class Tag(
|
||||
val tag: String,
|
||||
@@ -33,6 +35,10 @@ data class Tag(
|
||||
)
|
||||
}
|
||||
|
||||
override fun getSerializer(): SearchableSerializer {
|
||||
return TagSerializer()
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val Domain = "tag"
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package de.mm20.launcher2.searchable
|
||||
|
||||
import de.mm20.launcher2.search.SearchableDeserializer
|
||||
import de.mm20.launcher2.search.data.Tag
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.core.qualifier.named
|
||||
import org.koin.dsl.module
|
||||
|
||||
val searchableModule = module {
|
||||
single<SearchableRepository> { SearchableRepositoryImpl(androidContext(), get(), get()) }
|
||||
factory <SavableSearchableRepository> { SavableSearchableRepositoryImpl(androidContext(), get(), get()) }
|
||||
factory<SearchableDeserializer>(named(Tag.Domain)) { TagDeserializer() }
|
||||
}
|
||||
+18
-7
@@ -24,9 +24,13 @@ import kotlinx.coroutines.withContext
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONException
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.get
|
||||
import org.koin.core.error.InstanceCreationException
|
||||
import org.koin.core.error.NoBeanDefFoundException
|
||||
import org.koin.core.qualifier.named
|
||||
import java.io.File
|
||||
|
||||
interface SearchableRepository {
|
||||
interface SavableSearchableRepository {
|
||||
|
||||
fun insert(
|
||||
searchable: SavableSearchable,
|
||||
@@ -114,11 +118,11 @@ interface SearchableRepository {
|
||||
suspend fun cleanupDatabase(): Int
|
||||
}
|
||||
|
||||
internal class SearchableRepositoryImpl(
|
||||
internal class SavableSearchableRepositoryImpl(
|
||||
private val context: Context,
|
||||
private val database: AppDatabase,
|
||||
private val dataStore: LauncherDataStore
|
||||
) : SearchableRepository, KoinComponent {
|
||||
) : SavableSearchableRepository, KoinComponent {
|
||||
|
||||
private val scope = CoroutineScope(Job() + Dispatchers.Default)
|
||||
|
||||
@@ -348,10 +352,17 @@ internal class SearchableRepositoryImpl(
|
||||
return database.searchableDao().sortByWeight(keys)
|
||||
}
|
||||
|
||||
private fun fromDatabaseEntity(entity: SavedSearchableEntity): SavedSearchable {
|
||||
val deserializer: SearchableDeserializer =
|
||||
getDeserializer(context, entity.type)
|
||||
val searchable = deserializer.deserialize(entity.serializedSearchable)
|
||||
private suspend fun fromDatabaseEntity(entity: SavedSearchableEntity): SavedSearchable {
|
||||
val deserializer: SearchableDeserializer? = try {
|
||||
get(named(entity.type))
|
||||
} catch (e: NoBeanDefFoundException) {
|
||||
CrashReporter.logException(e)
|
||||
null
|
||||
} catch (e: InstanceCreationException) {
|
||||
CrashReporter.logException(e)
|
||||
null
|
||||
}
|
||||
val searchable = deserializer?.deserialize(entity.serializedSearchable)
|
||||
if (searchable == null) removeInvalidItem(entity.key)
|
||||
return SavedSearchable(
|
||||
key = entity.key,
|
||||
@@ -15,9 +15,7 @@ data class SavedSearchable(
|
||||
var weight: Double
|
||||
) {
|
||||
fun toDatabaseEntity(): SavedSearchableEntity? {
|
||||
val serializer = getSerializer(searchable)
|
||||
|
||||
val data = searchable?.let { serializer.serialize(it) } ?: return null
|
||||
val data = searchable?.serialize() ?: return null
|
||||
|
||||
return SavedSearchableEntity(
|
||||
key = key,
|
||||
|
||||
@@ -1,114 +1,8 @@
|
||||
package de.mm20.launcher2.searchable
|
||||
|
||||
import android.content.Context
|
||||
import de.mm20.launcher2.appshortcuts.LauncherShortcutDeserializer
|
||||
import de.mm20.launcher2.appshortcuts.LauncherShortcutSerializer
|
||||
import de.mm20.launcher2.appshortcuts.LegacyShortcutDeserializer
|
||||
import de.mm20.launcher2.appshortcuts.LegacyShortcutSerializer
|
||||
import de.mm20.launcher2.calendar.CalendarEventDeserializer
|
||||
import de.mm20.launcher2.calendar.CalendarEventSerializer
|
||||
import de.mm20.launcher2.contacts.ContactDeserializer
|
||||
import de.mm20.launcher2.contacts.ContactSerializer
|
||||
import de.mm20.launcher2.files.*
|
||||
import de.mm20.launcher2.search.NullDeserializer
|
||||
import de.mm20.launcher2.search.NullSerializer
|
||||
import de.mm20.launcher2.search.SavableSearchable
|
||||
import de.mm20.launcher2.search.Searchable
|
||||
import de.mm20.launcher2.search.SearchableDeserializer
|
||||
import de.mm20.launcher2.search.SearchableSerializer
|
||||
import de.mm20.launcher2.search.data.*
|
||||
import de.mm20.launcher2.websites.WebsiteDeserializer
|
||||
import de.mm20.launcher2.websites.WebsiteSerializer
|
||||
import de.mm20.launcher2.wikipedia.WikipediaDeserializer
|
||||
import de.mm20.launcher2.wikipedia.WikipediaSerializer
|
||||
|
||||
internal fun SavableSearchable.serialize(): String? {
|
||||
val serializer = getSerializer(this)
|
||||
val serializer = getSerializer()
|
||||
return serializer.serialize(this)
|
||||
}
|
||||
|
||||
internal fun getSerializer(searchable: Searchable?): SearchableSerializer {
|
||||
if (searchable is LauncherApp) {
|
||||
return LauncherAppSerializer()
|
||||
}
|
||||
if (searchable is LauncherShortcut) {
|
||||
return LauncherShortcutSerializer()
|
||||
}
|
||||
if (searchable is LegacyShortcut) {
|
||||
return LegacyShortcutSerializer()
|
||||
}
|
||||
if (searchable is CalendarEvent) {
|
||||
return CalendarEventSerializer()
|
||||
}
|
||||
if (searchable is Contact) {
|
||||
return ContactSerializer()
|
||||
}
|
||||
if (searchable is Wikipedia) {
|
||||
return WikipediaSerializer()
|
||||
}
|
||||
if (searchable is GDriveFile) {
|
||||
return GDriveFileSerializer()
|
||||
}
|
||||
if (searchable is OneDriveFile) {
|
||||
return OneDriveFileSerializer()
|
||||
}
|
||||
if (searchable is OwncloudFile) {
|
||||
return OwncloudFileSerializer()
|
||||
}
|
||||
if (searchable is NextcloudFile) {
|
||||
return NextcloudFileSerializer()
|
||||
}
|
||||
if (searchable is LocalFile) {
|
||||
return LocalFileSerializer()
|
||||
}
|
||||
if (searchable is Website) {
|
||||
return WebsiteSerializer()
|
||||
}
|
||||
if (searchable is Tag) {
|
||||
return TagSerializer()
|
||||
}
|
||||
return NullSerializer()
|
||||
}
|
||||
|
||||
internal fun getDeserializer(context: Context, type: String): SearchableDeserializer {
|
||||
if (type == LauncherApp.Domain) {
|
||||
return LauncherAppDeserializer(context)
|
||||
}
|
||||
if (type == LauncherShortcut.Domain) {
|
||||
return LauncherShortcutDeserializer(context)
|
||||
}
|
||||
if (type == LegacyShortcut.Domain) {
|
||||
return LegacyShortcutDeserializer(context)
|
||||
}
|
||||
if (type == CalendarEvent.Domain) {
|
||||
return CalendarEventDeserializer(context)
|
||||
}
|
||||
if (type == Contact.Domain) {
|
||||
return ContactDeserializer(context)
|
||||
}
|
||||
if (type == Wikipedia.Domain) {
|
||||
return WikipediaDeserializer(context)
|
||||
}
|
||||
if (type == GDriveFile.Domain) {
|
||||
return GDriveFileDeserializer()
|
||||
}
|
||||
if (type == OneDriveFile.Domain) {
|
||||
return OneDriveFileDeserializer()
|
||||
}
|
||||
if (type == NextcloudFile.Domain) {
|
||||
return NextcloudFileDeserializer()
|
||||
}
|
||||
if (type == OwncloudFile.Domain) {
|
||||
return OwncloudFileDeserializer()
|
||||
}
|
||||
if (type == LocalFile.Domain) {
|
||||
return LocalFileDeserializer(context)
|
||||
}
|
||||
if (type == Website.Domain) {
|
||||
return WebsiteDeserializer()
|
||||
}
|
||||
if (type == Tag.Domain) {
|
||||
return TagDeserializer()
|
||||
}
|
||||
return NullDeserializer()
|
||||
}
|
||||
@@ -19,7 +19,7 @@ class TagSerializer: SearchableSerializer {
|
||||
}
|
||||
|
||||
class TagDeserializer: SearchableDeserializer {
|
||||
override fun deserialize(serialized: String): SavableSearchable {
|
||||
override suspend fun deserialize(serialized: String): SavableSearchable {
|
||||
val json = JSONObject(serialized)
|
||||
|
||||
return Tag(json.getString("tag"))
|
||||
|
||||
Reference in New Issue
Block a user