feat: první beta widget
This commit is contained in:
parent
7dd3985ab7
commit
843f24e418
|
@ -26,7 +26,27 @@
|
|||
<action android:name="android.intent.action.MAIN"/>
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="es.antonborri.home_widget.action.LAUNCH" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<receiver android:name="AppWidgetProvider" >
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||
</intent-filter>
|
||||
<meta-data android:name="android.appwidget.provider"
|
||||
android:resource="@xml/widget_info" />
|
||||
</receiver>
|
||||
|
||||
<receiver android:name="es.antonborri.home_widget.HomeWidgetBackgroundReceiver">
|
||||
<intent-filter>
|
||||
<action android:name="es.antonborri.home_widget.action.BACKGROUND" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<service android:name="es.antonborri.home_widget.HomeWidgetBackgroundService"
|
||||
android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>
|
||||
|
||||
<!-- Don't delete the meta-data below.
|
||||
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
|
||||
<meta-data
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package cz.hernikplays.opencanteen
|
||||
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.net.Uri
|
||||
import android.widget.RemoteViews
|
||||
import es.antonborri.home_widget.HomeWidgetBackgroundIntent
|
||||
import es.antonborri.home_widget.HomeWidgetLaunchIntent
|
||||
import es.antonborri.home_widget.HomeWidgetProvider
|
||||
|
||||
class AppWidgetProvider : HomeWidgetProvider() {
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray, widgetData: SharedPreferences) {
|
||||
appWidgetIds.forEach { widgetId ->
|
||||
val views = RemoteViews(context.packageName, R.layout.widget_layout).apply {
|
||||
|
||||
// Open App on Widget Click
|
||||
val pendingIntent = HomeWidgetLaunchIntent.getActivity(context,
|
||||
MainActivity::class.java)
|
||||
setOnClickPendingIntent(R.id.widget_root, pendingIntent)
|
||||
|
||||
setTextViewText(R.id.obed, widgetData.getString("_obed", "Nic"))
|
||||
|
||||
// Pending intent to update counter on button click
|
||||
val backgroundIntent = HomeWidgetBackgroundIntent.getBroadcast(context,
|
||||
Uri.parse("myAppWidget://update"))
|
||||
setOnClickPendingIntent(R.id.bt_update, backgroundIntent)
|
||||
}
|
||||
appWidgetManager.updateAppWidget(widgetId, views)
|
||||
}
|
||||
}
|
||||
}
|
32
android/app/src/main/res/layout/widget_layout.xml
Normal file
32
android/app/src/main/res/layout/widget_layout.xml
Normal file
|
@ -0,0 +1,32 @@
|
|||
<FrameLayout
|
||||
android:id="@+id/widget_root"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="280dp"
|
||||
android:layout_height="150dp">
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#1f303d">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/obed"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentTop="true"
|
||||
android:gravity="center_horizontal"
|
||||
android:padding="12dp"
|
||||
android:text="--"
|
||||
android:textColor="@android:color/white"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/bt_update"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:text="UPDATE"
|
||||
android:textColor="@android:color/holo_blue_dark"
|
||||
android:textSize="12sp" />
|
||||
</RelativeLayout>
|
||||
</FrameLayout>
|
8
android/app/src/main/res/xml/widget_info.xml
Normal file
8
android/app/src/main/res/xml/widget_info.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<appwidget-provider
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:initialLayout="@layout/widget_layout"
|
||||
android:minWidth="280dp"
|
||||
android:minHeight="150dp"
|
||||
android:minResizeWidth="280dp"
|
||||
android:minResizeHeight="110dp"
|
||||
android:widgetCategory="home_screen" />
|
|
@ -4,6 +4,7 @@ import 'dart:io';
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:home_widget/home_widget.dart';
|
||||
import 'package:opencanteen/lang/lang_cz.dart';
|
||||
import 'package:opencanteen/loginmanager.dart';
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
|
@ -35,9 +36,32 @@ Copyright (C) 2022 Matyáš Caras a přispěvatelé
|
|||
*/
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
HomeWidget.registerBackgroundCallback(backgroundCallback);
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
/// Volá se na práci s widgetem
|
||||
void backgroundCallback(Uri? uri) async {
|
||||
if (uri != null && uri.host == 'update') {
|
||||
String _obed = "Nic";
|
||||
var d = await LoginManager.getDetails();
|
||||
if (d != null) {
|
||||
var c = Canteen(d["url"]!);
|
||||
await c.login(d["user"]!, d["pass"]!);
|
||||
var j = await c.jidelnicekDen();
|
||||
try {
|
||||
_obed = j.jidla.singleWhere((element) => element.objednano).nazev;
|
||||
} catch (_) {
|
||||
_obed = "Nic";
|
||||
}
|
||||
}
|
||||
await HomeWidget.saveWidgetData<String>('_obed', _obed);
|
||||
await HomeWidget.updateWidget(
|
||||
name: 'AppWidgetProvider', iOSName: 'AppWidgetProvider');
|
||||
}
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
|
|
|
@ -205,6 +205,13 @@ packages:
|
|||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
home_widget:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: home_widget
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.1.6"
|
||||
http:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
|
@ -6,7 +6,7 @@ publish_to: 'none'
|
|||
# The following defines the version and build number for your application.
|
||||
# A version number is three numbers separated by dots, like 1.2.43
|
||||
# followed by an optional build number separated by a +.
|
||||
version: 1.1.1+5
|
||||
version: 1.2.0+8
|
||||
|
||||
environment:
|
||||
sdk: ">=2.16.1 <3.0.0"
|
||||
|
@ -23,6 +23,7 @@ dependencies:
|
|||
path_provider: ^2.0.9
|
||||
shared_preferences: ^2.0.13
|
||||
introduction_screen: ^3.0.1
|
||||
home_widget: ^0.1.6
|
||||
|
||||
dev_dependencies:
|
||||
flutter_lints: ^1.0.0
|
||||
|
|
Loading…
Reference in a new issue