2024-09-15 04:25:50 +00:00
|
|
|
import 'dart:math' as math;
|
|
|
|
|
2024-09-21 14:44:08 +00:00
|
|
|
import 'package:action_slider/action_slider.dart';
|
2024-05-19 16:08:20 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-09-21 14:44:08 +00:00
|
|
|
import 'package:gap/gap.dart';
|
2024-05-19 16:08:20 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-08-21 07:39:29 +00:00
|
|
|
import 'package:solian/exceptions/request.dart';
|
2024-08-21 07:25:50 +00:00
|
|
|
import 'package:solian/exceptions/unauthorized.dart';
|
2024-05-19 16:08:20 +00:00
|
|
|
|
2024-09-15 04:25:50 +00:00
|
|
|
extension AppExtensions on BuildContext {
|
2024-07-31 18:10:57 +00:00
|
|
|
void showSnackbar(String content, {SnackBarAction? action}) {
|
2024-05-19 16:08:20 +00:00
|
|
|
ScaffoldMessenger.of(this).showSnackBar(SnackBar(
|
|
|
|
content: Text(content),
|
2024-07-31 18:10:57 +00:00
|
|
|
action: action,
|
2024-05-19 16:08:20 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-06-29 13:03:15 +00:00
|
|
|
void clearSnackbar() {
|
|
|
|
ScaffoldMessenger.of(this).clearSnackBars();
|
|
|
|
}
|
|
|
|
|
2024-06-30 10:03:02 +00:00
|
|
|
Future<void> showModalDialog(String title, desc) {
|
|
|
|
return showDialog<void>(
|
|
|
|
useRootNavigator: true,
|
|
|
|
context: this,
|
|
|
|
builder: (ctx) => AlertDialog(
|
|
|
|
title: Text(title),
|
|
|
|
content: Text(desc),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(ctx),
|
|
|
|
child: Text('okay'.tr),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-02 17:14:42 +00:00
|
|
|
Future<void> showInfoDialog(String title, body) {
|
|
|
|
return showDialog<void>(
|
|
|
|
useRootNavigator: true,
|
|
|
|
context: this,
|
|
|
|
builder: (ctx) => AlertDialog(
|
|
|
|
title: Text(title),
|
|
|
|
content: Text(body),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(ctx),
|
|
|
|
child: Text('okay'.tr),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-17 12:40:44 +00:00
|
|
|
Future<bool> showConfirmDialog(String title, body) async {
|
|
|
|
return await showDialog<bool>(
|
|
|
|
useRootNavigator: true,
|
|
|
|
context: this,
|
|
|
|
builder: (ctx) => AlertDialog(
|
|
|
|
title: Text(title),
|
|
|
|
content: Text(body),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(ctx, false),
|
|
|
|
child: Text('cancel'.tr),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(ctx, true),
|
|
|
|
child: Text('okay'.tr),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
) ??
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
2024-09-21 14:44:08 +00:00
|
|
|
Future<bool> showSlideToConfirmDialog(String title, body) async {
|
|
|
|
return await showDialog<bool>(
|
|
|
|
useRootNavigator: true,
|
|
|
|
context: this,
|
|
|
|
builder: (ctx) => AlertDialog(
|
|
|
|
title: Text(title, textAlign: TextAlign.center),
|
|
|
|
content: SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
child: ListView(
|
|
|
|
shrinkWrap: true,
|
|
|
|
children: [
|
|
|
|
Text(body, textAlign: TextAlign.center),
|
|
|
|
const Gap(28),
|
|
|
|
ActionSlider.standard(
|
|
|
|
icon: const Icon(Icons.send),
|
|
|
|
iconAlignment: Alignment.center,
|
|
|
|
sliderBehavior: SliderBehavior.move,
|
|
|
|
actionThresholdType: ThresholdType.release,
|
|
|
|
toggleColor: Colors.red,
|
|
|
|
action: (controller) async {
|
|
|
|
controller.success();
|
|
|
|
await Future.delayed(const Duration(milliseconds: 500));
|
|
|
|
Navigator.pop(ctx, true);
|
|
|
|
},
|
|
|
|
child: Text('slideToConfirm'.tr),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
actionsAlignment: MainAxisAlignment.center,
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(ctx, false),
|
|
|
|
child: Text('cancel'.tr),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
) ??
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
2024-05-19 16:08:20 +00:00
|
|
|
Future<void> showErrorDialog(dynamic exception) {
|
2024-08-21 07:25:50 +00:00
|
|
|
Widget content = Text(exception.toString().capitalize!);
|
|
|
|
if (exception is UnauthorizedException) {
|
|
|
|
content = Text('errorHappenedUnauthorized'.tr);
|
|
|
|
}
|
2024-08-21 07:39:29 +00:00
|
|
|
if (exception is RequestException) {
|
|
|
|
String overall;
|
|
|
|
switch (exception.data.statusCode) {
|
|
|
|
case 400:
|
|
|
|
overall = 'errorHappenedRequestBad'.tr;
|
|
|
|
break;
|
|
|
|
case 401:
|
|
|
|
overall = 'errorHappenedUnauthorized'.tr;
|
|
|
|
break;
|
|
|
|
case 403:
|
|
|
|
overall = 'errorHappenedRequestForbidden'.tr;
|
|
|
|
break;
|
|
|
|
case 404:
|
|
|
|
overall = 'errorHappenedRequestNotFound'.tr;
|
|
|
|
break;
|
|
|
|
case null:
|
|
|
|
overall = 'errorHappenedRequestConnection'.tr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
overall = 'errorHappenedRequestUnknown'.tr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exception.data.statusCode != null) {
|
|
|
|
content = Text(
|
|
|
|
'$overall\n\n(${exception.data.statusCode}) ${exception.data.bodyString}',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
content = Text(overall);
|
|
|
|
}
|
|
|
|
}
|
2024-07-17 03:38:25 +00:00
|
|
|
|
2024-05-19 16:08:20 +00:00
|
|
|
return showDialog<void>(
|
2024-06-01 12:18:25 +00:00
|
|
|
useRootNavigator: true,
|
2024-05-19 16:08:20 +00:00
|
|
|
context: this,
|
|
|
|
builder: (ctx) => AlertDialog(
|
|
|
|
title: Text('errorHappened'.tr),
|
2024-08-21 07:25:50 +00:00
|
|
|
content: content,
|
2024-05-19 16:08:20 +00:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(ctx),
|
|
|
|
child: Text('okay'.tr),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-09-15 04:25:50 +00:00
|
|
|
|
|
|
|
extension ByteFormatter on int {
|
|
|
|
String formatBytes({int decimals = 2}) {
|
|
|
|
if (this == 0) return '0 Bytes';
|
|
|
|
const k = 1024;
|
|
|
|
final dm = decimals < 0 ? 0 : decimals;
|
|
|
|
final sizes = [
|
|
|
|
'Bytes',
|
|
|
|
'KiB',
|
|
|
|
'MiB',
|
|
|
|
'GiB',
|
|
|
|
'TiB',
|
|
|
|
'PiB',
|
|
|
|
'EiB',
|
|
|
|
'ZiB',
|
|
|
|
'YiB'
|
|
|
|
];
|
|
|
|
final i = (math.log(this) / math.log(k)).floor().toInt();
|
|
|
|
return '${(this / math.pow(k, i)).toStringAsFixed(dm)} ${sizes[i]}';
|
|
|
|
}
|
|
|
|
}
|