ADDED EXPORT FUNCTIONALITY

This commit is contained in:
odweta
2026-04-03 19:16:20 +02:00
parent 2b4e64c72f
commit 6089ccda45
12 changed files with 158 additions and 31 deletions
+29 -22
View File
@@ -1,7 +1,14 @@
// gets all quotes from the database and returns them in a list
import 'package:go_router/go_router.dart';
import 'package:file_saver/file_saver.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/foundation.dart'; // for kIsWeb
import 'dart:convert';
import 'dart:typed_data';
import 'package:go_router/go_router.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:frontend/model/quote.dart';
@@ -67,6 +74,8 @@ class QuoteProvider extends ChangeNotifier {
late Quote randomQuote;
bool randomQuoteDeterminator = true;
String exportDir = "";
String get randomQuoteText {
if (randomQuoteDeterminator) {
return randomQuote.body;
@@ -170,30 +179,28 @@ class QuoteProvider extends ChangeNotifier {
notifyListeners();
}
Future<bool> exportWithSAF() async {
try {
final datetime = DateTime.now().toUtc().toString()
.replaceAll(" ", "T")
.replaceAll(":", "-")
.replaceAll(RegExp(r"\..*"), "");
final filename = 'hlasky_$datetime.txt';
final content = filteredQuotes.map((q) => "${q.body} ${q.author}").join('\n') + '\n';
final bytes = Uint8List.fromList(utf8.encode(content));
Future<Directory> _getSaveDirectory() async {
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
final path = Platform.isWindows
? Platform.environment['USERPROFILE']
: Platform.environment['HOME'];
// Show folder picker
final folderPath = await FilePicker.platform.getDirectoryPath();
if (folderPath == null) return false; // user cancelled
exportDir = folderPath;
return Directory(path!);
final file = File('$folderPath/$filename');
await file.writeAsBytes(bytes);
return true; // export successful
} catch (e) {
return false; // something went wrong
}
// Mobile fallback
return await getApplicationDocumentsDirectory();
}
Future<void> exportQuotesToFile() async {
final dir = await _getSaveDirectory();
final datetime = DateTime.now().toUtc().toString()
.replaceAll(" ", "T")
.replaceAll(":", "-")
.replaceAll(RegExp(r"\..*"), "");
final file = File('${dir.path}/hlasky_$datetime.txt');
await file.writeAsString("${filteredQuotes.join("\n")}\n");
}
Color getLeaderboardEntryBgColor(BuildContext context, int index) {