Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../shared/relay/relay.dart';
import '../../../shared/theme/theme_provider.dart';

final collapsedSectionKeyProvider = Provider<String?>((ref) {
final pubkey = ref.watch(myPubkeyProvider);
Expand All @@ -11,32 +11,32 @@ final collapsedSectionKeyProvider = Provider<String?>((ref) {
});

final collapsedSectionsProvider =
AsyncNotifierProvider.family<CollapsedSections, Set<String>, String?>(
NotifierProvider.family<CollapsedSections, Set<String>, String?>(
CollapsedSections.new,
);

class CollapsedSections extends AsyncNotifier<Set<String>> {
class CollapsedSections extends Notifier<Set<String>> {
CollapsedSections(this.key);
final String? key;
late SharedPreferences _prefs;

// Preferences are preloaded in main(), so the first sidebar frame already
// hides saved-collapsed rows instead of mounting and then animating them.
@override
Future<Set<String>> build() async {
Set<String> build() {
final storageKey = key;
if (storageKey == null) return {};
_prefs = await SharedPreferences.getInstance();
return {...?_prefs.getStringList(storageKey)};
return {...?ref.read(savedPrefsProvider).getStringList(storageKey)};
}

Future<void> toggle(String section) async {
if (!state.hasValue) await future;
if (!ref.mounted) return;
final next = {...state.requireValue};
final next = {...state};
if (!next.remove(section)) next.add(section);
state = AsyncData(next);
state = next;
final storageKey = key;
if (storageKey != null &&
!await _prefs.setStringList(storageKey, next.toList())) {
!await ref
.read(savedPrefsProvider)
.setStringList(storageKey, next.toList())) {
throw StateError('Could not save collapsed sections');
}
}
Expand Down
2 changes: 1 addition & 1 deletion mobile/lib/features/channels/channels_page/body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ class _SliverChannelsList extends HookConsumerWidget {
final collapseProvider = collapsedSectionsProvider(
ref.watch(collapsedSectionKeyProvider),
);
final collapsed = ref.watch(collapseProvider).value ?? const <String>{};
final collapsed = ref.watch(collapseProvider);
bool sectionExpanded(String key) => !collapsed.contains(key);

Future<void> toggleSection(String key) async {
Expand Down
16 changes: 7 additions & 9 deletions mobile/test/features/channels/channels_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ void main() {
}
}
});
late SharedPreferences emptyPrefs;
setUp(() async {
SharedPreferences.setMockInitialValues({});
emptyPrefs = await SharedPreferences.getInstance();
});
Widget buildTestable({
required List<Override> overrides,
Brightness brightness = Brightness.light,
Expand All @@ -139,6 +144,7 @@ void main() {
bool realHome = false,
Widget? soloSliver,
ConversationPaneController? pane,
SharedPreferences? prefs,
}) {
final channelsPage = ChannelsPage(
settingsPageBuilder: _buildSettingsPage,
Expand All @@ -164,6 +170,7 @@ void main() {
return communityIcons[relayUrl];
}),
appLifecycleProvider.overrideWith(() => _CommunitySettingsLifecycle()),
savedPrefsProvider.overrideWithValue(prefs ?? emptyPrefs),
...overrides,
],
child: MaterialApp(
Expand Down Expand Up @@ -1148,8 +1155,6 @@ void main() {
testWidgets('Home keeps header blur after returning from search', (
tester,
) async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
tester.view.physicalSize = const Size(320, 320);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
Expand All @@ -1160,7 +1165,6 @@ void main() {
colors: [Colors.yellow, Colors.blue],
),
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
channelsProvider.overrideWith(() => _FakeNotifier(testChannels)),
],
),
Expand Down Expand Up @@ -1378,13 +1382,10 @@ void main() {
testWidgets(
'section options preserve management actions and Duo quick sort',
(tester) async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
await tester.pumpWidget(
buildTestable(
overrides: [
myPubkeyProvider.overrideWithValue('aabb'),
savedPrefsProvider.overrideWithValue(prefs),
channelsProvider.overrideWith(() => _FakeNotifier(testChannels)),
channelSectionsProvider.overrideWith(
() => _FakeChannelSectionsNotifier(
Expand Down Expand Up @@ -1485,8 +1486,6 @@ void main() {
tester.view.viewPadding = const FakeViewPadding(right: 76);
addTearDown(tester.view.reset);
await tester.pumpWidget(const SizedBox.shrink());
SharedPreferences.setMockInitialValues({});
final duoPrefs = await SharedPreferences.getInstance();
final sections = _FakeChannelSectionsNotifier(
const ChannelSectionStore(
sections: [
Expand All @@ -1499,7 +1498,6 @@ void main() {
realHome: true,
overrides: [
myPubkeyProvider.overrideWithValue('aabb'),
savedPrefsProvider.overrideWithValue(duoPrefs),
channelsProvider.overrideWith(() => _FakeNotifier(testChannels)),
channelSectionsProvider.overrideWith(() => sections),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
part of '../channels_page_test.dart';

void _filterFeedbackTests(
Widget Function({required List<Override> overrides, bool realHome})
Widget Function({
required List<Override> overrides,
bool realHome,
SharedPreferences? prefs,
})
buildTestable,
) {
for (final coverMessages in [true, false]) {
Expand Down Expand Up @@ -41,9 +45,9 @@ void _filterFeedbackTests(
const ids = ['failed', 'loaded'];
await tester.pumpWidget(
buildTestable(
prefs: prefs,
realHome: coverMessages,
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
channelsProvider.overrideWith(
() => _FakeNotifier([
for (final id in ids)
Expand Down Expand Up @@ -160,6 +164,7 @@ void _filterFeedbackTests(
final manager = _filterTestManager(onPublish: () => publications++);
await tester.pumpWidget(
buildTestable(
prefs: prefs,
overrides: [
channelsProvider.overrideWith(
() => _FakeNotifier([
Expand All @@ -186,7 +191,6 @@ void _filterFeedbackTests(
initialStatus: SessionStatus.disconnected,
),
),
savedPrefsProvider.overrideWithValue(prefs),
channelFilterProvider.overrideWithValue(manager),
],
),
Expand Down Expand Up @@ -335,6 +339,7 @@ void _filterFeedbackTests(
final relay = _FilterJourneyRelay();
await tester.pumpWidget(
buildTestable(
prefs: prefs,
overrides: [
channelsProvider.overrideWith(
() => _FakeNotifier([
Expand Down Expand Up @@ -363,7 +368,6 @@ void _filterFeedbackTests(
),
communityStorageProvider.overrideWithValue(storage),
communitySnapshotWriterProvider.overrideWithValue((_) async {}),
savedPrefsProvider.overrideWithValue(prefs),
relaySessionProvider.overrideWith(() => relay),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ void _homeCommunityFlightTests(
required List<Override> overrides,
bool realHome,
bool disableAnimations,
SharedPreferences? prefs,
})
buildTestable,
) {
Expand Down Expand Up @@ -35,10 +36,10 @@ void _homeCommunityFlightTests(
final prefs = await SharedPreferences.getInstance();
await tester.pumpWidget(
buildTestable(
prefs: prefs,
realHome: true,
disableAnimations: reduced,
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
channelsProvider.overrideWith(
() => _FakeNotifier(
[],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
part of '../channels_page_test.dart';

void _readSeedJourney(
Widget Function({required List<Override> overrides}) buildTestable,
Widget Function({required List<Override> overrides, SharedPreferences? prefs})
buildTestable,
) {
testWidgets('late unseen channels stay unread after journal reload', (
tester,
Expand All @@ -14,8 +15,8 @@ void _readSeedJourney(
Future<ProviderContainer> mount(_ReadSeedSession session) async {
await tester.pumpWidget(
buildTestable(
prefs: prefs,
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
relayConfigProvider.overrideWith(() => _ReadSeedConfig(keys.nsec)),
relaySessionProvider.overrideWith(() => session),
activeCommunityProvider.overrideWith((ref) async => null),
Expand Down Expand Up @@ -89,8 +90,8 @@ void _readSeedJourney(
store.nonemptySnapshots.clear();
await tester.pumpWidget(
buildTestable(
prefs: prefs,
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
relayConfigProvider.overrideWith(() => _ReadSeedConfig(keys.nsec)),
relaySessionProvider.overrideWith(() => session),
activeCommunityProvider.overrideWith((ref) async => null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ void _communityUnreadLandingTests(
Widget Function({
required List<Override> overrides,
_FakeProfileNotifier? profile,
SharedPreferences? prefs,
})
buildTestable,
) {
Expand Down Expand Up @@ -183,7 +184,8 @@ class _StalledProfileNotifier extends _FakeProfileNotifier {
}

void _communityCachedLandingTests(
Widget Function({required List<Override> overrides}) buildTestable,
Widget Function({required List<Override> overrides, SharedPreferences? prefs})
buildTestable,
) {
testWidgets('a cached destination lands while its relay never answers', (
tester,
Expand Down Expand Up @@ -222,8 +224,8 @@ void _communityCachedLandingTests(
final session = _StalledDestinationSession(communitiesNotifier);
await tester.pumpWidget(
buildTestable(
prefs: prefs,
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
// Remote preference state never becomes ready for the destination.
channelSectionsProvider.overrideWith(_UnreadyLandingSections.new),
channelStarsProvider.overrideWith(_LandingStars.new),
Expand Down
Loading
Loading