fbmobile/lib/ui/views/history_view.dart

208 lines
7 KiB
Dart
Raw Normal View History

import 'package:clipboard/clipboard.dart';
2021-02-02 14:33:23 +00:00
import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:provider/provider.dart';
import 'package:share_plus/share_plus.dart';
2021-02-02 14:33:23 +00:00
import '../../core/enums/viewstate.dart';
import '../../core/models/session.dart';
import '../../core/util/formatter_util.dart';
import '../../core/util/paste_util.dart';
2021-02-02 14:33:23 +00:00
import '../../core/viewmodels/history_model.dart';
import '../../ui/widgets/centered_error_row.dart';
import '../shared/app_colors.dart';
import '../widgets/my_appbar.dart';
import 'base_view.dart';
class HistoryView extends StatelessWidget {
static const routeName = '/history';
2023-01-04 20:17:54 +00:00
const HistoryView({super.key});
2021-02-02 14:33:23 +00:00
@override
Widget build(BuildContext context) {
return BaseView<HistoryModel>(
onModelReady: (model) {
model.init();
return model.getHistory();
},
2023-01-16 00:44:34 +00:00
builder: (context, model, child) => Scaffold(
appBar: MyAppBar(title: Text(translate('titles.history'))),
body: _render(model, context)),
2021-02-02 14:33:23 +00:00
);
}
Widget _render(HistoryModel model, BuildContext context) {
var url = Provider.of<Session>(context).url;
2023-01-04 20:17:54 +00:00
return model.state == ViewState.busy
? const Center(child: CircularProgressIndicator())
: (model.errorMessage == null
? Container(
2023-01-04 20:17:54 +00:00
padding: const EdgeInsets.all(0),
child: RefreshIndicator(
2023-01-16 00:44:34 +00:00
onRefresh: () async => await model.getHistory(),
child: _renderItems(model, url, context)))
: Container(
2023-01-04 20:17:54 +00:00
padding: const EdgeInsets.all(25),
child: CenteredErrorRow(
model.errorMessage,
retryCallback: () => model.getHistory(),
)));
}
Widget _renderItems(HistoryModel model, String url, BuildContext context) {
List<Widget> cards = [];
2021-02-02 14:33:23 +00:00
2023-01-04 20:17:54 +00:00
if (model.pastes.isNotEmpty) {
for (var paste in model.pastes.reversed) {
2021-02-02 14:33:23 +00:00
List<Widget> widgets = [];
var fullPasteUrl = PasteUtil.generateLink(url, paste.id);
2021-02-02 14:33:23 +00:00
var openInBrowserButton = _renderOpenInBrowser(model, fullPasteUrl);
var dateWidget = ListTile(
2023-01-16 00:44:34 +00:00
title: Text(
FormatterUtil.formatEpoch(paste.date!.millisecondsSinceEpoch)),
2021-02-02 14:33:23 +00:00
subtitle: Text(translate('history.date')),
);
var linkWidget = ListTile(
title: Text(translate('history.open_link')),
trailing: openInBrowserButton,
);
var copyWidget = ListTile(
title: Text(translate('history.copy_link.description')),
trailing: IconButton(
2023-01-16 00:44:34 +00:00
icon: const Icon(Icons.copy,
color: blueColor, textDirection: TextDirection.ltr),
onPressed: () {
FlutterClipboard.copy(fullPasteUrl).then((value) {
final snackBar = SnackBar(
action: SnackBarAction(
label: translate('history.copy_link.dismiss'),
textColor: blueColor,
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
),
content: Text(translate('history.copy_link.copied')),
2023-01-04 20:17:54 +00:00
duration: const Duration(seconds: 10),
);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
});
}));
2021-02-02 14:33:23 +00:00
var deleteWidget = ListTile(
title: Text(translate('history.delete')),
trailing: IconButton(
2023-01-04 20:17:54 +00:00
icon: const Icon(Icons.delete, color: redColor),
2021-02-02 14:33:23 +00:00
onPressed: () {
model.deletePaste(paste.id);
2021-02-02 14:33:23 +00:00
}));
if (!paste.isMulti!) {
2021-02-02 14:33:23 +00:00
var titleWidget = ListTile(
title: Text(paste.filename ?? paste.id),
subtitle: Text(translate('history.filename')),
);
var fileSizeWidget = ListTile(
title: Text(FormatterUtil.formatBytes(paste.filesize as int, 2)),
2021-02-02 14:33:23 +00:00
subtitle: Text(translate('history.filesize')),
);
var idWidget = ListTile(
title: Text(paste.id),
subtitle: Text(translate('history.id')),
);
var mimeTypeWidget = ListTile(
title: Text(paste.mimetype!),
subtitle: Text(translate('history.mimetype')),
);
2021-02-02 14:33:23 +00:00
widgets.add(titleWidget);
widgets.add(idWidget);
widgets.add(fileSizeWidget);
widgets.add(mimeTypeWidget);
2021-02-02 14:33:23 +00:00
} else {
2023-01-04 20:17:54 +00:00
for (var element in paste.items!) {
2021-02-02 14:33:23 +00:00
widgets.add(ListTile(
title: Text(element!),
2021-02-02 14:33:23 +00:00
subtitle: Text(translate('history.multipaste_element')),
trailing: _renderOpenInBrowser(model, '$url/$element'),
));
2023-01-04 20:17:54 +00:00
}
2021-02-02 14:33:23 +00:00
}
widgets.add(dateWidget);
widgets.add(linkWidget);
widgets.add(copyWidget);
2021-02-02 14:33:23 +00:00
widgets.add(deleteWidget);
var expandable = ExpandableTheme(
2023-01-04 20:17:54 +00:00
data: const ExpandableThemeData(
iconPlacement: ExpandablePanelIconPlacement.right,
headerAlignment: ExpandablePanelHeaderAlignment.center,
hasIcon: true,
iconColor: blueColor,
tapHeaderToExpand: true),
2021-02-02 14:33:23 +00:00
child: ExpandablePanel(
2021-05-08 09:17:53 +00:00
collapsed: Container(),
header: InkWell(
child: Text(
paste.id,
2023-01-04 20:17:54 +00:00
style: const TextStyle(color: blueColor),
textAlign: TextAlign.left,
)),
2021-02-02 14:33:23 +00:00
expanded: Column(
mainAxisSize: MainAxisSize.min,
children: widgets,
),
),
);
cards.add(Card(
child: ListTile(
title: expandable,
trailing: Wrap(children: [
openInBrowserButton,
IconButton(
2023-01-16 00:44:34 +00:00
icon: const Icon(Icons.share,
color: blueColor, textDirection: TextDirection.ltr),
onPressed: () async {
await Share.share(fullPasteUrl);
})
]),
2023-01-16 00:44:34 +00:00
subtitle: Text(!paste.isMulti! ? paste.filename! : '',
style: const TextStyle(fontStyle: FontStyle.italic)),
2021-02-02 14:33:23 +00:00
),
));
2023-01-04 20:17:54 +00:00
}
2021-02-02 14:33:23 +00:00
} else {
cards.add(Card(
child: ListTile(
title: Text(translate('history.no_items')),
),
));
}
return ListView(
padding: const EdgeInsets.all(8),
2023-01-04 20:17:54 +00:00
physics: const AlwaysScrollableScrollPhysics(),
2021-02-02 14:33:23 +00:00
children: cards,
);
}
Widget _renderOpenInBrowser(HistoryModel model, String url) {
return IconButton(
2023-01-16 00:44:34 +00:00
icon: const Icon(Icons.open_in_new,
color: blueColor, textDirection: TextDirection.ltr),
onPressed: () {
return model.openLink(url);
});
}
2021-02-02 14:33:23 +00:00
}