ente/lib/ui/home_widget.dart

389 lines
12 KiB
Dart
Raw Normal View History

2020-05-05 12:56:24 +00:00
import 'dart:async';
import 'dart:io';
2020-04-27 15:11:29 +00:00
import 'package:flutter/cupertino.dart';
2020-04-14 15:36:18 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2020-04-14 15:36:18 +00:00
import 'package:flutter/widgets.dart';
import 'package:move_to_background/move_to_background.dart';
import 'package:photos/core/configuration.dart';
2020-05-04 20:03:06 +00:00
import 'package:photos/core/event_bus.dart';
2021-04-20 20:11:39 +00:00
import 'package:photos/db/files_db.dart';
import 'package:photos/events/backup_folders_updated_event.dart';
2020-05-04 20:03:06 +00:00
import 'package:photos/events/local_photos_updated_event.dart';
2021-03-17 21:07:17 +00:00
import 'package:photos/events/subscription_purchased_event.dart';
2020-10-26 11:18:00 +00:00
import 'package:photos/events/tab_changed_event.dart';
2021-03-17 22:08:13 +00:00
import 'package:photos/events/trigger_logout_event.dart';
2021-03-17 21:07:17 +00:00
import 'package:photos/events/user_logged_out_event.dart';
import 'package:photos/models/selected_files.dart';
import 'package:photos/services/billing_service.dart';
2020-10-03 17:58:26 +00:00
import 'package:photos/services/sync_service.dart';
import 'package:photos/ui/backup_folder_selection_widget.dart';
import 'package:photos/ui/collections_gallery_widget.dart';
2020-11-16 08:28:43 +00:00
import 'package:photos/ui/extents_page_view.dart';
2020-05-05 15:50:36 +00:00
import 'package:photos/ui/gallery.dart';
import 'package:photos/ui/gallery_app_bar_widget.dart';
2021-03-12 08:40:36 +00:00
import 'package:photos/ui/grant_permissions_widget.dart';
2020-07-21 10:25:19 +00:00
import 'package:photos/ui/memories_widget.dart';
2020-10-03 17:56:18 +00:00
import 'package:photos/services/user_service.dart';
2021-02-06 21:48:46 +00:00
import 'package:photos/ui/nav_bar.dart';
import 'package:photos/ui/settings_button.dart';
import 'package:photos/ui/shared_collections_gallery.dart';
2020-05-02 17:12:03 +00:00
import 'package:logging/logging.dart';
2020-11-10 14:55:28 +00:00
import 'package:photos/ui/sign_in_header_widget.dart';
2020-11-12 16:32:10 +00:00
import 'package:photos/ui/sync_indicator.dart';
2021-03-17 22:08:13 +00:00
import 'package:photos/utils/dialog_util.dart';
import 'package:uni_links/uni_links.dart';
2020-04-14 15:36:18 +00:00
class HomeWidget extends StatefulWidget {
2021-03-21 06:27:35 +00:00
const HomeWidget({Key key}) : super(key: key);
2020-04-14 15:36:18 +00:00
@override
State<StatefulWidget> createState() => _HomeWidgetState();
}
class _HomeWidgetState extends State<HomeWidget> {
2021-04-27 15:00:52 +00:00
static const _deviceFolderGalleryWidget = const CollectionsGalleryWidget();
static const _sharedCollectionGallery = const SharedCollectionGallery();
static const _headerWidget = HeaderWidget();
2020-05-04 20:10:11 +00:00
final _logger = Logger("HomeWidgetState");
final _selectedFiles = SelectedFiles();
final _settingsButton = SettingsButton();
2020-11-10 11:36:51 +00:00
final PageController _pageController = PageController();
2021-02-06 19:48:29 +00:00
int _selectedTabIndex = 0;
2021-04-05 23:39:44 +00:00
Widget _headerWidgetWithSettingsButton;
2020-06-15 19:57:48 +00:00
2020-10-26 11:18:00 +00:00
StreamSubscription<TabChangedEvent> _tabChangedEventSubscription;
2021-03-17 21:07:17 +00:00
StreamSubscription<SubscriptionPurchasedEvent> _subscriptionPurchaseEvent;
2021-03-17 22:08:13 +00:00
StreamSubscription<TriggerLogoutEvent> _triggerLogoutEvent;
2021-03-17 21:07:17 +00:00
StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
StreamSubscription<BackupFoldersUpdatedEvent> _backupFoldersUpdatedEvent;
2020-04-14 15:36:18 +00:00
2020-05-02 17:12:03 +00:00
@override
void initState() {
2021-02-05 19:04:15 +00:00
_logger.info("Building initstate");
2021-04-05 23:39:44 +00:00
_headerWidgetWithSettingsButton = Container(
margin: const EdgeInsets.only(top: 12),
child: Stack(
children: [
_headerWidget,
_settingsButton,
],
),
);
2020-10-26 11:18:00 +00:00
_tabChangedEventSubscription =
2020-11-16 08:28:43 +00:00
Bus.instance.on<TabChangedEvent>().listen((event) {
if (event.source != TabChangedEventSource.tab_bar) {
2021-02-06 19:48:29 +00:00
setState(() {
_selectedTabIndex = event.selectedIndex;
});
2020-11-16 08:28:43 +00:00
}
if (event.source != TabChangedEventSource.page_view) {
_pageController.animateToPage(
event.selectedIndex,
duration: Duration(milliseconds: 150),
curve: Curves.easeIn,
);
}
2020-10-26 11:18:00 +00:00
});
2021-03-17 21:07:17 +00:00
_subscriptionPurchaseEvent =
Bus.instance.on<SubscriptionPurchasedEvent>().listen((event) {
setState(() {});
});
2021-03-17 22:08:13 +00:00
_triggerLogoutEvent =
Bus.instance.on<TriggerLogoutEvent>().listen((event) async {
AlertDialog alert = AlertDialog(
title: Text("session expired"),
content: Text("please login again"),
actions: [
TextButton(
child: Text(
"ok",
style: TextStyle(
color: Theme.of(context).buttonColor,
),
),
onPressed: () async {
2021-03-21 11:21:45 +00:00
Navigator.of(context, rootNavigator: true).pop('dialog');
2021-03-17 22:08:13 +00:00
final dialog = createProgressDialog(context, "logging out...");
await dialog.show();
await Configuration.instance.logout();
await dialog.hide();
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
});
2021-03-17 21:07:17 +00:00
_loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
setState(() {});
});
_backupFoldersUpdatedEvent =
Bus.instance.on<BackupFoldersUpdatedEvent>().listen((event) {
setState(() {});
});
_initDeepLinks();
if (Configuration.instance.getPathsToBackUp().isEmpty &&
Configuration.instance.hasConfiguredAccount() &&
BillingService.instance.hasActiveSubscription()) {
2021-05-09 18:44:05 +00:00
showBackupFolderSelectionDialog(context);
}
2020-05-04 20:03:06 +00:00
super.initState();
2020-05-02 17:12:03 +00:00
}
2020-04-14 15:36:18 +00:00
@override
Widget build(BuildContext context) {
2021-02-05 19:04:15 +00:00
_logger.info("Building home_Widget");
return WillPopScope(
child: Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(0),
child: Container(),
),
2021-05-09 18:44:05 +00:00
body: _getBody(),
),
onWillPop: () async {
if (Platform.isAndroid) {
MoveToBackground.moveTaskToBack();
return false;
} else {
return true;
}
},
);
}
2021-05-09 18:44:05 +00:00
Widget _getBody() {
if (!Configuration.instance.hasConfiguredAccount()) {
return SignInHeader();
}
if (!SyncService.instance.hasGrantedPermissions()) {
return GrantPermissionsWidget();
}
return Stack(
children: [
ExtentsPageView(
children: [
_getMainGalleryWidget(),
_deviceFolderGalleryWidget,
_sharedCollectionGallery,
],
onPageChanged: (page) {
Bus.instance.fire(TabChangedEvent(
page,
TabChangedEventSource.page_view,
));
},
physics: NeverScrollableScrollPhysics(),
controller: _pageController,
),
Align(
alignment: Alignment.bottomCenter,
child: _buildBottomNavigationBar(),
),
],
);
}
Future<bool> _initDeepLinks() async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
String initialLink = await getInitialLink();
// Parse the link and warn the user, if it is not correct,
// but keep in mind it could be `null`.
if (initialLink != null) {
_logger.info("Initial link received: " + initialLink);
_getCredentials(context, initialLink);
return true;
} else {
_logger.info("No initial link received.");
}
} on PlatformException {
// Handle exception by warning the user their action did not succeed
// return?
_logger.severe("PlatformException thrown while getting initial link");
}
// Attach a listener to the stream
getLinksStream().listen((String link) {
_logger.info("Link received: " + link);
_getCredentials(context, link);
}, onError: (err) {
_logger.severe(err);
});
return false;
}
void _getCredentials(BuildContext context, String link) {
if (Configuration.instance.hasConfiguredAccount()) {
return;
}
final ott = Uri.parse(link).queryParameters["ott"];
2020-10-03 17:56:18 +00:00
UserService.instance.getCredentials(context, ott);
}
2020-06-15 19:57:48 +00:00
Widget _getMainGalleryWidget() {
2021-04-21 13:09:18 +00:00
var header;
if (_selectedFiles.files.isEmpty) {
header = _headerWidgetWithSettingsButton;
} else {
header = _headerWidget;
}
final gallery = Gallery(
asyncLoader: (creationStartTime, creationEndTime, {limit, asc}) {
final importantPaths = Configuration.instance.getPathsToBackUp();
if (importantPaths.isNotEmpty) {
return FilesDB.instance.getFilesInPaths(
creationStartTime, creationEndTime, importantPaths.toList(),
limit: limit, asc: asc);
} else {
2021-05-09 18:44:05 +00:00
return FilesDB.instance.getAllFiles(
creationStartTime, creationEndTime,
limit: limit, asc: asc);
}
},
2021-04-21 13:09:18 +00:00
reloadEvent: Bus.instance.on<LocalPhotosUpdatedEvent>(),
tagPrefix: "home_gallery",
selectedFiles: _selectedFiles,
headerWidget: header,
2020-06-06 14:24:00 +00:00
);
2021-05-02 20:42:26 +00:00
return Stack(
children: [
Container(
margin: const EdgeInsets.only(bottom: 50),
child: gallery,
),
2021-05-02 20:42:26 +00:00
HomePageAppBar(_selectedFiles),
],
);
2020-06-06 14:24:00 +00:00
}
2020-11-10 11:36:51 +00:00
Widget _buildBottomNavigationBar() {
2021-02-06 19:48:29 +00:00
return Container(
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.90),
),
child: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 8),
child: GNav(
rippleColor: Theme.of(context).buttonColor.withOpacity(0.20),
hoverColor: Theme.of(context).buttonColor.withOpacity(0.20),
2021-02-06 19:48:29 +00:00
gap: 8,
activeColor: Theme.of(context).buttonColor.withOpacity(0.75),
iconSize: 24,
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
duration: Duration(milliseconds: 400),
tabMargin: EdgeInsets.only(left: 8, right: 8),
2021-02-06 19:48:29 +00:00
tabBackgroundColor:
Theme.of(context).appBarTheme.color.withOpacity(0.7),
haptic: false,
tabs: [
GButton(
icon: Icons.photo_library_outlined,
text: 'photos',
),
GButton(
icon: Icons.folder_special_outlined,
text: 'albums',
),
GButton(
icon: Icons.folder_shared_outlined,
text: 'shared',
),
],
selectedIndex: _selectedTabIndex,
onTabChange: (index) {
setState(() {
Bus.instance.fire(TabChangedEvent(
index,
TabChangedEventSource.tab_bar,
));
});
}),
),
2020-11-10 11:36:51 +00:00
),
);
}
2020-05-02 17:12:03 +00:00
@override
void dispose() {
2020-10-26 11:18:00 +00:00
_tabChangedEventSubscription.cancel();
2021-03-17 21:07:17 +00:00
_subscriptionPurchaseEvent.cancel();
2021-03-17 22:08:13 +00:00
_triggerLogoutEvent.cancel();
2021-03-17 21:07:17 +00:00
_loggedOutEvent.cancel();
_backupFoldersUpdatedEvent.cancel();
2020-05-02 17:12:03 +00:00
super.dispose();
}
2020-04-14 15:36:18 +00:00
}
2020-11-10 11:36:51 +00:00
2021-05-02 20:42:26 +00:00
class HomePageAppBar extends StatefulWidget {
const HomePageAppBar(
this.selectedFiles, {
Key key,
}) : super(key: key);
final SelectedFiles selectedFiles;
@override
_HomePageAppBarState createState() => _HomePageAppBarState();
}
class _HomePageAppBarState extends State<HomePageAppBar> {
@override
void initState() {
super.initState();
widget.selectedFiles.addListener(() {
setState(() {});
});
}
@override
Widget build(BuildContext context) {
final appBar = Container(
height: 60,
child: GalleryAppBarWidget(
GalleryAppBarType.homepage,
null,
widget.selectedFiles,
),
);
if (widget.selectedFiles.files.isEmpty) {
return IgnorePointer(child: appBar);
} else {
return appBar;
}
}
}
2021-02-05 19:04:15 +00:00
class HeaderWidget extends StatelessWidget {
static const _memoriesWidget = const MemoriesWidget();
static const _syncIndicator = const SyncIndicator();
const HeaderWidget({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
Logger("Header").info("Building header widget");
const list = [
_syncIndicator,
2021-02-05 19:04:15 +00:00
_memoriesWidget,
];
2021-04-19 12:52:54 +00:00
return Column(
children: list,
crossAxisAlignment: CrossAxisAlignment.start,
);
2021-02-05 19:04:15 +00:00
}
}