shlink-manager/lib/views/url_detail_view.dart

243 lines
8.2 KiB
Dart
Raw Normal View History

2023-07-09 23:00:00 +02:00
import 'package:flutter/material.dart';
2024-01-27 23:07:06 +01:00
import 'package:shlink_app/API/Classes/ShortURL/short_url.dart';
2023-07-09 23:00:00 +02:00
import 'package:intl/intl.dart';
2024-01-27 23:07:06 +01:00
import 'package:shlink_app/API/server_manager.dart';
2024-01-28 01:05:35 +01:00
import 'package:shlink_app/widgets/url_tags_list_widget.dart';
2024-01-27 23:07:06 +01:00
import '../globals.dart' as globals;
2023-07-09 23:00:00 +02:00
class URLDetailView extends StatefulWidget {
const URLDetailView({super.key, required this.shortURL});
final ShortURL shortURL;
@override
State<URLDetailView> createState() => _URLDetailViewState();
}
class _URLDetailViewState extends State<URLDetailView> {
Future showDeletionConfirmation() {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("Delete Short URL"),
content: SingleChildScrollView(
child: ListBody(
children: [
const Text("You're about to delete"),
2024-01-27 23:07:06 +01:00
const SizedBox(height: 4),
2024-01-28 00:32:09 +01:00
Text(
widget.shortURL.title ?? widget.shortURL.shortCode,
style: const TextStyle(fontStyle: FontStyle.italic),
),
2024-01-27 23:07:06 +01:00
const SizedBox(height: 4),
2023-07-09 23:00:00 +02:00
const Text("It'll be gone forever! (a very long time)")
],
),
),
actions: [
2024-01-28 00:32:09 +01:00
TextButton(
onPressed: () => {Navigator.of(context).pop()},
child: const Text("Cancel")),
2023-07-09 23:00:00 +02:00
TextButton(
onPressed: () async {
2024-01-28 00:32:09 +01:00
var response = await globals.serverManager
.deleteShortUrl(widget.shortURL.shortCode);
2023-07-09 23:00:00 +02:00
response.fold((l) {
Navigator.pop(context);
Navigator.pop(context, "reload");
2024-01-28 00:32:09 +01:00
final snackBar = SnackBar(
content: const Text("Short URL deleted!"),
backgroundColor: Colors.green[400],
behavior: SnackBarBehavior.floating);
2023-07-09 23:00:00 +02:00
ScaffoldMessenger.of(context).showSnackBar(snackBar);
return true;
}, (r) {
var text = "";
if (r is RequestFailure) {
text = r.description;
2024-01-28 00:32:09 +01:00
} else {
2023-07-09 23:00:00 +02:00
text = (r as ApiFailure).detail;
}
2024-01-28 00:32:09 +01:00
final snackBar = SnackBar(
content: Text(text),
backgroundColor: Colors.red[400],
behavior: SnackBarBehavior.floating);
2023-07-09 23:00:00 +02:00
ScaffoldMessenger.of(context).showSnackBar(snackBar);
return false;
});
},
2024-01-28 00:32:09 +01:00
child:
const Text("Delete", style: TextStyle(color: Colors.red)),
2023-07-09 23:00:00 +02:00
)
],
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar.medium(
2024-01-28 00:32:09 +01:00
title: Text(widget.shortURL.title ?? widget.shortURL.shortCode,
style: const TextStyle(fontWeight: FontWeight.bold)),
2023-07-09 23:00:00 +02:00
actions: [
2024-01-28 00:32:09 +01:00
IconButton(
onPressed: () {
showDeletionConfirmation();
},
icon: const Icon(
Icons.delete,
color: Colors.red,
))
2023-07-09 23:00:00 +02:00
],
),
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.only(left: 16.0, right: 16.0),
2024-01-28 01:05:35 +01:00
child: UrlTagsListWidget(tags: widget.shortURL.tags)
2023-07-09 23:00:00 +02:00
),
),
_ListCell(title: "Short Code", content: widget.shortURL.shortCode),
_ListCell(title: "Short URL", content: widget.shortURL.shortUrl),
_ListCell(title: "Long URL", content: widget.shortURL.longUrl),
2024-01-28 00:32:09 +01:00
_ListCell(
title: "iOS",
content: widget.shortURL.deviceLongUrls.ios,
sub: true),
_ListCell(
title: "Android",
content: widget.shortURL.deviceLongUrls.android,
sub: true),
_ListCell(
title: "Desktop",
content: widget.shortURL.deviceLongUrls.desktop,
sub: true),
_ListCell(
title: "Creation Date", content: widget.shortURL.dateCreated),
2024-01-27 23:07:06 +01:00
const _ListCell(title: "Visits", content: ""),
2024-01-28 00:32:09 +01:00
_ListCell(
title: "Total",
content: widget.shortURL.visitsSummary.total,
sub: true),
_ListCell(
title: "Non-Bots",
content: widget.shortURL.visitsSummary.nonBots,
sub: true),
_ListCell(
title: "Bots",
content: widget.shortURL.visitsSummary.bots,
sub: true),
2024-01-27 23:07:06 +01:00
const _ListCell(title: "Meta", content: ""),
2024-01-28 00:32:09 +01:00
_ListCell(
title: "Valid Since",
content: widget.shortURL.meta.validSince,
sub: true),
_ListCell(
title: "Valid Until",
content: widget.shortURL.meta.validUntil,
sub: true),
_ListCell(
title: "Max Visits",
content: widget.shortURL.meta.maxVisits,
sub: true),
2023-07-09 23:00:00 +02:00
_ListCell(title: "Domain", content: widget.shortURL.domain),
2024-01-28 00:32:09 +01:00
_ListCell(
title: "Crawlable",
content: widget.shortURL.crawlable,
last: true)
2023-07-09 23:00:00 +02:00
],
),
);
}
}
class _ListCell extends StatefulWidget {
2024-01-28 00:32:09 +01:00
const _ListCell(
{required this.title,
required this.content,
this.sub = false,
this.last = false});
2023-07-09 23:00:00 +02:00
final String title;
final dynamic content;
final bool sub;
final bool last;
@override
State<_ListCell> createState() => _ListCellState();
}
class _ListCellState extends State<_ListCell> {
@override
Widget build(BuildContext context) {
return SliverToBoxAdapter(
2024-01-28 00:32:09 +01:00
child: Padding(
padding: EdgeInsets.only(top: 16, bottom: widget.last ? 30 : 0),
child: Container(
padding: const EdgeInsets.only(top: 16, left: 8, right: 8),
decoration: BoxDecoration(
border: Border(
top: BorderSide(
color: MediaQuery.of(context).platformBrightness ==
Brightness.dark
? Colors.grey[800]!
: Colors.grey[300]!)),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
if (widget.sub)
Padding(
padding: const EdgeInsets.only(right: 4),
child: SizedBox(
width: 20,
height: 6,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: Theme.of(context).brightness == Brightness.dark
? Colors.grey[700]
: Colors.grey[300],
2023-07-09 23:00:00 +02:00
),
),
),
2024-01-28 00:32:09 +01:00
),
Text(
widget.title,
style: const TextStyle(fontWeight: FontWeight.bold),
)
],
),
if (widget.content is bool)
Icon(widget.content ? Icons.check : Icons.close,
color: widget.content ? Colors.green : Colors.red)
else if (widget.content is int)
Text(widget.content.toString())
else if (widget.content is String)
Expanded(
child: Text(
widget.content,
textAlign: TextAlign.end,
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
)
else if (widget.content is DateTime)
Text(DateFormat('yyyy-MM-dd - HH:mm').format(widget.content))
else
const Text("N/A")
],
2023-07-09 23:00:00 +02:00
),
2024-01-28 00:32:09 +01:00
),
));
2023-07-09 23:00:00 +02:00
}
}