mirror of
https://github.com/hmalik144/easy_cc_flutter.git
synced 2025-12-10 03:05:34 +00:00
- Homescreen widget for android added
This commit is contained in:
@@ -47,7 +47,7 @@ android {
|
||||
applicationId "com.appttude.h_mal.easycc"
|
||||
// You can update the following values to match your application needs.
|
||||
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
|
||||
minSdkVersion flutter.minSdkVersion
|
||||
minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger()
|
||||
targetSdkVersion flutter.targetSdkVersion
|
||||
versionCode flutterVersionCode.toInteger()
|
||||
versionName flutterVersionName
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
package com.appttude.h_mal.easycc
|
||||
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.net.Uri
|
||||
import android.widget.RemoteViews
|
||||
import android.widget.Toast
|
||||
import es.antonborri.home_widget.HomeWidgetBackgroundIntent
|
||||
import es.antonborri.home_widget.HomeWidgetLaunchIntent
|
||||
import es.antonborri.home_widget.HomeWidgetPlugin
|
||||
import es.antonborri.home_widget.HomeWidgetProvider
|
||||
|
||||
|
||||
class AppWidgetProvider : HomeWidgetProvider(){
|
||||
override fun onUpdate(
|
||||
context: Context,
|
||||
@@ -19,16 +24,22 @@ class AppWidgetProvider : HomeWidgetProvider(){
|
||||
appWidgetIds.forEach { widgetId ->
|
||||
val views = RemoteViews(context.packageName, R.layout.currency_app_widget).apply {
|
||||
// Data from background operation received
|
||||
val from: String? = widgetData.getString("from", null)
|
||||
val to: String? = widgetData.getString("to", null)
|
||||
val rate: String? = widgetData.getString("rate", null)
|
||||
val from: String? = widgetData.getString("${widgetId}_from", null)
|
||||
val to: String? = widgetData.getString("${widgetId}_to", null)
|
||||
val rate: String? = widgetData.getString("${widgetId}_rate", null)
|
||||
|
||||
if (from.isNullOrBlank() or to.isNullOrBlank() or rate.isNullOrBlank()) {
|
||||
Toast.makeText(context, "Unable to review data for widget", Toast.LENGTH_SHORT).show()
|
||||
return@apply
|
||||
}
|
||||
|
||||
val titleString = "${from}${to}"
|
||||
setTextViewText(R.id.exchangeName, titleString)
|
||||
setTextViewText(R.id.exchangeRate, rate.toString())
|
||||
|
||||
val uri = Uri.parse("myAppWidget://update")
|
||||
uri.buildUpon().query(widgetId.toString()).build()
|
||||
val uri = Uri.parse("myAppWidget://updatewidget").buildUpon()
|
||||
.appendQueryParameter("id", widgetId.toString())
|
||||
.build()
|
||||
|
||||
setImageViewResource(R.id.refresh_icon, R.drawable.ic_refresh_white_24dp)
|
||||
val backgroundIntent = HomeWidgetBackgroundIntent.getBroadcast(context, uri)
|
||||
@@ -42,4 +53,36 @@ class AppWidgetProvider : HomeWidgetProvider(){
|
||||
appWidgetManager.updateAppWidget(widgetId, views)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
super.onReceive(context, intent)
|
||||
|
||||
when (intent?.action) {
|
||||
|
||||
}
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context);
|
||||
val appWidgetIds = appWidgetManager.getAppWidgetIds(
|
||||
context?.let { ComponentName(it, this::class.java) })
|
||||
val widgetDate = context?.let { HomeWidgetPlugin.getData(it) }
|
||||
|
||||
appWidgetIds.forEach { widgetId ->
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDeleted(context: Context?, appWidgetIds: IntArray?) {
|
||||
super.onDeleted(context, appWidgetIds)
|
||||
|
||||
appWidgetIds?.forEach { widgetId ->
|
||||
val uri = Uri.parse("myAppWidget://deletewidget")
|
||||
uri.buildUpon().appendQueryParameter("id", widgetId.toString()).build()
|
||||
|
||||
context?.let {
|
||||
HomeWidgetBackgroundIntent
|
||||
.getBroadcast(it, uri)
|
||||
.send()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,13 @@
|
||||
package com.appttude.h_mal.easycc
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.TextView
|
||||
import androidx.activity.viewModels
|
||||
import com.appttude.h_mal.easycc.databinding.CurrencyAppWidgetConfigureBinding
|
||||
import com.appttude.h_mal.easycc.ui.BaseActivity
|
||||
import com.appttude.h_mal.easycc.ui.main.CustomDialogClass
|
||||
import com.appttude.h_mal.easycc.utils.transformIntToArray
|
||||
import com.appttude.h_mal.easycc.widget.CurrencyAppWidgetKotlin
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import android.widget.Toast
|
||||
import es.antonborri.home_widget.HomeWidgetBackgroundIntent
|
||||
import es.antonborri.home_widget.HomeWidgetBackgroundReceiver
|
||||
import es.antonborri.home_widget.HomeWidgetPlugin
|
||||
|
||||
/**
|
||||
* The configuration screen for the [CurrencyAppWidgetKotlin] AppWidget.
|
||||
@@ -28,6 +17,9 @@ class CurrencyAppWidgetConfigureActivity : Activity(),
|
||||
|
||||
private var mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
|
||||
private var top: String? = null
|
||||
private var bottom: String? = null
|
||||
|
||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.currency_app_widget_configure)
|
||||
@@ -61,18 +53,23 @@ class CurrencyAppWidgetConfigureActivity : Activity(),
|
||||
override fun onClick(view: View?) {
|
||||
when (view?.tag.toString()) {
|
||||
"top", "bottom" -> showCustomDialog(view)
|
||||
"submit" -> viewModel.submitSelectionOnClick()
|
||||
else -> {
|
||||
return
|
||||
}
|
||||
"submit" -> displaySubmitDialog()
|
||||
}
|
||||
}
|
||||
|
||||
private fun displaySubmitDialog() {
|
||||
val message = viewModel.getSubmitDialogMessage()
|
||||
WidgetSubmitDialog(this, message, object : DialogSubmit {
|
||||
if (top == null || bottom == null) {
|
||||
Toast.makeText(this, "Selections incomplete", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
if (top == bottom) {
|
||||
Toast.makeText(this, "Selected rates cannot be the same", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
WidgetSubmitDialog(this, getSubmitDialogMessage(), object : DialogSubmit {
|
||||
override fun onSubmit() {
|
||||
sendUpdateIntent()
|
||||
sendUpdateIntent(top!!, bottom!!)
|
||||
finishCurrencyWidgetActivity()
|
||||
}
|
||||
}).show()
|
||||
@@ -82,7 +79,10 @@ class CurrencyAppWidgetConfigureActivity : Activity(),
|
||||
private fun showCustomDialog(view: View?) {
|
||||
CustomDialogClass(this) {
|
||||
(view as TextView).text = it
|
||||
viewModel.setCurrencyName(view.tag, it)
|
||||
when (view.tag.toString()) {
|
||||
"top" -> top = it
|
||||
"bottom" -> bottom = it
|
||||
}
|
||||
}.show()
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ class CurrencyAppWidgetConfigureActivity : Activity(),
|
||||
fun sendUpdateIntent(from: String, to: String) {
|
||||
// It is the responsibility of the configuration activity to update the app widget
|
||||
// Send update broadcast to widget app class
|
||||
val uri = Uri.parse("myAppWidget://createWidget").buildUpon()
|
||||
val uri = Uri.parse("myAppWidget://createwidget").buildUpon()
|
||||
.appendQueryParameter("id", mAppWidgetId.toString())
|
||||
.appendQueryParameter("from", from)
|
||||
.appendQueryParameter("to", to)
|
||||
@@ -107,4 +107,13 @@ class CurrencyAppWidgetConfigureActivity : Activity(),
|
||||
backgroundIntent.send()
|
||||
}
|
||||
|
||||
private fun getSubmitDialogMessage(): String {
|
||||
val widgetName = getWidgetStringName()
|
||||
return StringBuilder().append("Create widget for ")
|
||||
.append(widgetName)
|
||||
.append("?").toString()
|
||||
}
|
||||
|
||||
private fun getWidgetStringName() = "${top!!.substring(0, 3)}${bottom!!.substring(0, 3)}"
|
||||
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.appttude.h_mal.easycc
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.appttude.h_mal.easycc.data.repository.Repository
|
||||
import com.appttude.h_mal.easycc.ui.BaseViewModel
|
||||
import com.appttude.h_mal.easycc.utils.trimToThree
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
|
||||
class WidgetViewModel: ViewModel() {
|
||||
|
||||
var appWidgetId: Int? = null
|
||||
|
||||
var rateIdFrom: String? = null
|
||||
var rateIdTo: String? = null
|
||||
|
||||
// Setup viewmodel app widget ID
|
||||
// Set default values for text views
|
||||
fun initiate(appId: Int) {
|
||||
appWidgetId = appId
|
||||
}
|
||||
|
||||
// Retrieve name for submit dialog (eg. AUDGBP)
|
||||
fun getSubmitDialogMessage(): String {
|
||||
val widgetName = getWidgetStringName()
|
||||
return StringBuilder().append("Create widget for ")
|
||||
.append(widgetName)
|
||||
.append("?").toString()
|
||||
}
|
||||
|
||||
fun submitSelectionOnClick() {
|
||||
if (rateIdTo == null || rateIdFrom == null) {
|
||||
onError("Selections incomplete")
|
||||
return
|
||||
}
|
||||
if (rateIdFrom == rateIdTo) {
|
||||
onError("Selected rates cannot be the same")
|
||||
return
|
||||
}
|
||||
onSuccess(Unit)
|
||||
}
|
||||
|
||||
fun setWidgetStored() {
|
||||
|
||||
}
|
||||
|
||||
// Start operation based on dialog selection
|
||||
fun setCurrencyName(tag: Any?, currencyName: String) {
|
||||
when (tag.toString()) {
|
||||
"top" -> rateIdFrom = currencyName
|
||||
"bottom" -> rateIdTo = currencyName
|
||||
}
|
||||
}
|
||||
|
||||
private fun getWidgetStringName() = "${rateIdFrom!!.substring(0, 3)}${rateIdTo!!.substring(0, 3)}"
|
||||
|
||||
}
|
||||
BIN
android/app/src/main/res/drawable/easyycc_widget_preview.png
Normal file
BIN
android/app/src/main/res/drawable/easyycc_widget_preview.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
11
android/app/src/main/res/xml/currency_app_widget_info.xml
Normal file
11
android/app/src/main/res/xml/currency_app_widget_info.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:configure="com.appttude.h_mal.easycc.CurrencyAppWidgetConfigureActivity"
|
||||
android:initialKeyguardLayout="@layout/currency_app_widget"
|
||||
android:initialLayout="@layout/currency_app_widget"
|
||||
android:minWidth="110dp"
|
||||
android:minHeight="40dp"
|
||||
android:previewImage="@drawable/easyycc_widget_preview"
|
||||
android:resizeMode="horizontal|vertical"
|
||||
android:updatePeriodMillis="3600000"
|
||||
android:widgetCategory="home_screen|keyguard" />
|
||||
Reference in New Issue
Block a user