import 'dart:convert'; import 'package:cwtch/main.dart'; import 'package:cwtch/models/contact.dart'; import 'package:cwtch/models/profile.dart'; import 'package:cwtch/settings.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:scrollable_positioned_list/scrollable_positioned_list.dart'; import '../cwtch_icons_icons.dart'; class FileSharingView extends StatefulWidget { @override _FileSharingViewState createState() => _FileSharingViewState(); } class _FileSharingViewState extends State { @override Widget build(BuildContext context) { var handle = Provider.of(context).nickname; if (handle.isEmpty) { handle = Provider.of(context).onion; } var profileHandle = Provider.of(context).onion; return Scaffold( appBar: AppBar( title: Text(handle + " ยป " + AppLocalizations.of(context)!.manageSharedFiles), ), body: FutureBuilder( future: Provider.of(context, listen: false).cwtch.GetSharedFiles(profileHandle, Provider.of(context).identifier), builder: (context, snapshot) { if (snapshot.hasData) { List sharedFiles = jsonDecode(snapshot.data as String) ?? List.empty(); sharedFiles.sort((a, b) { return a["DateShared"].toString().compareTo(b["DateShared"].toString()); }); var fileList = ScrollablePositionedList.separated( itemScrollController: ItemScrollController(), itemCount: sharedFiles.length, shrinkWrap: true, reverse: true, physics: BouncingScrollPhysics(), semanticChildCount: sharedFiles.length, itemBuilder: (context, index) { String filekey = sharedFiles[index]["FileKey"]; // This makes the UI *very* slow when enabled. But can be useful for debugging // Uncomment if necessary. // EnvironmentConfig.debugLog("$sharedFiles " + sharedFiles[index].toString()); return SwitchListTile( title: Text(sharedFiles[index]["Path"]), subtitle: Text(sharedFiles[index]["DateShared"]), value: sharedFiles[index]["Active"], activeTrackColor: Provider.of(context).theme.defaultButtonColor, inactiveTrackColor: Provider.of(context).theme.defaultButtonDisabledColor, secondary: Icon(CwtchIcons.attached_file_3, color: Provider.of(context).current().mainTextColor), onChanged: (newValue) { setState(() { if (newValue) { Provider.of(context, listen: false).cwtch.RestartSharing(profileHandle, filekey); } else { Provider.of(context, listen: false).cwtch.StopSharing(profileHandle, filekey); } }); }); }, separatorBuilder: (BuildContext context, int index) { return Divider(height: 1); }, ); return fileList; } return Container(); }, ), ); } }