Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Add home widget feature displaying the first four missing anime
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Aug 8, 2023
1 parent e3cb56b commit 54e26fd
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 4 deletions.
8 changes: 8 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
</intent-filter>
</activity>

<receiver android:name="HomeWidgetProvider" android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/home_widget_provider" />
</receiver>

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-5658764393995798~7443712880"/>
Expand Down
66 changes: 66 additions & 0 deletions android/app/src/main/kotlin/fr/ziedelth/jais/HomeWidgetProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fr.ziedelth.jais

import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.SharedPreferences
import android.graphics.BitmapFactory
import android.net.Uri
import android.view.View
import android.widget.RemoteViews

import es.antonborri.home_widget.HomeWidgetBackgroundIntent
import es.antonborri.home_widget.HomeWidgetLaunchIntent
import es.antonborri.home_widget.HomeWidgetProvider

class HomeWidgetProvider : HomeWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray, widgetData: SharedPreferences) {
appWidgetIds.forEach { widgetId ->
val views = RemoteViews(context.packageName, R.layout.home_widget_layout).apply {
// Open App on Widget Click
val pendingIntent = HomeWidgetLaunchIntent.getActivity(
context,
MainActivity::class.java)
setOnClickPendingIntent(R.id.home_widget_container, pendingIntent)

// Show Images saved with `renderFlutterWidget`
val image1 = widgetData.getString("image0", null)

if (image1 != null) {
setImageViewBitmap(R.id.widget_img1, BitmapFactory.decodeFile(image1))
setViewVisibility(R.id.widget_img1, View.VISIBLE)
} else {
setViewVisibility(R.id.widget_img1, View.GONE)
}

val image2 = widgetData.getString("image1", null)

if (image2 != null) {
setImageViewBitmap(R.id.widget_img2, BitmapFactory.decodeFile(image2))
setViewVisibility(R.id.widget_img2, View.VISIBLE)
} else {
setViewVisibility(R.id.widget_img2, View.GONE)
}

val image3 = widgetData.getString("image2", null)

if (image3 != null) {
setImageViewBitmap(R.id.widget_img3, BitmapFactory.decodeFile(image3))
setViewVisibility(R.id.widget_img3, View.VISIBLE)
} else {
setViewVisibility(R.id.widget_img3, View.GONE)
}

val image4 = widgetData.getString("image3", null)

if (image4 != null) {
setImageViewBitmap(R.id.widget_img4, BitmapFactory.decodeFile(image4))
setViewVisibility(R.id.widget_img4, View.VISIBLE)
} else {
setViewVisibility(R.id.widget_img4, View.GONE)
}
}

appWidgetManager.updateAppWidget(widgetId, views)
}
}
}
5 changes: 5 additions & 0 deletions android/app/src/main/res/drawable/home_widget_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?android:attr/colorBackground"/>
<corners android:radius="16dp"/>
</shape>
40 changes: 40 additions & 0 deletions android/app/src/main/res/layout/home_widget_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="horizontal"
android:background="@drawable/home_widget_background"
android:theme="@android:style/Theme.DeviceDefault.DayNight"
android:padding="8dp"
android:id="@+id/home_widget_container">

<ImageView
android:layout_width="wrap_content"
android:layout_height="76dp"
android:layout_weight="1"
android:id="@+id/widget_img1"
android:visibility="gone"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="76dp"
android:layout_weight="1"
android:id="@+id/widget_img2"
android:visibility="gone"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="76dp"
android:layout_weight="1"
android:id="@+id/widget_img3"
android:visibility="gone"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="76dp"
android:layout_weight="1"
android:id="@+id/widget_img4"
android:visibility="gone"/>
</LinearLayout>
9 changes: 9 additions & 0 deletions android/app/src/main/res/xml/home_widget_provider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="80dp"
android:minHeight="40dp"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/home_widget_layout"
android:resizeMode="horizontal"
android:widgetCategory="home_screen">
</appwidget-provider>
59 changes: 57 additions & 2 deletions lib/controllers/episode_tab_controller.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import 'package:flutter/material.dart';
import 'package:home_widget/home_widget.dart';
import 'package:jais/controllers/animes/missing_anime_controller.dart';
import 'package:jais/controllers/episodes/episode_controller.dart';
import 'package:jais/controllers/logger.dart';
import 'package:jais/models/anime.dart';
import 'package:jais/utils.dart';
import 'package:jais/widgets/animes/anime_image.dart';
import 'package:jais/widgets/animes/missing_anime_widget.dart';

class EpisodeTabController with ChangeNotifier {
late MissingAnimeController missingAnimeController;
Expand All @@ -17,8 +23,57 @@ class EpisodeTabController with ChangeNotifier {
episodeController.reset();
notify();

missingAnimeController.load().whenComplete(() => notify());
episodeController.load().whenComplete(() => notify());
missingAnimeController.load().whenComplete(() async {
notify();

try {
final List<AnimeImage> animeImages = [];

for (int i = 0; i < 4; i++) {
final MissingAnimeWidget? missingAnimeWidget =
missingAnimeController.list.elementAtOrNull(i)
as MissingAnimeWidget?;

if (missingAnimeWidget == null) {
break;
}

final Anime anime = missingAnimeWidget.missingAnime.anime;

animeImages.add(AnimeImage(
anime: anime,
width: Const.missingAnimeImageWith,
height: Const.missingAnimeImageHeight,
radius: 360,
));
}

await Future<void>.delayed(const Duration(seconds: 3));
info('EpisodeTabController', 'updateHomeWidget()');

// await HomeWidget.renderFlutterWidget(
// animeImage,
// logicalSize: const Size(Const.missingAnimeImageWith, Const.missingAnimeImageHeight),
// key: 'dashIcon',
// );

await Future.wait([
for (final AnimeImage image in animeImages)
HomeWidget.renderFlutterWidget(
image,
logicalSize: const Size(
Const.missingAnimeImageWith, Const.missingAnimeImageHeight),
key: 'image${animeImages.indexOf(image)}',
),
]);

await HomeWidget.updateWidget(name: 'HomeWidgetProvider');
} catch (exception, stackTrace) {
error('EpisodeTabController', '$exception', exception, stackTrace);
}
});

episodeController.load().whenComplete(notify);
}

void notify() {
Expand Down
16 changes: 14 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:home_widget/home_widget.dart';
import 'package:jais/controllers/animes/anime_detail_controller.dart';
import 'package:jais/controllers/animes/anime_diary_controller.dart';
import 'package:jais/controllers/animes/anime_search_controller.dart';
Expand All @@ -24,11 +25,22 @@ Future<void> main() async {
);
}

class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
State<StatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
static const Color _mainWhiteThemeColor = Color(0xFFa32d26);
static const Color _mainDarkThemeColor = Color(0xFFfde5c9);

const MyApp({super.key});
@override
void initState() {
super.initState();
HomeWidget.setAppGroupId('jais');
}

@override
Widget build(BuildContext context) {
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.0"
home_widget:
dependency: "direct main"
description:
name: home_widget
sha256: "9a0ed6094823b07025727a39d3dc2d3e02c5281372af22d72e611137e0b3c10d"
url: "https://pub.dev"
source: hosted
version: "0.3.0"
html:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
firebase_messaging: ^14.6.5
flutter:
sdk: flutter
home_widget: ^0.3.0
http: ^1.1.0
intl: ^0.18.1
json_annotation: ^4.8.1
Expand Down

0 comments on commit 54e26fd

Please sign in to comment.