2021-02-13 22:50:38 +00:00
|
|
|
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/share.dart';
|
|
|
|
|
|
|
|
import '../../core/enums/viewstate.dart';
|
|
|
|
import '../../core/models/session.dart';
|
|
|
|
import '../../core/util/formatter_util.dart';
|
|
|
|
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';
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
var url = Provider.of<Session>(context).url;
|
|
|
|
|
|
|
|
return BaseView<HistoryModel>(
|
2021-02-04 00:07:03 +00:00
|
|
|
onModelReady: (model) {
|
|
|
|
model.init();
|
|
|
|
return model.getHistory();
|
|
|
|
},
|
2021-02-02 14:33:23 +00:00
|
|
|
builder: (context, model, child) => Scaffold(
|
|
|
|
appBar: MyAppBar(title: Text(translate('titles.history'))),
|
|
|
|
backgroundColor: backgroundColor,
|
|
|
|
body: model.state == ViewState.Busy
|
|
|
|
? Center(child: CircularProgressIndicator())
|
|
|
|
: (model.errorMessage == null
|
|
|
|
? Container(
|
|
|
|
padding: EdgeInsets.all(0),
|
2021-02-13 22:50:38 +00:00
|
|
|
child: RefreshIndicator(onRefresh: () => model.getHistory(), child: _render(model, url, context)))
|
2021-02-02 14:33:23 +00:00
|
|
|
: Container(
|
|
|
|
padding: EdgeInsets.all(25),
|
|
|
|
child: CenteredErrorRow(
|
|
|
|
model.errorMessage,
|
|
|
|
retryCallback: () => model.getHistory(),
|
|
|
|
)))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-13 22:50:38 +00:00
|
|
|
Widget _render(HistoryModel model, String url, BuildContext context) {
|
2021-04-03 12:30:55 +00:00
|
|
|
List<Widget> cards = [];
|
2021-02-02 14:33:23 +00:00
|
|
|
|
|
|
|
if (model.pastes.length > 0) {
|
|
|
|
model.pastes.reversed.forEach((paste) {
|
|
|
|
List<Widget> widgets = [];
|
|
|
|
|
|
|
|
var fullPasteUrl = '$url/${paste.id}';
|
|
|
|
var openInBrowserButton = _renderOpenInBrowser(model, fullPasteUrl);
|
|
|
|
|
|
|
|
var dateWidget = ListTile(
|
|
|
|
title: Text(FormatterUtil.formatEpoch(paste.date.millisecondsSinceEpoch)),
|
|
|
|
subtitle: Text(translate('history.date')),
|
|
|
|
);
|
|
|
|
|
|
|
|
var linkWidget = ListTile(
|
|
|
|
title: Text(translate('history.open_link')),
|
|
|
|
trailing: openInBrowserButton,
|
|
|
|
);
|
|
|
|
|
2021-02-13 22:50:38 +00:00
|
|
|
var copyWidget = ListTile(
|
|
|
|
title: Text(translate('history.copy_link.description')),
|
|
|
|
trailing: IconButton(
|
2021-04-05 20:06:54 +00:00
|
|
|
icon: Icon(Icons.copy, color: blueColor, textDirection: TextDirection.ltr),
|
2021-02-13 22:50:38 +00:00
|
|
|
onPressed: () {
|
|
|
|
FlutterClipboard.copy(fullPasteUrl).then((value) {
|
|
|
|
final snackBar = SnackBar(
|
|
|
|
action: SnackBarAction(
|
|
|
|
label: translate('history.copy_link.dismiss'),
|
2021-04-05 20:06:54 +00:00
|
|
|
textColor: blueColor,
|
2021-02-13 22:50:38 +00:00
|
|
|
onPressed: () {
|
2021-04-03 12:30:55 +00:00
|
|
|
ScaffoldMessenger.of(context).hideCurrentSnackBar();
|
2021-02-13 22:50:38 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
content: Text(translate('history.copy_link.copied')),
|
|
|
|
duration: Duration(seconds: 10),
|
|
|
|
);
|
2021-04-03 12:30:55 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
2021-02-13 22:50:38 +00:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2021-02-02 14:33:23 +00:00
|
|
|
var deleteWidget = ListTile(
|
|
|
|
title: Text(translate('history.delete')),
|
|
|
|
trailing: IconButton(
|
2021-04-05 20:06:54 +00:00
|
|
|
icon: Icon(Icons.delete, color: redColor),
|
2021-02-02 14:33:23 +00:00
|
|
|
onPressed: () {
|
|
|
|
return model.deletePaste(paste.id);
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (!paste.isMulti) {
|
|
|
|
var titleWidget = ListTile(
|
|
|
|
title: Text(paste.filename ?? paste.id),
|
|
|
|
subtitle: Text(translate('history.filename')),
|
|
|
|
);
|
|
|
|
var fileSizeWidget = ListTile(
|
|
|
|
title: Text(FormatterUtil.formatBytes(paste.filesize, 2)),
|
|
|
|
subtitle: Text(translate('history.filesize')),
|
|
|
|
);
|
|
|
|
var idWidget = ListTile(
|
|
|
|
title: Text(paste.id),
|
|
|
|
subtitle: Text(translate('history.id')),
|
|
|
|
);
|
2021-02-03 00:57:42 +00:00
|
|
|
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);
|
2021-02-03 00:57:42 +00:00
|
|
|
widgets.add(fileSizeWidget);
|
|
|
|
widgets.add(mimeTypeWidget);
|
2021-02-02 14:33:23 +00:00
|
|
|
} else {
|
|
|
|
paste.items.forEach((element) {
|
|
|
|
widgets.add(ListTile(
|
|
|
|
title: Text(element),
|
|
|
|
subtitle: Text(translate('history.multipaste_element')),
|
|
|
|
trailing: _renderOpenInBrowser(model, '$url/$element'),
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
widgets.add(dateWidget);
|
|
|
|
widgets.add(linkWidget);
|
2021-02-13 22:50:38 +00:00
|
|
|
widgets.add(copyWidget);
|
2021-02-02 14:33:23 +00:00
|
|
|
widgets.add(deleteWidget);
|
|
|
|
|
|
|
|
var expandable = ExpandableTheme(
|
|
|
|
data: ExpandableThemeData(
|
2021-02-03 00:57:42 +00:00
|
|
|
iconPlacement: ExpandablePanelIconPlacement.right,
|
|
|
|
headerAlignment: ExpandablePanelHeaderAlignment.center,
|
|
|
|
hasIcon: true,
|
2021-04-05 20:06:54 +00:00
|
|
|
iconColor: blueColor,
|
2021-02-03 00:57:42 +00:00
|
|
|
tapHeaderToExpand: true),
|
2021-02-02 14:33:23 +00:00
|
|
|
child: ExpandablePanel(
|
2021-02-03 00:57:42 +00:00
|
|
|
header: InkWell(
|
|
|
|
onLongPress: () => model.deletePaste(paste.id),
|
|
|
|
child: Text(
|
|
|
|
paste.id,
|
2021-04-05 20:06:54 +00:00
|
|
|
style: TextStyle(color: blueColor),
|
2021-02-03 00:57:42 +00:00
|
|
|
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,
|
2021-02-03 00:57:42 +00:00
|
|
|
trailing: Wrap(children: [
|
|
|
|
openInBrowserButton,
|
|
|
|
IconButton(
|
2021-04-05 20:06:54 +00:00
|
|
|
icon: Icon(Icons.share, color: blueColor, textDirection: TextDirection.ltr),
|
2021-02-03 00:57:42 +00:00
|
|
|
onPressed: () {
|
|
|
|
return Share.share(fullPasteUrl);
|
|
|
|
})
|
|
|
|
]),
|
|
|
|
subtitle: Text(!paste.isMulti ? paste.filename : '', style: TextStyle(fontStyle: FontStyle.italic)),
|
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),
|
|
|
|
children: cards,
|
|
|
|
physics: AlwaysScrollableScrollPhysics(),
|
|
|
|
);
|
|
|
|
}
|
2021-02-04 00:07:03 +00:00
|
|
|
|
|
|
|
Widget _renderOpenInBrowser(HistoryModel model, String url) {
|
|
|
|
return IconButton(
|
2021-04-05 20:06:54 +00:00
|
|
|
icon: Icon(Icons.open_in_new, color: blueColor, textDirection: TextDirection.ltr),
|
2021-02-04 00:07:03 +00:00
|
|
|
onPressed: () {
|
|
|
|
return model.openLink(url);
|
|
|
|
});
|
|
|
|
}
|
2021-02-02 14:33:23 +00:00
|
|
|
}
|