Use dependency injection for most singletons
This commit is contained in:
@@ -49,6 +49,8 @@ dependencies {
|
||||
implementation(libs.textdrawable)
|
||||
implementation(libs.jsoup)
|
||||
|
||||
implementation(libs.koin.android)
|
||||
|
||||
implementation(project(":preferences"))
|
||||
implementation(project(":search"))
|
||||
implementation(project(":base"))
|
||||
|
||||
@@ -39,17 +39,6 @@ class Website(
|
||||
val color: Int
|
||||
) : Searchable() {
|
||||
|
||||
override fun serialize(): String {
|
||||
return jsonObjectOf(
|
||||
"label" to label,
|
||||
"url" to url,
|
||||
"description" to description,
|
||||
"image" to image,
|
||||
"favicon" to favicon,
|
||||
"color" to color
|
||||
).toString()
|
||||
}
|
||||
|
||||
override val key = "web://$url"
|
||||
override suspend fun loadIconAsync(context: Context, size: Int): LauncherIcon? {
|
||||
if (favicon.isEmpty()) return null
|
||||
@@ -159,17 +148,5 @@ class Website(
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
fun deserialize(serialized: String): Website {
|
||||
val json = JSONObject(serialized)
|
||||
return Website(
|
||||
label = json.getString("label"),
|
||||
favicon = json.getString("favicon"),
|
||||
image = json.getString("image"),
|
||||
description = json.getString("description"),
|
||||
url = json.getString("url"),
|
||||
color = json.getInt("color")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package de.mm20.launcher2.websites
|
||||
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.androidx.viewmodel.dsl.viewModel
|
||||
import org.koin.dsl.module
|
||||
|
||||
val websitesModule = module {
|
||||
single { WebsiteRepository(androidContext()) }
|
||||
viewModel {
|
||||
WebsiteViewModel(get())
|
||||
}
|
||||
}
|
||||
@@ -9,16 +9,16 @@ import kotlinx.coroutines.withContext
|
||||
import okhttp3.OkHttpClient
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class WebsiteRepository private constructor(val context: Context): BaseSearchableRepository() {
|
||||
class WebsiteRepository(val context: Context) : BaseSearchableRepository() {
|
||||
|
||||
val website = MutableLiveData<Website?>()
|
||||
|
||||
private val httpClient = OkHttpClient
|
||||
.Builder()
|
||||
.connectTimeout(200, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(3000, TimeUnit.MILLISECONDS)
|
||||
.writeTimeout(1000, TimeUnit.MILLISECONDS)
|
||||
.build()
|
||||
.Builder()
|
||||
.connectTimeout(200, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(3000, TimeUnit.MILLISECONDS)
|
||||
.writeTimeout(1000, TimeUnit.MILLISECONDS)
|
||||
.build()
|
||||
|
||||
override fun onCancel() {
|
||||
super.onCancel()
|
||||
@@ -40,13 +40,4 @@ class WebsiteRepository private constructor(val context: Context): BaseSearchabl
|
||||
}
|
||||
website.value = wiki
|
||||
}
|
||||
|
||||
companion object {
|
||||
private lateinit var instance: WebsiteRepository
|
||||
|
||||
fun getInstance(context: Context): WebsiteRepository {
|
||||
if(!::instance.isInitialized) instance = WebsiteRepository(context.applicationContext)
|
||||
return instance
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package de.mm20.launcher2.websites
|
||||
|
||||
import de.mm20.launcher2.ktx.jsonObjectOf
|
||||
import de.mm20.launcher2.search.SearchableDeserializer
|
||||
import de.mm20.launcher2.search.SearchableSerializer
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import de.mm20.launcher2.search.data.Website
|
||||
import org.json.JSONObject
|
||||
|
||||
class WebsiteSerializer : SearchableSerializer {
|
||||
override fun serialize(searchable: Searchable): String {
|
||||
searchable as Website
|
||||
return jsonObjectOf(
|
||||
"label" to searchable.label,
|
||||
"url" to searchable.url,
|
||||
"description" to searchable.description,
|
||||
"image" to searchable.image,
|
||||
"favicon" to searchable.favicon,
|
||||
"color" to searchable.color
|
||||
).toString()
|
||||
}
|
||||
|
||||
override val typePrefix: String
|
||||
get() = "website"
|
||||
}
|
||||
|
||||
class WebsiteDeserializer: SearchableDeserializer {
|
||||
override fun deserialize(serialized: String): Searchable? {
|
||||
val json = JSONObject(serialized)
|
||||
return Website(
|
||||
label = json.getString("label"),
|
||||
favicon = json.getString("favicon"),
|
||||
image = json.getString("image"),
|
||||
description = json.getString("description"),
|
||||
url = json.getString("url"),
|
||||
color = json.getInt("color")
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
package de.mm20.launcher2.websites
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import de.mm20.launcher2.search.data.Website
|
||||
|
||||
class WebsiteViewModel(val app: Application): AndroidViewModel(app) {
|
||||
val website: LiveData<Website?> = WebsiteRepository.getInstance(app).website
|
||||
class WebsiteViewModel(
|
||||
websiteRepository: WebsiteRepository
|
||||
): ViewModel() {
|
||||
val website: LiveData<Website?> = websiteRepository.website
|
||||
}
|
||||
Reference in New Issue
Block a user