Skip to content

Commit

Permalink
[#285] [NF] Screen Widgets. Mock dynamic data
Browse files Browse the repository at this point in the history
  • Loading branch information
lyskouski committed Dec 4, 2024
1 parent b552f14 commit be6cb54
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
android:label="Fingrom"
android:roundIcon="@mipmap/ic_launcher_round">
<receiver
android:name=".PaymentsAppWidget"
android:name=".PaymentsWidgetProvider"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ import android.widget.RemoteViewsService
import androidx.core.graphics.ColorUtils
import android.graphics.Color

import es.antonborri.home_widget.HomeWidgetPlugin

class PaymentsWidgetFactory(private val context: Context, intent: Intent) : RemoteViewsService.RemoteViewsFactory {

private val items = mutableListOf<PaymentItem>()

override fun onCreate() {
items.add(PaymentItem("Bill: first", "-$100.00", "monthly"))
items.clear()
val widgetData = HomeWidgetPlugin.getData(context)
val store = widgetData.getString("payments_widget_data", null)
items.add(PaymentItem(store ?: "Not found", "-$100.00", "monthly"))
items.add(PaymentItem("Invoice:", "+$50.00", "weekly"))
items.add(PaymentItem("Bill: second", "-$20.00", "monthly"))
items.add(PaymentItem("Bill: second", "-$20.00", "monthly"))
}

override fun onDataSetChanged() {
// Called when the widget data is updated.
onCreate()
}

override fun getCount(): Int = items.size
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.tercad.fingrom

import android.app.AlarmManager
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.SystemClock
import android.widget.RemoteViews
import android.widget.AdapterView

/**
* Implementation of App Widget functionality.
*/
class PaymentsAppWidget : AppWidgetProvider() {
class PaymentsWidgetProvider : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
android:resizeMode="horizontal|vertical"
android:targetCellWidth="3"
android:targetCellHeight="2"
android:updatePeriodMillis="360000"
android:updatePeriodMillis="10000"
android:widgetCategory="home_screen" />
2 changes: 1 addition & 1 deletion android/app/src/main/res/xml/payments_app_widget_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
android:minHeight="110dp"
android:previewImage="@drawable/payments_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="360000"
android:updatePeriodMillis="10000"
android:widgetCategory="home_screen" />
23 changes: 23 additions & 0 deletions lib/_classes/controller/payments_controller.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Copyright 2024 The terCAD team. All rights reserved.
// Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.

import 'dart:io';
import 'dart:math';

import 'package:app_finance/_classes/storage/app_data.dart';
import 'package:app_finance/_classes/structure/payment_app_data.dart';
import 'package:app_finance/_configs/budget_type.dart';
import 'package:app_finance/_ext/data_ext.dart';
import 'package:app_finance/_ext/date_time_ext.dart';
import 'package:home_widget/home_widget.dart';

class PaymentsController {
final AppData state;
Expand Down Expand Up @@ -35,5 +39,24 @@ class PaymentsController {
state.update(item.uuid!, item);
}
}
// TODO: create iOS Widget
if (Platform.isAndroid) {
// updateWidget();
}
}

Future<void> updateWidget() async {
var data = 'TEST_DATA ${Random().nextInt(100)}';
print(['updateWidget', data]);
await HomeWidget.saveWidgetData<String>('payments_widget_data', data);
await HomeWidget.updateWidget(
name: 'PaymentsWidgetProvider',
// iOSName: 'PaymentsWidget',
);
if (Platform.isAndroid) {
await HomeWidget.updateWidget(
qualifiedAndroidName: 'com.tercad.fingrom.PaymentsWidgetService',
);
}
}
}

0 comments on commit be6cb54

Please sign in to comment.