chore: fix analytics CSV download (#3489)

This commit is contained in:
ggurdin 2025-07-16 15:17:28 -04:00 committed by GitHub
parent c948aeef7b
commit 77040024d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:csv/csv.dart';
import 'package:excel/excel.dart';
import 'package:intl/intl.dart';
import 'package:matrix/matrix.dart';
import 'package:fluffychat/config/app_config.dart';
@ -81,19 +83,52 @@ class AnalyticsDownloadDialogState extends State<AnalyticsDownloadDialog> {
}
try {
final content = _getExcelFileContent({
ConstructTypeEnum.vocab: vocabSummary,
ConstructTypeEnum.morph: morphSummary,
});
if (_downloadType == DownloadType.csv) {
final vocabContent = _getCSVFileContent(
ConstructTypeEnum.vocab,
vocabSummary,
);
final fileName =
"analytics_${MatrixState.pangeaController.matrixState.client.userID?.localpart}_${DateTime.now().toIso8601String()}.${_downloadType == DownloadType.xlsx ? 'xlsx' : 'csv'}";
final morphContent = _getCSVFileContent(
ConstructTypeEnum.morph,
morphSummary,
);
await downloadFile(
content,
fileName,
_downloadType,
);
final vocabFileName =
"analytics_vocab_${MatrixState.pangeaController.matrixState.client.userID?.localpart}_${DateFormat('yyyy-MM-dd-hh:mm:ss').format(DateTime.now())}.csv";
final morphFileName =
"analytics_morph_${MatrixState.pangeaController.matrixState.client.userID?.localpart}_${DateFormat('yyyy-MM-dd-hh:mm:ss').format(DateTime.now())}.csv";
final futures = [
downloadFile(
vocabContent,
vocabFileName,
_downloadType,
),
downloadFile(
morphContent,
morphFileName,
_downloadType,
),
];
await Future.wait(futures);
} else {
final content = _getExcelFileContent({
ConstructTypeEnum.vocab: vocabSummary,
ConstructTypeEnum.morph: morphSummary,
});
final fileName =
"analytics_${MatrixState.pangeaController.matrixState.client.userID?.localpart}_${DateFormat('yyyy-MM-dd-hh:mm:ss').format(DateTime.now())}.xlsx'}";
await downloadFile(
content,
fileName,
_downloadType,
);
}
_downloaded = true;
} catch (e, s) {
ErrorHandler.logError(
@ -312,6 +347,37 @@ class AnalyticsDownloadDialogState extends State<AnalyticsDownloadDialog> {
return excel.encode() ?? [];
}
String _getCSVFileContent(
ConstructTypeEnum type,
List<AnalyticsSummaryModel> summaries,
) {
final values = type == ConstructTypeEnum.vocab
? AnalyticsSummaryEnum.vocabValues
: AnalyticsSummaryEnum.morphValues;
final List<List<dynamic>> rows = [];
final headerRow = [];
for (final key in values) {
headerRow.add(key.header(context));
}
rows.add(headerRow);
for (final summary in summaries) {
final List<dynamic> row = [];
for (int i = 0; i < values.length; i++) {
final key = values[i];
final value = summary.getValue(key);
row.add(
value is List<String> ? value.map((v) => "\"$v\"").join(", ") : value,
);
}
rows.add(row);
}
final String fileString = const ListToCsvConverter().convert(rows);
return fileString;
}
List<CellValue> _formatExcelRow(
AnalyticsSummaryModel summary,
ConstructTypeEnum type,