This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add home widget feature displaying the first four missing anime
- Loading branch information
Showing
15 changed files
with
263 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
android/app/src/main/kotlin/fr/ziedelth/jais/HomeWidgetProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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) | ||
} | ||
|
||
// Nothing in your watchlist yet | ||
if (image1 != null || image2 != null || image3 != null || image4 != null) { | ||
setViewVisibility(R.id.widget_nothing, View.GONE) | ||
} else { | ||
setViewVisibility(R.id.widget_nothing, View.VISIBLE) | ||
setTextViewText(R.id.widget_nothing, "Nothing in your watchlist yet") | ||
} | ||
} | ||
|
||
appWidgetManager.updateAppWidget(widgetId, views) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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="wrap_content" | ||
android:layout_weight="1" | ||
android:id="@+id/widget_img1" | ||
android:visibility="gone"/> | ||
|
||
<ImageView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:id="@+id/widget_img2" | ||
android:visibility="gone"/> | ||
|
||
<ImageView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:id="@+id/widget_img3" | ||
android:visibility="gone"/> | ||
|
||
<ImageView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:id="@+id/widget_img4" | ||
android:visibility="gone"/> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:text="Nothing in your watchlist yet" | ||
android:gravity="center" | ||
android:id="@+id/widget_nothing"/> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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/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 HomeWidgetController { | ||
late MissingAnimeController missingAnimeController; | ||
static const int limit = 4; | ||
|
||
void init() { | ||
missingAnimeController = | ||
MissingAnimeController(limit: 4, notifyListenersCallback: () {}); | ||
} | ||
|
||
Future<void> notify() async { | ||
missingAnimeController.reset(); | ||
await missingAnimeController.load(); | ||
|
||
try { | ||
final List<AnimeImage> animeImages = []; | ||
|
||
for (int i = 0; i < limit; 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 * 10, | ||
height: Const.missingAnimeImageHeight * 10, | ||
radius: 360, | ||
), | ||
); | ||
} | ||
|
||
info( | ||
'EpisodeTabController', | ||
'updateHomeWidget() - ${animeImages.length} images', | ||
); | ||
|
||
if (animeImages.isNotEmpty) { | ||
await Future.wait([ | ||
for (final AnimeImage image in animeImages) | ||
HomeWidget.renderFlutterWidget( | ||
image, | ||
logicalSize: const Size( | ||
Const.missingAnimeImageWith * 10, | ||
Const.missingAnimeImageHeight * 10, | ||
), | ||
key: 'image${animeImages.indexOf(image)}', | ||
), | ||
]); | ||
} else { | ||
await Future.wait([ | ||
for (int i = 0; i < limit; i++) | ||
HomeWidget.saveWidgetData('image$i', null), | ||
]); | ||
} | ||
|
||
await HomeWidget.updateWidget(name: 'HomeWidgetProvider'); | ||
} catch (exception, stackTrace) { | ||
error('EpisodeTabController', '$exception', exception, stackTrace); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters