Skip to content

Commit 3169b44

Browse files
committed
Fix cache settings not editable correctly
1 parent 26dccfc commit 3169b44

File tree

3 files changed

+76
-61
lines changed

3 files changed

+76
-61
lines changed

app/lib/cubits/settings.dart

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -830,12 +830,13 @@ class SettingsCubit extends Cubit<ButterflySettings>
830830
connections: List<ExternalStorage>.from(state.connections).map((e) {
831831
if (e.identifier == identifier && e is RemoteStorage) {
832832
final documents = List<String>.from(e.cachedDocuments[''] ?? []);
833-
return (e as dynamic).copyWith(
834-
cachedDocuments: documents
835-
..removeWhere((element) => element == current)
836-
..add(current),
837-
)
838-
as ExternalStorage;
833+
return e.copyWith(
834+
cachedDocuments: {
835+
'': documents
836+
..removeWhere((element) => element == current)
837+
..add(current),
838+
},
839+
);
839840
}
840841
return e;
841842
}).toList(),
@@ -849,12 +850,12 @@ class SettingsCubit extends Cubit<ButterflySettings>
849850
state.copyWith(
850851
connections: List<ExternalStorage>.from(state.connections).map((e) {
851852
if (e.identifier == identifier && e is RemoteStorage) {
852-
return (e as dynamic).copyWith(
853-
cachedDocuments: List<String>.from(
854-
e.cachedDocuments[''] ?? [],
855-
)..remove(current),
856-
)
857-
as ExternalStorage;
853+
final documents = List<String>.from(e.cachedDocuments[''] ?? []);
854+
return e.copyWith(
855+
cachedDocuments: {
856+
'': documents..removeWhere((element) => element == current),
857+
},
858+
);
858859
}
859860
return e;
860861
}).toList(),
@@ -878,11 +879,11 @@ class SettingsCubit extends Cubit<ButterflySettings>
878879
return save();
879880
}
880881

