99 lines
3.1 KiB
Dart
99 lines
3.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:manager_app/Components/collection_editor.dart';
|
|
import 'package:manager_app/Components/multi_string_input_and_resource_container.dart';
|
|
import 'package:manager_app/Models/managerContext.dart';
|
|
import 'package:manager_app/app_context.dart';
|
|
import 'package:manager_app/l10n/app_localizations.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class PDFConfig extends StatefulWidget {
|
|
final String? color;
|
|
final String? label;
|
|
final PdfDTO initialValue;
|
|
final ValueChanged<PdfDTO> onChanged;
|
|
const PDFConfig({
|
|
Key? key,
|
|
this.color,
|
|
this.label,
|
|
required this.initialValue,
|
|
required this.onChanged,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
_PDFConfigState createState() => _PDFConfigState();
|
|
}
|
|
|
|
class _PDFConfigState extends State<PDFConfig> {
|
|
late PdfDTO pdfDTO;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
pdfDTO = widget.initialValue;
|
|
pdfDTO.pdfs = List<OrderedTranslationAndResourceDTO>.from(pdfDTO.pdfs ?? []);
|
|
pdfDTO.pdfs!.sort((a, b) => (a.order ?? 0).compareTo(b.order ?? 0));
|
|
}
|
|
|
|
void _persist() {
|
|
var i = 0;
|
|
for (final pdf in pdfDTO.pdfs!) {
|
|
pdf.order = i;
|
|
i++;
|
|
}
|
|
widget.onChanged(pdfDTO);
|
|
}
|
|
|
|
OrderedTranslationAndResourceDTO _emptyFile(List<String> languages) {
|
|
final file = OrderedTranslationAndResourceDTO();
|
|
file.translationAndResourceDTOs = <TranslationAndResourceDTO>[];
|
|
for (final language in languages) {
|
|
file.translationAndResourceDTOs!.add(
|
|
TranslationAndResourceDTO(language: language, value: "", resourceId: null),
|
|
);
|
|
}
|
|
return file;
|
|
}
|
|
|
|
String _fileLabel(AppLocalizations l, OrderedTranslationAndResourceDTO file, int index) {
|
|
final translations = file.translationAndResourceDTOs ?? [];
|
|
if (translations.isEmpty) return l.untitledFile(index + 1);
|
|
final value = translations
|
|
.firstWhere((t) => t.language == 'FR', orElse: () => translations.first)
|
|
.value ??
|
|
"";
|
|
final plain = value.replaceAll(RegExp(r'<[^>]*>'), ' ').trim();
|
|
return plain.isEmpty ? l.untitledFile(index + 1) : plain;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l = AppLocalizations.of(context)!;
|
|
final languages = (Provider.of<AppContext>(context).getContext()
|
|
as ManagerAppContext)
|
|
.selectedConfiguration!
|
|
.languages!;
|
|
|
|
return CollectionEditor<OrderedTranslationAndResourceDTO>(
|
|
items: pdfDTO.pdfs!,
|
|
addLabel: l.createPdfTitle,
|
|
itemLabel: (file, index) => _fileLabel(l, file, index),
|
|
createItem: () => _emptyFile(languages),
|
|
onChanged: (_) => _persist(),
|
|
detailBuilder: (file, index) => MultiStringInputAndResourceContainer(
|
|
key: ValueKey(identityHashCode(file)),
|
|
label: l.pdfFileAndTitleLabel,
|
|
modalLabel: l.pdfFileAndTitleModalLabel,
|
|
initialValue: file.translationAndResourceDTOs ?? [],
|
|
resourceTypes: [ResourceType.Pdf],
|
|
onGetResult: (value) {
|
|
file.translationAndResourceDTOs = value;
|
|
_persist();
|
|
},
|
|
maxLines: 1,
|
|
isTitle: true,
|
|
),
|
|
);
|
|
}
|
|
}
|