nc-photos/lib/widget/album_viewer_mixin.dart

185 lines
4.5 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2021-07-02 13:55:52 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/pref.dart';
2021-07-25 10:42:27 +02:00
import 'package:nc_photos/widget/album_viewer_app_bar.dart';
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
2021-07-25 16:27:19 +02:00
import 'package:nc_photos/widget/selection_app_bar.dart';
2021-07-25 10:52:24 +02:00
import 'package:nc_photos/widget/zoom_menu_button.dart';
mixin AlbumViewerMixin<T extends StatefulWidget>
on SelectableItemStreamListMixin<T> {
@override
initState() {
super.initState();
2021-07-23 22:05:57 +02:00
_thumbZoomLevel = Pref.inst().getAlbumViewerZoomLevelOr(0);
}
@protected
2021-07-23 22:05:57 +02:00
void initCover(Account account, List<File> backingFiles) {
try {
final coverFile =
2021-07-23 22:05:57 +02:00
backingFiles.firstWhere((element) => element.hasPreview ?? false);
_coverPreviewUrl = api_util.getFilePreviewUrl(account, coverFile,
width: 1024, height: 600);
2021-07-23 22:05:57 +02:00
} catch (_) {}
}
@protected
2021-06-30 07:11:02 +02:00
Widget buildNormalAppBar(
BuildContext context,
Account account,
Album album, {
2021-07-23 22:05:57 +02:00
List<Widget>? actions,
List<PopupMenuEntry<int>> Function(BuildContext)? menuItemBuilder,
void Function(int)? onSelectedMenuItem,
2021-06-30 07:11:02 +02:00
}) {
2021-07-25 10:42:27 +02:00
return AlbumViewerAppBar(
account: account,
album: album,
coverPreviewUrl: _coverPreviewUrl,
actions: [
2021-07-25 10:52:24 +02:00
ZoomMenuButton(
initialZoom: _thumbZoomLevel,
minZoom: 0,
maxZoom: 2,
onZoomChanged: (value) {
setState(() {
_thumbZoomLevel = value.round();
});
Pref.inst().setAlbumViewerZoomLevel(_thumbZoomLevel);
},
),
2021-06-30 07:11:02 +02:00
...(actions ?? []),
2021-07-23 22:05:57 +02:00
PopupMenuButton<int>(
2021-07-02 13:55:52 +02:00
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
itemBuilder: (context) => [
PopupMenuItem(
value: -1,
2021-07-25 07:00:38 +02:00
child: Text(L10n.of(context).editAlbumMenuLabel),
2021-07-02 13:55:52 +02:00
),
...(menuItemBuilder?.call(context) ?? []),
],
onSelected: (option) {
if (option >= 0) {
onSelectedMenuItem?.call(option);
} else {
switch (option) {
case _menuValueEdit:
_onAppBarEditPressed(context, album);
break;
default:
_log.shout("[buildNormalAppBar] Unknown value: $option");
break;
}
}
},
),
],
);
}
2021-07-02 13:54:18 +02:00
@protected
Widget buildSelectionAppBar(BuildContext context, List<Widget> actions) {
2021-07-25 16:27:19 +02:00
return SelectionAppBar(
count: selectedListItems.length,
onClosePressed: () {
setState(() {
clearSelectedItems();
});
},
actions: actions,
);
}
2021-07-02 13:55:52 +02:00
@protected
Widget buildEditAppBar(
BuildContext context,
Account account,
Album album, {
2021-07-23 22:05:57 +02:00
List<Widget>? actions,
2021-07-02 13:55:52 +02:00
}) {
2021-07-25 10:42:27 +02:00
return AlbumViewerEditAppBar(
account: account,
album: album,
coverPreviewUrl: _coverPreviewUrl,
2021-07-02 13:55:52 +02:00
actions: actions,
2021-07-25 10:42:27 +02:00
onDonePressed: () {
if (validateEditMode()) {
setState(() {
_isEditMode = false;
});
doneEditMode();
}
},
onAlbumNameSaved: (value) {
_editFormValue.name = value;
},
2021-07-02 13:55:52 +02:00
);
}
@protected
get isEditMode => _isEditMode;
@protected
@mustCallSuper
void enterEditMode() {}
/// Validates the pending modifications
2021-07-02 13:55:52 +02:00
@protected
bool validateEditMode();
@protected
void doneEditMode();
2021-07-02 13:55:52 +02:00
/// Return a new album with the edits
@protected
Album makeEdited(Album album) {
return album.copyWith(
name: _editFormValue.name,
);
}
@protected
int get thumbSize {
switch (_thumbZoomLevel) {
case 1:
return 176;
case 2:
return 256;
case 0:
default:
return 112;
}
}
2021-07-02 13:55:52 +02:00
void _onAppBarEditPressed(BuildContext context, Album album) {
setState(() {
_isEditMode = true;
enterEditMode();
2021-07-02 13:55:52 +02:00
_editFormValue = _EditFormValue();
});
}
2021-07-23 22:05:57 +02:00
String? _coverPreviewUrl;
var _thumbZoomLevel = 0;
2021-07-02 13:55:52 +02:00
var _isEditMode = false;
var _editFormValue = _EditFormValue();
static final _log = Logger("widget.album_viewer_mixin.AlbumViewerMixin");
static const _menuValueEdit = -1;
}
class _EditFormValue {
2021-07-23 22:05:57 +02:00
late String name;
}