Initial commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="de.mm20.launcher2.widgets">
|
||||
|
||||
/
|
||||
</manifest>
|
||||
@@ -0,0 +1,32 @@
|
||||
package de.mm20.launcher2.widgets
|
||||
|
||||
import de.mm20.launcher2.database.entities.WidgetEntity
|
||||
|
||||
data class Widget(
|
||||
val type: WidgetType,
|
||||
var data: String,
|
||||
var height: Int,
|
||||
val label: String = ""
|
||||
) {
|
||||
constructor(entity: WidgetEntity) : this(
|
||||
type = if (entity.type == "internal") WidgetType.INTERNAL else WidgetType. THIRD_PARTY,
|
||||
data = entity.data,
|
||||
height = entity.height,
|
||||
label = entity.label
|
||||
)
|
||||
|
||||
fun toDatabaseEntity(position: Int): WidgetEntity {
|
||||
return WidgetEntity(
|
||||
type = if (type == WidgetType.INTERNAL) "internal" else "3rdparty",
|
||||
label = label,
|
||||
position = position,
|
||||
height = height,
|
||||
data = data
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
enum class WidgetType {
|
||||
INTERNAL,
|
||||
THIRD_PARTY
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package de.mm20.launcher2.widgets
|
||||
|
||||
import android.content.Context
|
||||
import de.mm20.launcher2.widgets.R
|
||||
import de.mm20.launcher2.database.AppDatabase
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
internal class WidgetRepository(
|
||||
val context: Context
|
||||
) {
|
||||
|
||||
suspend fun getWidgets(): List<Widget> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).widgetDao().getWidgets().map { Widget(it) }
|
||||
}
|
||||
}
|
||||
|
||||
fun getInternalWidgets(): List<Widget> {
|
||||
return listOf(
|
||||
Widget(WidgetType.INTERNAL, "weather", -1, context.getString(R.string.widget_name_weather))
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
suspend fun saveWidgets(widgets: List<Widget>) {
|
||||
withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).widgetDao().updateWidgets(widgets.mapIndexed { i, widget -> widget.toDatabaseEntity(i) })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package de.mm20.launcher2.widgets
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import de.mm20.launcher2.calendar.CalendarRepository
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class WidgetViewModel(app: Application) : AndroidViewModel(app) {
|
||||
|
||||
private val widgetRepository: WidgetRepository by lazy {
|
||||
WidgetRepository(app)
|
||||
}
|
||||
|
||||
suspend fun getWidgets(): List<Widget> {
|
||||
return withContext(viewModelScope.coroutineContext + Dispatchers.IO) {
|
||||
widgetRepository.getWidgets()
|
||||
}
|
||||
}
|
||||
|
||||
fun saveWidgets(widgets: List<Widget>) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
widgetRepository.saveWidgets(widgets)
|
||||
}
|
||||
}
|
||||
|
||||
fun getInternalWidgets(): List<Widget> {
|
||||
return widgetRepository.getInternalWidgets()
|
||||
}
|
||||
|
||||
private val calendarRepository: CalendarRepository by lazy {
|
||||
CalendarRepository.getInstance(app)
|
||||
}
|
||||
|
||||
fun requestCalendarUpdate() {
|
||||
calendarRepository.requestCalendarUpdate()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user