881-
Future<void> clearCaches(ExternalStorage storage) {
882+
Future<void> clearCaches(String identifier) {
882883
emit(
883884
state.copyWith(
884885
connections: List<ExternalStorage>.from(state.connections).map((e) {
885-
if (e.identifier == storage.identifier && e is RemoteStorage) {
886+
if (e.identifier == identifier && e is RemoteStorage) {
886887
return (e as dynamic).copyWith(cachedDocuments: [])
887888
as ExternalStorage;
888889
}

app/lib/settings/connection.dart

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ class ConnectionSettingsPage extends StatefulWidget {
1818

1919
class _ConnectionSettingsPageState extends State<ConnectionSettingsPage>
2020
with TickerProviderStateMixin {
21-
late TabController _tabController;
22-
ExternalStorage? storage;
21+
bool? _isRemote;
22+
late final TabController _tabController;
2323

2424
@override
2525
void initState() {
2626
super.initState();
27-
storage = context.read<SettingsCubit>().getRemote(widget.remote);
28-
_tabController = TabController(length: _isRemote ? 2 : 1, vsync: this);
27+
_isRemote =
28+
context.read<SettingsCubit>().getRemote(widget.remote) is RemoteStorage;
29+
_tabController = TabController(
30+
length: (_isRemote ?? false) ? 2 : 1,
31+
vsync: this,
32+
);
2933
}
3034

31-
bool get _isRemote => storage is RemoteStorage;
32-
3335
@override
3436
void dispose() {
3537
_tabController.dispose();
@@ -49,7 +51,7 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage>
4951
return Scaffold(
5052
appBar: WindowTitleBar<SettingsCubit, ButterflySettings>(
5153
title: Text(widget.remote),
52-
bottom: _isRemote
54+
bottom: (_isRemote ?? false)
5355
? TabBar(
5456
controller: _tabController,
5557
onTap: (_) => setState(() {}),
@@ -67,16 +69,17 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage>
6769
)
6870
: null,
6971
),
70-
body: storage == null
72+
body: _isRemote == null
7173
? Center(child: Text(AppLocalizations.of(context).noConnections))
7274
: TabBarView(
7375
controller: _tabController,
7476
children: [
75-
_GeneralConnectionSettingsView(storage: storage!),
76-
if (_isRemote)
77-
_CachesConnectionSettingsView(
78-
storage: storage as RemoteStorage,
79-
),
77+
_GeneralConnectionSettingsView(
78+
identifier: widget.remote,
79+
isRemote: _isRemote ?? false,
80+
),
81+
if (_isRemote ?? false)
82+
_CachesConnectionSettingsView(identifier: widget.remote),
8083
],
8184
),
8285
floatingActionButton: _createFab()[_tabController.index],
@@ -120,8 +123,13 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage>
120123
}
121124

122125
class _GeneralConnectionSettingsView extends StatelessWidget {
123-
final ExternalStorage storage;
124-
const _GeneralConnectionSettingsView({required this.storage});
126+
final String identifier;
127+
final bool isRemote;
128+
129+
const _GeneralConnectionSettingsView({
130+
required this.identifier,
131+
required this.isRemote,
132+
});
125133

126134
@override
127135
Widget build(BuildContext context) {
@@ -143,27 +151,26 @@ class _GeneralConnectionSettingsView extends StatelessWidget {
143151
style: TextTheme.of(context).headlineSmall,
144152
),
145153
const SizedBox(height: 16),
146-
if (storage is RemoteStorage) ...[
154+
if (isRemote) ...[
147155
BlocBuilder<SettingsCubit, ButterflySettings>(
148156
builder: (context, state) {
149157
final storage =
150-
state.getRemote(this.storage.identifier)
151-
as RemoteStorage?;
158+
state.getRemote(identifier) as RemoteStorage?;
159+
final isRootCached =
160+
storage?.cachedDocuments['']?.contains('/') ??
161+
false;
152162
return CheckboxListTile(
153-
value:
154-
storage?.cachedDocuments['']?.contains('/') ??
155-
false,
163+
value: isRootCached,
156164
onChanged: (value) {
157165
if (storage == null) return;
158-
if (storage.cachedDocuments['']?.contains('/') ??
159-
false) {
166+
if (isRootCached) {
160167
context.read<SettingsCubit>().removeCache(
161-
storage.identifier,
168+
identifier,
162169
'/',
163170
);
164171
} else {
165172
context.read<SettingsCubit>().addCache(
166-
storage.identifier,
173+
identifier,
167174
'/',
168175
);
169176
}
@@ -181,17 +188,15 @@ class _GeneralConnectionSettingsView extends StatelessWidget {
181188
title: Text(AppLocalizations.of(context).clearCaches),
182189
leading: const PhosphorIcon(PhosphorIconsLight.fileX),
183190
onTap: () {
184-
context.read<SettingsCubit>().clearCaches(storage);
191+
context.read<SettingsCubit>().clearCaches(identifier);
185192
},
186193
),
187194
],
188195
ListTile(
189196
title: Text(AppLocalizations.of(context).delete),
190197
leading: const PhosphorIcon(PhosphorIconsLight.trash),
191198
onTap: () {
192-
context.read<SettingsCubit>().deleteRemote(
193-
storage.identifier,
194-
);
199+
context.read<SettingsCubit>().deleteRemote(identifier);
195200
GoRouter.of(context).pop();
196201
},
197202
),
@@ -207,35 +212,43 @@ class _GeneralConnectionSettingsView extends StatelessWidget {
207212
}
208213

209214
class _CachesConnectionSettingsView extends StatelessWidget {
210-
final RemoteStorage storage;
211-
const _CachesConnectionSettingsView({required this.storage});
215+
final String identifier;
216+
217+
const _CachesConnectionSettingsView({required this.identifier});
212218

213219
@override
214220
Widget build(BuildContext context) {
215221
return BlocBuilder<SettingsCubit, ButterflySettings>(
216222
builder: (context, state) {
223+
final storage = state.getRemote(identifier);
224+
if (storage == null || storage is! RemoteStorage) {
225+
return Center(child: Text(AppLocalizations.of(context).noElements));
226+
}
227+
final cached = storage.cachedDocuments[''] ?? <String>[];
217228
return Align(
218229
alignment: Alignment.center,
219230
child: ConstrainedBox(
220231
constraints: const BoxConstraints(
221232
maxWidth: LeapBreakpoints.compact,
222233
),
223-
child: ListView.builder(
224-
itemCount: storage.cachedDocuments['']?.length,
225-
itemBuilder: (context, index) {
226-
final current = storage.cachedDocuments['']![index];
227-
return Dismissible(
228-
key: Key(current),
229-
onDismissed: (_) {
230-
context.read<SettingsCubit>().removeCache(
231-
storage.identifier,
232-
current,
233-
);
234-
},
235-
child: ListTile(title: Text(current)),
236-
);
237-
},
238-
),
234+
child: cached.isEmpty
235+
? Text(AppLocalizations.of(context).noElements)
236+
: ListView.builder(
237+
itemCount: cached.length,
238+
itemBuilder: (context, index) {
239+
final current = cached[index];
240+
return Dismissible(
241+
key: Key(current),
242+
onDismissed: (_) {
243+
context.read<SettingsCubit>().removeCache(
244+
storage.identifier,
245+
current,
246+
);
247+
},
248+
child: ListTile(title: Text(current)),
249+
);
250+
},
251+
),
239252
),
240253
);
241254
},

metadata/en-US/changelogs/152.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Fix header text color on certain themes
77
* Fix theme names not displaying correctly in personalization settings
88
* Fix metadata file version not updating if file version is null
9+
* Fix cache settings not editable correctly
910
* Add 16kb page size support for android
1011

1112
Read more here: https://linwood.dev/butterfly/2.4.1-rc.0

0 commit comments

Comments
 (0)