ToLaptop
This commit is contained in:
69
lib/utils/NameBeautify.dart
Normal file
69
lib/utils/NameBeautify.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class NameBeautify {
|
||||
|
||||
static final Map<String, String> Longify = {
|
||||
|
||||
"ctr": "Centre",
|
||||
"stn": "Station",
|
||||
"tn": "Town",
|
||||
|
||||
};
|
||||
|
||||
static String beautifyStopName(String label) {
|
||||
|
||||
String stopName = label.toUpperCase();
|
||||
|
||||
// remove <>
|
||||
stopName = stopName.replaceAll("<>", "");
|
||||
|
||||
// remove any parathesese pairs as well as the contents (), [], {}, <>, ><
|
||||
stopName = stopName.replaceAll(RegExp(r'\(.*\)'), '');
|
||||
stopName = stopName.replaceAll(RegExp(r'\[.*\]'), '');
|
||||
stopName = stopName.replaceAll(RegExp(r'\{.*\}'), '');
|
||||
// stopName = stopName.replaceAll(RegExp(r'\<.*\>'), '');
|
||||
stopName = stopName.replaceAll(RegExp(r'\>.*\<'), '');
|
||||
|
||||
|
||||
// remove any special characters except & and /
|
||||
stopName = stopName.replaceAll(RegExp(r'[^a-zA-Z0-9&/ ]'), '');
|
||||
|
||||
// remove any double spaces
|
||||
stopName = stopName.replaceAll(RegExp(r' '), ' ');
|
||||
|
||||
// remove any spaces at the start or end of the string
|
||||
stopName = stopName.trim();
|
||||
|
||||
// replace any short words with their long form
|
||||
for (String phrase in Longify.keys) {
|
||||
stopName = stopName.replaceAll(RegExp(phrase, caseSensitive: false), Longify[phrase]!);
|
||||
}
|
||||
|
||||
|
||||
stopName = stopName.toLowerCase();
|
||||
// Capitalify the first letter of each word
|
||||
try {
|
||||
stopName = stopName.split(' ').map((word) => word[0].toUpperCase() + word.substring(1)).join(' ');
|
||||
} catch (e) {}
|
||||
|
||||
return stopName;
|
||||
|
||||
}
|
||||
|
||||
static String getShortTime(){
|
||||
|
||||
// return the HH:MM with AM and PM and make sure that the hour is 12 hour format and it always double digits. IE 01, 02 etc
|
||||
DateTime now = DateTime.now();
|
||||
String formatted = DateFormat('hh:mm a').format(now);
|
||||
return formatted;
|
||||
}
|
||||
|
||||
static String getLongTime() {
|
||||
DateTime now = DateTime.now();
|
||||
String formattedTime = DateFormat('HH:mm:ss dd.MM.yyyy').format(now);
|
||||
return formattedTime;
|
||||
}
|
||||
|
||||
}
|
||||
125
lib/utils/audio wrapper.dart
Normal file
125
lib/utils/audio wrapper.dart
Normal file
@@ -0,0 +1,125 @@
|
||||
|
||||
import 'package:audioplayers/audioplayers.dart' as audioplayers;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:just_audio/just_audio.dart' as justaudio;
|
||||
|
||||
enum AudioWrapper_State {
|
||||
Playing,
|
||||
NotPlaying
|
||||
}
|
||||
|
||||
class AudioWrapper {
|
||||
|
||||
audioplayers.AudioPlayer _audioPlayer_AudioPlayer = audioplayers.AudioPlayer();
|
||||
justaudio.AudioPlayer _justAudio_AudioPlayer = justaudio.AudioPlayer();
|
||||
|
||||
justaudio.AudioSource _convertSource_JustAudio(AudioWrapperSource source){
|
||||
if (source is AudioWrapperByteSource){
|
||||
return _ByteSource(source.bytes);
|
||||
} else if (source is AudioWrapperAssetSource){
|
||||
return justaudio.AudioSource.asset(source.assetPath);
|
||||
} else {
|
||||
throw Exception("Unknown source type");
|
||||
}
|
||||
}
|
||||
|
||||
audioplayers.Source _convertSource_AudioPlayers(AudioWrapperSource source){
|
||||
if (source is AudioWrapperByteSource){
|
||||
return audioplayers.BytesSource(source.bytes);
|
||||
} else if (source is AudioWrapperAssetSource){
|
||||
return audioplayers.AssetSource(source.assetPath);
|
||||
} else {
|
||||
throw Exception("Unknown source type");
|
||||
}
|
||||
}
|
||||
|
||||
Future<Duration?> play(AudioWrapperSource source) async {
|
||||
if (kIsWeb) {
|
||||
// Use just_audio
|
||||
|
||||
justaudio.AudioSource audioSource = _convertSource_JustAudio(source);
|
||||
|
||||
Duration? duration = await _justAudio_AudioPlayer.setAudioSource(audioSource);
|
||||
|
||||
_justAudio_AudioPlayer.play();
|
||||
|
||||
return duration;
|
||||
|
||||
} else {
|
||||
// Use audioplayers
|
||||
|
||||
audioplayers.Source audioSource = _convertSource_AudioPlayers(source);
|
||||
|
||||
await _audioPlayer_AudioPlayer.play(audioSource);
|
||||
|
||||
return await _audioPlayer_AudioPlayer.getDuration();
|
||||
}
|
||||
}
|
||||
|
||||
void stop(){
|
||||
if (kIsWeb) {
|
||||
_justAudio_AudioPlayer.stop();
|
||||
} else {
|
||||
_audioPlayer_AudioPlayer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
AudioWrapper_State get state {
|
||||
if (kIsWeb) {
|
||||
if (_justAudio_AudioPlayer.playing){
|
||||
return AudioWrapper_State.Playing;
|
||||
} else {
|
||||
return AudioWrapper_State.NotPlaying;
|
||||
}
|
||||
} else {
|
||||
if (_audioPlayer_AudioPlayer.state == audioplayers.PlayerState.playing){
|
||||
return AudioWrapper_State.Playing;
|
||||
} else {
|
||||
return AudioWrapper_State.NotPlaying;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class AudioWrapperSource {
|
||||
|
||||
}
|
||||
|
||||
class AudioWrapperByteSource extends AudioWrapperSource {
|
||||
|
||||
Uint8List bytes = Uint8List(0);
|
||||
|
||||
AudioWrapperByteSource(Uint8List bytes){
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class AudioWrapperAssetSource extends AudioWrapperSource {
|
||||
|
||||
String assetPath = "";
|
||||
|
||||
AudioWrapperAssetSource(String assetPath){
|
||||
this.assetPath = assetPath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class _ByteSource extends justaudio.StreamAudioSource {
|
||||
final List<int> bytes;
|
||||
_ByteSource(this.bytes);
|
||||
|
||||
@override
|
||||
Future<justaudio.StreamAudioResponse> request([int? start, int? end]) async {
|
||||
start ??= 0;
|
||||
end ??= bytes.length;
|
||||
return justaudio.StreamAudioResponse(
|
||||
sourceLength: bytes.length,
|
||||
contentLength: end - start,
|
||||
offset: start,
|
||||
stream: Stream.value(bytes.sublist(start, end)),
|
||||
contentType: 'audio/mpeg',
|
||||
);
|
||||
}
|
||||
}
|
||||
90
lib/utils/delegates.dart
Normal file
90
lib/utils/delegates.dart
Normal file
@@ -0,0 +1,90 @@
|
||||
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
/// Event system
|
||||
|
||||
class ListenerReceipt<T> {
|
||||
Function(T) listener;
|
||||
|
||||
ListenerReceipt(this.listener);
|
||||
}
|
||||
|
||||
class EventDelegate<T> {
|
||||
final List<ListenerReceipt<T>> _receipts = [];
|
||||
|
||||
ListenerReceipt<T> addListener(Function(T) listener) {
|
||||
final receipt = ListenerReceipt(listener);
|
||||
_receipts.add(receipt);
|
||||
return receipt;
|
||||
}
|
||||
|
||||
void removeListener(ListenerReceipt<T> receipt) {
|
||||
_receipts.remove(receipt);
|
||||
print("removed listener");
|
||||
}
|
||||
|
||||
void trigger(T event) {
|
||||
print("triggering event");
|
||||
for (var receipt in _receipts) {
|
||||
print("triggering listener");
|
||||
try {
|
||||
receipt.listener(event);
|
||||
} catch (e) {
|
||||
print("Error in listener: $e");
|
||||
removeListener(receipt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// flutter integration
|
||||
|
||||
class DelegateBuilder<T> extends StatefulWidget {
|
||||
final EventDelegate<T> delegate;
|
||||
final Widget Function(BuildContext, T) builder;
|
||||
final Widget Function(BuildContext)? defaultBuilder;
|
||||
|
||||
DelegateBuilder({required this.delegate, required this.builder, this.defaultBuilder}) : super(key: UniqueKey())
|
||||
{
|
||||
print("created delegate builder widget");
|
||||
}
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _DelegateBuilderState<T>();
|
||||
|
||||
|
||||
}
|
||||
|
||||
class _DelegateBuilderState<T> extends State<DelegateBuilder<T>> {
|
||||
late ListenerReceipt<T> _receipt;
|
||||
|
||||
T? lastEvent;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
print("init delegate builder widget");
|
||||
_receipt = widget.delegate.addListener((event) {
|
||||
lastEvent = event;
|
||||
print("triggered");
|
||||
setState(() {
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
widget.delegate.removeListener(_receipt);
|
||||
print("disposed");
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
print("rebuilt");
|
||||
print("Valid: ${lastEvent != null}");
|
||||
return lastEvent == null ? widget.defaultBuilder == null ? Container() : widget.defaultBuilder!(context) : widget.builder(context, lastEvent!);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user