Skip to content

Commit

Permalink
Create a WidgetConfigureActivity base class (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods authored May 29, 2022
1 parent b7428d4 commit 7b87209
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 287 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.homeassistant.companion.android.widgets

import android.appwidget.AppWidgetManager
import android.content.Context
import android.view.View
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import io.homeassistant.companion.android.BaseActivity
import io.homeassistant.companion.android.common.R
import io.homeassistant.companion.android.database.widget.WidgetDao
import kotlinx.coroutines.launch

abstract class BaseWidgetConfigureActivity : BaseActivity() {

protected var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID

abstract val dao: WidgetDao

protected val onDeleteWidget = View.OnClickListener { deleteConfirmation(it.context) }

private fun deleteConfirmation(context: Context) {
val builder: android.app.AlertDialog.Builder = android.app.AlertDialog.Builder(context)

builder.setTitle(R.string.confirm_delete_this_widget_title)
builder.setMessage(R.string.confirm_delete_this_widget_message)

builder.setPositiveButton(
R.string.confirm_positive
) { dialog, _ ->
lifecycleScope.launch {
dao.delete(appWidgetId)
dialog.dismiss()
finish()
}
}

builder.setNegativeButton(
R.string.confirm_negative
) { dialog, _ -> // Do nothing
dialog.dismiss()
}

val alert: android.app.AlertDialog? = builder.create()
alert?.show()
}

protected fun showAddWidgetError() {
Toast.makeText(applicationContext, R.string.widget_creation_error, Toast.LENGTH_LONG).show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.homeassistant.companion.android.widgets.button
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
Expand All @@ -17,13 +16,13 @@ import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.toColorInt
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
Expand All @@ -35,28 +34,25 @@ import com.maltaisn.icondialog.pack.IconPack
import com.maltaisn.icondialog.pack.IconPackLoader
import com.maltaisn.iconpack.mdi.createMaterialDesignIconPack
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.BaseActivity
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import io.homeassistant.companion.android.common.data.integration.Service
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.widget.ButtonWidgetDao
import io.homeassistant.companion.android.database.widget.WidgetBackgroundType
import io.homeassistant.companion.android.databinding.WidgetButtonConfigureBinding
import io.homeassistant.companion.android.settings.widgets.ManageWidgetsViewModel
import io.homeassistant.companion.android.util.getHexForColor
import io.homeassistant.companion.android.widgets.BaseWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.common.ServiceFieldBinder
import io.homeassistant.companion.android.widgets.common.SingleItemArrayAdapter
import io.homeassistant.companion.android.widgets.common.WidgetDynamicFieldAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import javax.inject.Inject
import io.homeassistant.companion.android.common.R as commonR

@AndroidEntryPoint
class ButtonWidgetConfigureActivity : BaseActivity(), IconDialog.Callback {
class ButtonWidgetConfigureActivity : BaseWidgetConfigureActivity(), IconDialog.Callback {
companion object {
private const val TAG: String = "ButtonWidgetConfigAct"
private const val ICON_DIALOG_TAG = "icon-dialog"
Expand All @@ -66,6 +62,9 @@ class ButtonWidgetConfigureActivity : BaseActivity(), IconDialog.Callback {
@Inject
lateinit var integrationUseCase: IntegrationRepository

private lateinit var buttonWidgetDao: ButtonWidgetDao
override val dao get() = buttonWidgetDao

private lateinit var iconPack: IconPack

private var services = HashMap<String, Service>()
Expand All @@ -75,16 +74,8 @@ class ButtonWidgetConfigureActivity : BaseActivity(), IconDialog.Callback {

private lateinit var binding: WidgetButtonConfigureBinding

private val mainScope: CoroutineScope = CoroutineScope(Dispatchers.Main + Job())

private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
private var requestLauncherSetup = false

private var onDeleteWidget = View.OnClickListener {
val context = this@ButtonWidgetConfigureActivity
deleteConfirmation(context)
}

private val onAddFieldListener = View.OnClickListener {
val context = this@ButtonWidgetConfigureActivity
val fieldKeyInput = EditText(context)
Expand Down Expand Up @@ -199,7 +190,7 @@ class ButtonWidgetConfigureActivity : BaseActivity(), IconDialog.Callback {
return
}

val buttonWidgetDao = AppDatabase.getInstance(applicationContext).buttonWidgetDao()
buttonWidgetDao = AppDatabase.getInstance(applicationContext).buttonWidgetDao()
val buttonWidget = buttonWidgetDao.get(appWidgetId)
var serviceText = ""

Expand Down Expand Up @@ -247,7 +238,7 @@ class ButtonWidgetConfigureActivity : BaseActivity(), IconDialog.Callback {
binding.widgetTextConfigService.setAdapter(serviceAdapter)
binding.widgetTextConfigService.onFocusChangeListener = dropDownOnFocus

mainScope.launch {
lifecycleScope.launch {
try {
// Fetch services
integrationUseCase.getServices()?.forEach {
Expand Down Expand Up @@ -373,11 +364,6 @@ class ButtonWidgetConfigureActivity : BaseActivity(), IconDialog.Callback {
}
}

override fun onDestroy() {
mainScope.cancel()
super.onDestroy()
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent != null && intent.extras != null && intent.hasExtra(PIN_WIDGET_CALLBACK)) {
Expand Down Expand Up @@ -493,34 +479,4 @@ class ButtonWidgetConfigureActivity : BaseActivity(), IconDialog.Callback {
showAddWidgetError()
}
}

private fun showAddWidgetError() {
Toast.makeText(applicationContext, commonR.string.widget_creation_error, Toast.LENGTH_LONG).show()
}

private fun deleteConfirmation(context: Context) {
val buttonWidgetDao = AppDatabase.getInstance(context).buttonWidgetDao()

val builder: android.app.AlertDialog.Builder = android.app.AlertDialog.Builder(context)

builder.setTitle(commonR.string.confirm_delete_this_widget_title)
builder.setMessage(commonR.string.confirm_delete_this_widget_message)

builder.setPositiveButton(
commonR.string.confirm_positive
) { dialog, _ ->
buttonWidgetDao.delete(appWidgetId)
dialog.dismiss()
finish()
}

builder.setNegativeButton(
commonR.string.confirm_negative
) { dialog, _ -> // Do nothing
dialog.dismiss()
}

val alert: android.app.AlertDialog? = builder.create()
alert?.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.homeassistant.companion.android.widgets.camera
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
Expand All @@ -13,42 +12,41 @@ import android.widget.AdapterView
import android.widget.AutoCompleteTextView
import android.widget.Toast
import androidx.core.content.getSystemService
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.BaseActivity
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import io.homeassistant.companion.android.common.data.integration.domain
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.widget.CameraWidgetDao
import io.homeassistant.companion.android.databinding.WidgetCameraConfigureBinding
import io.homeassistant.companion.android.settings.widgets.ManageWidgetsViewModel
import io.homeassistant.companion.android.widgets.BaseWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.common.SingleItemArrayAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import javax.inject.Inject
import io.homeassistant.companion.android.common.R as commonR

@AndroidEntryPoint
class CameraWidgetConfigureActivity : BaseActivity() {
class CameraWidgetConfigureActivity : BaseWidgetConfigureActivity() {

companion object {
private const val TAG: String = "CameraWidgetConfigAct"
private const val PIN_WIDGET_CALLBACK = "io.homeassistant.companion.android.widgets.camera.CameraWidgetConfigureActivity.PIN_WIDGET_CALLBACK"
}

private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
private var requestLauncherSetup = false

@Inject
lateinit var integrationUseCase: IntegrationRepository
private val mainScope: CoroutineScope = CoroutineScope(Dispatchers.Main + Job())

private var entities = LinkedHashMap<String, Entity<Any>>()
private var selectedEntity: Entity<Any>? = null

private lateinit var cameraWidgetDao: CameraWidgetDao
override val dao get() = cameraWidgetDao

public override fun onCreate(icicle: Bundle?) {
super.onCreate(icicle)

Expand Down Expand Up @@ -96,7 +94,7 @@ class CameraWidgetConfigureActivity : BaseActivity() {
return
}

val cameraWidgetDao = AppDatabase.getInstance(this).cameraWidgetDao()
cameraWidgetDao = AppDatabase.getInstance(applicationContext).cameraWidgetDao()
val cameraWidget = cameraWidgetDao.get(appWidgetId)
if (cameraWidget != null) {
binding.widgetTextConfigEntityId.setText(cameraWidget.entityId)
Expand Down Expand Up @@ -124,7 +122,7 @@ class CameraWidgetConfigureActivity : BaseActivity() {
binding.widgetTextConfigEntityId.onFocusChangeListener = dropDownOnFocus
binding.widgetTextConfigEntityId.onItemClickListener = entityDropDownOnItemClick

mainScope.launch {
lifecycleScope.launch {
try {
// Fetch entities
val fetchedEntities = integrationUseCase.getEntities()
Expand Down Expand Up @@ -192,15 +190,6 @@ class CameraWidgetConfigureActivity : BaseActivity() {
}
}

private fun showAddWidgetError() {
Toast.makeText(applicationContext, commonR.string.widget_creation_error, Toast.LENGTH_LONG).show()
}

override fun onDestroy() {
mainScope.cancel()
super.onDestroy()
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent != null && intent.extras != null && intent.hasExtra(PIN_WIDGET_CALLBACK)) {
Expand All @@ -210,35 +199,4 @@ class CameraWidgetConfigureActivity : BaseActivity() {
onAddWidget()
}
}

private var onDeleteWidget = View.OnClickListener {
val context = this@CameraWidgetConfigureActivity
deleteConfirmation(context)
}

private fun deleteConfirmation(context: Context) {
val cameraWidgetDao = AppDatabase.getInstance(context).cameraWidgetDao()

val builder: android.app.AlertDialog.Builder = android.app.AlertDialog.Builder(context)

builder.setTitle(commonR.string.confirm_delete_this_widget_title)
builder.setMessage(commonR.string.confirm_delete_this_widget_message)

builder.setPositiveButton(
commonR.string.confirm_positive
) { dialog, _ ->
cameraWidgetDao.delete(appWidgetId)
dialog.dismiss()
finish()
}

builder.setNegativeButton(
commonR.string.confirm_negative
) { dialog, _ -> // Do nothing
dialog.dismiss()
}

val alert: android.app.AlertDialog? = builder.create()
alert?.show()
}
}
Loading

0 comments on commit 7b87209

Please sign in to comment.