nc-photos/lib/widget/home.dart

150 lines
3.8 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2021-08-08 14:09:21 +02:00
import 'package:logging/logging.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/account.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
2021-08-08 14:09:21 +02:00
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/k.dart' as k;
2021-08-08 14:09:21 +02:00
import 'package:nc_photos/lab.dart';
import 'package:nc_photos/pref.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/theme.dart';
2021-08-08 14:09:21 +02:00
import 'package:nc_photos/use_case/import_potential_shared_album.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/home_albums.dart';
import 'package:nc_photos/widget/home_photos.dart';
class HomeArguments {
HomeArguments(this.account);
final Account account;
}
class Home extends StatefulWidget {
static const routeName = "/home";
2021-07-23 22:05:57 +02:00
static Route buildRoute(HomeArguments args) => MaterialPageRoute(
builder: (context) => Home.fromArgs(args),
);
2021-09-15 08:58:06 +02:00
const Home({
2021-07-23 22:05:57 +02:00
Key? key,
required this.account,
2021-04-10 06:28:12 +02:00
}) : super(key: key);
2021-07-23 22:05:57 +02:00
Home.fromArgs(HomeArguments args, {Key? key})
2021-04-10 06:28:12 +02:00
: this(
2021-09-15 08:58:06 +02:00
key: key,
2021-04-10 06:28:12 +02:00
account: args.account,
);
@override
createState() => _HomeState();
final Account account;
}
class _HomeState extends State<Home> {
2021-08-08 14:09:21 +02:00
@override
initState() {
super.initState();
if (Lab().enableSharedAlbum) {
_importPotentialSharedAlbum().then((value) {
if (value.isNotEmpty) {
Pref.inst().setNewSharedAlbum(true);
}
});
}
}
2021-04-10 06:28:12 +02:00
@override
build(BuildContext context) {
return AppTheme(
child: Scaffold(
bottomNavigationBar: _buildBottomNavigationBar(context),
body: Builder(builder: (context) => _buildContent(context)),
),
);
}
Widget _buildBottomNavigationBar(BuildContext context) {
return BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: const Icon(Icons.photo_outlined),
label: L10n.global().photosTabLabel,
2021-04-10 06:28:12 +02:00
),
BottomNavigationBarItem(
2021-08-29 16:16:42 +02:00
icon: const Icon(Icons.grid_view_outlined),
label: L10n.global().collectionsTooltip,
2021-04-10 06:28:12 +02:00
),
],
currentIndex: _nextPage,
onTap: _onTapNavItem,
);
}
Widget _buildContent(BuildContext context) {
return PageView.builder(
controller: _pageController,
physics: const NeverScrollableScrollPhysics(),
itemCount: 2,
itemBuilder: _buildPage,
);
}
Widget _buildPage(BuildContext context, int index) {
switch (index) {
case 0:
return _buildPhotosPage(context);
case 1:
return _buildAlbumsPage(context);
default:
throw ArgumentError("Invalid page index: $index");
}
}
Widget _buildPhotosPage(BuildContext context) {
return HomePhotos(
account: widget.account,
);
}
Widget _buildAlbumsPage(BuildContext context) {
return HomeAlbums(
account: widget.account,
);
}
void _onTapNavItem(int index) {
_pageController.animateToPage(index,
duration: k.animationDurationNormal, curve: Curves.easeInOut);
setState(() {
_nextPage = index;
});
}
2021-08-08 14:09:21 +02:00
Future<List<Album>> _importPotentialSharedAlbum() async {
2021-09-15 08:58:06 +02:00
const fileRepo = FileRepo(FileWebdavDataSource());
2021-08-08 14:09:21 +02:00
// don't want the potential albums to be cached at this moment
final albumRepo = AlbumRepo(AlbumRemoteDataSource());
try {
return await ImportPotentialSharedAlbum(fileRepo, albumRepo)(
widget.account);
} catch (e, stacktrace) {
_log.shout(
"[_importPotentialSharedAlbum] Failed while ImportPotentialSharedAlbum",
e,
stacktrace);
return [];
}
}
2021-07-23 22:05:57 +02:00
final _pageController = PageController(initialPage: 0, keepPage: false);
2021-04-10 06:28:12 +02:00
int _nextPage = 0;
2021-08-08 14:09:21 +02:00
static final _log = Logger("widget.home._HomeState");
2021-04-10 06:28:12 +02:00
}