fix: segregate logic for launch from widget

This commit is contained in:
Prateek Sunal 2024-04-14 14:35:10 +05:30
parent f04e45a461
commit 5da8ee7e27
2 changed files with 54 additions and 57 deletions

View file

@ -10,23 +10,16 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:home_widget/home_widget.dart' as hw;
import 'package:logging/logging.dart';
import 'package:media_extension/media_extension_action_types.dart';
import "package:photos/db/files_db.dart";
import 'package:photos/ente_theme_data.dart';
import "package:photos/generated/l10n.dart";
import "package:photos/l10n/l10n.dart";
import "package:photos/models/collection/collection_items.dart";
import 'package:photos/services/app_lifecycle_service.dart';
import "package:photos/services/collections_service.dart";
import "package:photos/services/favorites_service.dart";
import "package:photos/services/home_widget_service.dart";
import "package:photos/services/machine_learning/machine_learning_controller.dart";
import 'package:photos/services/sync_service.dart';
import 'package:photos/ui/tabs/home_widget.dart';
import "package:photos/ui/viewer/actions/file_viewer.dart";
import "package:photos/ui/viewer/file/detail_page.dart";
import "package:photos/ui/viewer/gallery/collection_page.dart";
import "package:photos/utils/intent_util.dart";
import "package:photos/utils/navigation_util.dart";
class EnteApp extends StatefulWidget {
final Future<void> Function(String) runBackgroundTask;
@ -68,61 +61,15 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
void didChangeDependencies() {
super.didChangeDependencies();
_checkForWidgetLaunch();
hw.HomeWidget.widgetClicked.listen(_launchedFromWidget);
}
void _checkForWidgetLaunch() {
hw.HomeWidget.initiallyLaunchedFromHomeWidget().then(_launchedFromWidget);
}
Future<void> _launchedFromWidget(Uri? uri) async {
if (uri == null) return;
final collectionID =
await FavoritesService.instance.getFavoriteCollectionID();
if (collectionID == null) {
return;
}
final collection = CollectionsService.instance.getCollectionByID(
collectionID,
hw.HomeWidget.initiallyLaunchedFromHomeWidget().then(
(uri) => HomeWidgetService.instance.onLaunchFromWidget(uri, context),
);
if (collection == null) {
return;
}
unawaited(HomeWidgetService.instance.initHomeWidget());
final thumbnail = await CollectionsService.instance.getCover(collection);
final previousGeneratedId =
await hw.HomeWidget.getWidgetData<int>("home_widget_last_img");
final res = previousGeneratedId != null
? await FilesDB.instance.getFile(
previousGeneratedId,
)
: null;
routeToPage(
context,
CollectionPage(
CollectionWithThumbnail(
collection,
thumbnail,
),
),
).ignore();
if (previousGeneratedId == null) return;
if (res == null) return;
final page = DetailPage(
DetailPageConfiguration(
List.unmodifiable([res]),
null,
0,
"collection",
),
hw.HomeWidget.widgetClicked.listen(
(uri) => HomeWidgetService.instance.onLaunchFromWidget(uri, context),
);
routeToPage(context, page, forceCustomPageRoute: true).ignore();
}
setLocale(Locale newLocale) {

View file

@ -8,9 +8,14 @@ import "package:logging/logging.dart";
import "package:photos/core/configuration.dart";
import "package:photos/core/constants.dart";
import "package:photos/db/files_db.dart";
import "package:photos/models/collection/collection_items.dart";
import "package:photos/models/file/file_type.dart";
import "package:photos/services/collections_service.dart";
import "package:photos/services/favorites_service.dart";
import "package:photos/ui/viewer/file/detail_page.dart";
import "package:photos/ui/viewer/gallery/collection_page.dart";
import "package:photos/utils/file_util.dart";
import "package:photos/utils/navigation_util.dart";
import "package:photos/utils/preload_util.dart";
class HomeWidgetService {
@ -171,4 +176,49 @@ class HomeWidgetService {
);
_logger.info(">>> SlideshowWidget cleared");
}
Future<void> onLaunchFromWidget(Uri? uri, BuildContext context) async {
if (uri == null) return;
final collectionID =
await FavoritesService.instance.getFavoriteCollectionID();
if (collectionID == null) {
return;
}
final collection = CollectionsService.instance.getCollectionByID(
collectionID,
);
if (collection == null) {
return;
}
final thumbnail = await CollectionsService.instance.getCover(collection);
final previousGeneratedId =
await hw.HomeWidget.getWidgetData<int>("home_widget_last_img");
final res = previousGeneratedId != null
? await FilesDB.instance.getFile(
previousGeneratedId,
)
: null;
routeToPage(
context,
CollectionPage(
CollectionWithThumbnail(
collection,
thumbnail,
),
),
).ignore();
if (res == null) return;
final page = DetailPage(
DetailPageConfiguration(List.unmodifiable([res]), null, 0, "collection"),
);
routeToPage(context, page, forceCustomPageRoute: true).ignore();
}
}