cofounderella/lib/utils/helper.dart

132 lines
3.3 KiB
Dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'dart:io' show Platform;
import '../enumerations.dart';
/// Returns [true] if app is running on Mobile (Android or iOS), else [false].
bool get isMobile {
if (kIsWeb) {
return false;
} else {
return Platform.isIOS || Platform.isAndroid;
}
}
/// Compare two lists by their content ignoring their elements order.
bool equalContent(List<dynamic> list1, List<dynamic> list2) {
return const DeepCollectionEquality.unordered().equals(list1, list2);
}
/// Creates a composite ID from the passed [ids].
/// In the format id(1)_id(n)
String getCompoundId(List<String> ids) {
ids.sort(); // sort to ensure the result is the same for any order of ids
return ids.join('_');
}
/// Returns a date format of '$weekday, $day. $month $year $hours:$minutes'.
/// For example: [Sat. 3 Jun. 2024 15:03].
/// If any errors occur, an empty string will be returned.
String formatTimestamp(Timestamp timestamp) {
try {
const List<String> weekdays = [
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
'Sun'
];
const List<String> months = [
'Jan.',
'Feb.',
'Mar.',
'Apr.',
'May',
'Jun.',
'Jul.',
'Aug.',
'Sep.',
'Oct.',
'Nov.',
'Dec.'
];
DateTime dateTime = timestamp.toDate();
String weekday = weekdays[dateTime.weekday - 1];
int day = dateTime.day;
String month = months[dateTime.month - 1];
int year = dateTime.year;
int hour = dateTime.hour;
int minute = dateTime.minute;
return '$weekday, $day. $month $year ${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}';
} catch (e) {
return '';
}
}
/// Get pronoun for given [userGender].
/// Returns [He] if male, [She] if female, else [He/She].
String getPronoun(Gender? userGender) {
switch (userGender) {
case Gender.male:
return 'He';
case Gender.female:
return 'She';
default:
return 'He/She';
}
}
/// Get the [displayName] of our own Enumerations.
String getDisplayText(dynamic option) {
// Check if the option is an enum and has a displayName property
if (option is Enum) {
final dynamicEnum = option as dynamic;
if (dynamicEnum.displayName != null) {
return dynamicEnum.displayName;
}
}
// Fallback to default toString if not an enum
return option.toString().split('.').last;
}
/// Show a simple message dialog
void showMsg(BuildContext context, String title, String content) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(title),
content: Text(content),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
),
);
}
/// Show a red colored SnackBar with a [Dismiss] Button
void showErrorSnackBar(BuildContext context, String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: Colors.red,
action: SnackBarAction(
label: 'Dismiss',
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
),
),
);
}