Line data Source code
1 : import 'package:cwtch/models/profile.dart';
2 : import 'package:cwtch/models/profileservers.dart';
3 : import 'package:cwtch/models/remoteserver.dart';
4 : import 'package:cwtch/models/servers.dart';
5 : import 'package:cwtch/widgets/remoteserverrow.dart';
6 : import 'package:flutter/material.dart';
7 : import 'package:flutter_gen/gen_l10n/app_localizations.dart';
8 : import 'package:provider/provider.dart';
9 :
10 : import '../cwtch_icons_icons.dart';
11 : import '../main.dart';
12 : import '../settings.dart';
13 :
14 : class ProfileServersView extends StatefulWidget {
15 0 : @override
16 0 : _ProfileServersView createState() => _ProfileServersView();
17 : }
18 :
19 : class _ProfileServersView extends State<ProfileServersView> {
20 0 : @override
21 : void dispose() {
22 0 : super.dispose();
23 : }
24 :
25 0 : @override
26 : Widget build(BuildContext context) {
27 0 : var knownServers = Provider.of<ProfileInfoState>(context).serverList.servers.map<String>((RemoteServerInfoState remoteServer) {
28 0 : return remoteServer.onion + ".onion";
29 0 : }).toSet();
30 0 : var importServerList = Provider.of<ServerListState>(context).servers.where((server) => !knownServers.contains(server.onion)).map<DropdownMenuItem<String>>((ServerInfoState serverInfo) {
31 0 : return DropdownMenuItem<String>(
32 0 : value: serverInfo.onion,
33 0 : child: Text(
34 0 : serverInfo.description.isNotEmpty ? serverInfo.description : serverInfo.onion,
35 : overflow: TextOverflow.ellipsis,
36 : ),
37 : );
38 0 : }).toList();
39 :
40 0 : importServerList.insert(0, DropdownMenuItem<String>(value: "", child: Text(AppLocalizations.of(context)!.importLocalServerSelectText)));
41 :
42 0 : return Scaffold(
43 0 : appBar: AppBar(
44 0 : title: Text(MediaQuery.of(context).size.width > 600 ? AppLocalizations.of(context)!.manageKnownServersLong : AppLocalizations.of(context)!.manageKnownServersShort),
45 : ),
46 0 : body: Consumer<ProfileInfoState>(
47 0 : builder: (context, profile, child) {
48 0 : ProfileServerListState servers = profile.serverList;
49 0 : final tiles = servers.servers.map(
50 0 : (RemoteServerInfoState server) {
51 0 : return ChangeNotifierProvider<RemoteServerInfoState>.value(
52 : value: server,
53 0 : builder: (context, child) => RemoteServerRow(),
54 : );
55 : },
56 : );
57 :
58 0 : final divided = ListTile.divideTiles(
59 : context: context,
60 : tiles: tiles,
61 0 : ).toList();
62 :
63 0 : final importCard = Card(
64 0 : child: ListTile(
65 0 : title: Text(AppLocalizations.of(context)!.importLocalServerLabel),
66 0 : leading: Icon(CwtchIcons.add_circle_24px, color: Provider.of<Settings>(context).current().mainTextColor),
67 0 : trailing: DropdownButton(
68 0 : onChanged: (String? importServer) {
69 0 : if (importServer!.isNotEmpty) {
70 0 : var server = Provider.of<ServerListState>(context).getServer(importServer)!;
71 0 : showImportConfirm(context, profile.onion, server.onion, server.description, server.serverBundle);
72 : }
73 : },
74 : value: "",
75 : items: importServerList,
76 : )));
77 :
78 0 : return LayoutBuilder(builder: (BuildContext context, BoxConstraints viewportConstraints) {
79 0 : ScrollController controller = ScrollController();
80 0 : return Scrollbar(
81 : trackVisibility: true,
82 : controller: controller,
83 0 : child: SingleChildScrollView(
84 : clipBehavior: Clip.antiAlias,
85 : controller: controller,
86 0 : child: Container(
87 0 : margin: EdgeInsets.fromLTRB(5, 0, 5, 10),
88 0 : padding: EdgeInsets.fromLTRB(5, 0, 5, 10),
89 0 : child: Column(children: [if (importServerList.length > 1) importCard, Column(children: divided)]))));
90 : });
91 :
92 : return ListView(children: divided);
93 : },
94 : ));
95 : }
96 :
97 0 : showImportConfirm(BuildContext context, String profileHandle, String serverHandle, String serverDesc, String bundle) {
98 0 : var serverLabel = serverDesc.isNotEmpty ? serverDesc : serverHandle;
99 0 : serverHandle = serverHandle.substring(0, serverHandle.length - 6); // remove '.onion'
100 : // set up the buttons
101 0 : Widget cancelButton = ElevatedButton(
102 0 : child: Text(AppLocalizations.of(context)!.cancel),
103 0 : onPressed: () {
104 0 : Navigator.of(context).pop(); // dismiss dialog
105 : },
106 : );
107 0 : Widget continueButton = ElevatedButton(
108 0 : child: Text(AppLocalizations.of(context)!.importLocalServerButton.replaceAll("%1", serverLabel)),
109 0 : onPressed: () {
110 0 : Provider.of<FlwtchState>(context, listen: false).cwtch.ImportBundle(profileHandle, bundle);
111 : // Wait 500ms and hope the server is imported and add it's description in the UI and as an attribute
112 0 : Future.delayed(const Duration(milliseconds: 500), () {
113 0 : var profile = Provider.of<ProfileInfoState>(context);
114 0 : if (profile.serverList.getServer(serverHandle) != null) {
115 0 : profile.serverList.getServer(serverHandle)?.updateDescription(serverDesc);
116 :
117 0 : Provider.of<FlwtchState>(context, listen: false).cwtch.SetConversationAttribute(profile.onion, profile.serverList.getServer(serverHandle)!.identifier, "server.description", serverDesc);
118 : }
119 : });
120 0 : Navigator.of(context).pop();
121 : });
122 :
123 : // set up the AlertDialog
124 0 : AlertDialog alert = AlertDialog(
125 0 : title: Text(AppLocalizations.of(context)!.importLocalServerButton.replaceAll("%1", serverLabel)),
126 0 : actions: [
127 : cancelButton,
128 : continueButton,
129 : ],
130 : );
131 :
132 : // show the dialog
133 0 : showDialog(
134 : context: context,
135 0 : builder: (BuildContext context) {
136 : return alert;
137 : },
138 : );
139 : }
140 : }
|