mirror of
https://github.com/PranshulGG/WeatherMaster.git
synced 2026-10-21 18:43:03 -05:00
some background work
This commit is contained in:
1 parent
0e4fe25e49
commit
f50a905fa2
5 files changed
+73
-30
No files matched your search
@@ -55,6 +55,9 @@
|
||||
android:exported="true" />
|
||||
|
||||
|
||||
<receiver android:name=".WidgetUpdateReceiver" android:exported="true"/>
|
||||
|
||||
|
||||
|
||||
</application>
|
||||
<!-- Required to query activities that can process text, see:
|
||||
|
||||
@@ -8,42 +8,42 @@ import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
|
||||
class MainActivity: FlutterActivity() {
|
||||
class MainActivity : FlutterActivity() {
|
||||
private val CHANNEL = "weather.widget/channel"
|
||||
|
||||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
|
||||
call, result ->
|
||||
if (call.method == "updateCurrentWidget") {
|
||||
val temp = call.argument<Int>("temp") ?: 0
|
||||
val iconCode = call.argument<Int>("iconCode") ?: 0
|
||||
updateCurrentWeatherWidget(temp, iconCode)
|
||||
result.success(null)
|
||||
} else {
|
||||
result.notImplemented()
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
|
||||
.setMethodCallHandler { call, result ->
|
||||
if (call.method == "updateCurrentWidget") {
|
||||
val temp = call.argument<Int>("temp") ?: 0
|
||||
val iconCode = call.argument<Int>("iconCode") ?: 0
|
||||
updateCurrentWeatherWidget(temp, iconCode)
|
||||
result.success("Widget updated")
|
||||
} else {
|
||||
result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateCurrentWeatherWidget(temp: Int, iconCode: Int) {
|
||||
val views = RemoteViews(packageName, R.layout.widget_current_weather)
|
||||
views.setTextViewText(R.id.current_temp, "$temp°C")
|
||||
|
||||
val iconRes = when (iconCode) {
|
||||
1 -> R.drawable.sunny
|
||||
2 -> R.drawable.cloudy
|
||||
3 -> R.drawable.heavy_rain
|
||||
else -> R.drawable.sunny // fallback
|
||||
}
|
||||
|
||||
views.setImageViewResource(R.id.weather_icon, iconRes)
|
||||
|
||||
val manager = AppWidgetManager.getInstance(applicationContext)
|
||||
val componentName = ComponentName(applicationContext, CurrentWeatherWidget::class.java)
|
||||
manager.updateAppWidget(componentName, views)
|
||||
}
|
||||
|
||||
private fun updateCurrentWeatherWidget(temp: Int, iconCode: Int) {
|
||||
|
||||
val views = RemoteViews(packageName, R.layout.widget_current_weather)
|
||||
views.setTextViewText(R.id.current_temp, "$temp°C")
|
||||
|
||||
val iconRes = when (iconCode) {
|
||||
1 -> R.drawable.sunny
|
||||
2 -> R.drawable.cloudy
|
||||
3 -> R.drawable.heavy_rain
|
||||
else -> R.drawable.sunny
|
||||
}
|
||||
|
||||
views.setImageViewResource(R.id.weather_icon, iconRes)
|
||||
|
||||
val manager = AppWidgetManager.getInstance(applicationContext)
|
||||
val componentName = ComponentName(applicationContext, CurrentWeatherWidget::class.java)
|
||||
manager.updateAppWidget(componentName, views)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.pranshulgg.weather_master_app
|
||||
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.widget.RemoteViews
|
||||
|
||||
class WidgetUpdateReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val temp = intent.getIntExtra("temp", 0)
|
||||
val iconCode = intent.getIntExtra("iconCode", 1)
|
||||
|
||||
val views = RemoteViews(context.packageName, R.layout.widget_current_weather)
|
||||
views.setTextViewText(R.id.current_temp, "$temp°C")
|
||||
|
||||
val iconRes = when (iconCode) {
|
||||
1 -> R.drawable.sunny
|
||||
2 -> R.drawable.cloudy
|
||||
3 -> R.drawable.heavy_rain
|
||||
else -> R.drawable.sunny
|
||||
}
|
||||
|
||||
views.setImageViewResource(R.id.weather_icon, iconRes)
|
||||
|
||||
val manager = AppWidgetManager.getInstance(context)
|
||||
val componentName = ComponentName(context, CurrentWeatherWidget::class.java)
|
||||
manager.updateAppWidget(componentName, views)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
# Generated by pub
|
||||
# See https://dart.dev/tools/pub/glossary#lockfile
|
||||
packages:
|
||||
android_intent_plus:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: android_intent_plus
|
||||
sha256: dfc1fd3a577205ae8f11e990fb4ece8c90cceabbee56fcf48e463ecf0bd6aae3
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.3.0"
|
||||
animations:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
||||
@@ -64,6 +64,7 @@ dependencies:
|
||||
file_picker: ^10.2.0
|
||||
flutter_dotenv: ^5.2.1
|
||||
workmanager: ^0.9.0
|
||||
android_intent_plus: ^5.3.0
|
||||
|
||||
settings_tiles:
|
||||
path: ./settings_tiles_custom
|
||||
|
||||
Reference in new issue
Block a user