Merge pull request #1263 from OffbandMesh/feat/1255-region-identity
feat(#1255): region scope keyed by channel identity + scope bannerpull/1279/head
commit
a55057b7b6
@ -0,0 +1,15 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// Tracks which region-scoped channels have had their banner dismissed, for the
|
||||
/// current app session only. Keyed by channel PSK hex, held in memory, so a
|
||||
/// dismissal lasts until the app is closed and the banner returns on a fresh
|
||||
/// open (#1258). Not persisted.
|
||||
class RegionScopeBannerService extends ChangeNotifier {
|
||||
final Set<String> _dismissed = {};
|
||||
|
||||
bool isDismissed(String channelPskHex) => _dismissed.contains(channelPskHex);
|
||||
|
||||
void dismiss(String channelPskHex) {
|
||||
if (_dismissed.add(channelPskHex)) notifyListeners();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../l10n/l10n.dart';
|
||||
|
||||
/// A banner at the top of a region-scoped channel chat reminding the user that
|
||||
/// the channel's messages ride a region. Tapping it opens Set Region Scope so
|
||||
/// the user can change or clear the scope; the close button dismisses it for the
|
||||
/// session. Renders nothing when [regionName] is null (unscoped or dismissed).
|
||||
/// (#1258)
|
||||
class RegionScopeBanner extends StatelessWidget {
|
||||
const RegionScopeBanner({
|
||||
super.key,
|
||||
required this.regionName,
|
||||
required this.onTap,
|
||||
required this.onDismiss,
|
||||
});
|
||||
|
||||
final String? regionName;
|
||||
final VoidCallback onTap;
|
||||
final VoidCallback onDismiss;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final name = regionName;
|
||||
if (name == null) return const SizedBox.shrink();
|
||||
final l10n = context.l10n;
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return Material(
|
||||
color: scheme.secondaryContainer,
|
||||
child: InkWell(
|
||||
key: const Key('regionScopeBannerBody'),
|
||||
onTap: onTap,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 8, 8),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.travel_explore,
|
||||
size: 20,
|
||||
color: scheme.onSecondaryContainer,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.setRegionScope_banner(name),
|
||||
style: TextStyle(color: scheme.onSecondaryContainer),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
key: const Key('regionScopeBannerDismiss'),
|
||||
icon: const Icon(Icons.close, size: 20),
|
||||
color: scheme.onSecondaryContainer,
|
||||
tooltip: l10n.setRegionScope_bannerDismiss,
|
||||
onPressed: onDismiss,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
// Per-session dismissal of the region-scope banner, keyed by channel PSK (#1258).
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/services/region_scope_banner_service.dart';
|
||||
|
||||
void main() {
|
||||
test('a channel banner is not dismissed until dismissed', () {
|
||||
final s = RegionScopeBannerService();
|
||||
expect(s.isDismissed('pska'), isFalse);
|
||||
s.dismiss('pska');
|
||||
expect(s.isDismissed('pska'), isTrue);
|
||||
expect(s.isDismissed('pskb'), isFalse); // other channels unaffected
|
||||
});
|
||||
|
||||
test('dismiss notifies listeners so the banner can hide', () {
|
||||
final s = RegionScopeBannerService();
|
||||
var notified = 0;
|
||||
s.addListener(() => notified++);
|
||||
s.dismiss('pska');
|
||||
expect(notified, 1);
|
||||
});
|
||||
}
|
||||
@ -0,0 +1,84 @@
|
||||
// Cyr2Lat per-channel settings, keyed by channel PSK identity (#1257).
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/storage/channel_settings_store.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
const _devA = 'aabbccddee0011223344';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
});
|
||||
|
||||
ChannelSettingsStore storeFor(String deviceKeyHex) {
|
||||
final s = ChannelSettingsStore();
|
||||
s.setPublicKeyHex = deviceKeyHex;
|
||||
return s;
|
||||
}
|
||||
|
||||
test(
|
||||
'enabled + profile round-trip without a resolver (index fallback)',
|
||||
() async {
|
||||
final store = storeFor(_devA);
|
||||
expect(await store.loadCyr2LatEnabled(0), isFalse);
|
||||
await store.saveCyr2LatEnabled(0, true);
|
||||
expect(await store.loadCyr2LatEnabled(0), isTrue);
|
||||
await store.saveCyr2LatProfileId(0, 'ru');
|
||||
expect(await store.loadCyr2LatProfileId(0), 'ru');
|
||||
},
|
||||
);
|
||||
|
||||
group('PSK identity keying (#1257)', () {
|
||||
test('enabled follows the channel PSK across a slot reshuffle', () async {
|
||||
final store = storeFor(_devA);
|
||||
final slots = <int, String?>{0: 'pskaaaa', 1: 'pskbbbb'};
|
||||
store.channelPskResolver = (i) => slots[i];
|
||||
await store.saveCyr2LatEnabled(0, true); // channel pskaaaa at slot 0
|
||||
|
||||
slots[0] = 'pskbbbb';
|
||||
slots[1] = 'pskaaaa';
|
||||
|
||||
expect(await store.loadCyr2LatEnabled(1), isTrue); // followed pskaaaa
|
||||
expect(await store.loadCyr2LatEnabled(0), isFalse); // pskbbbb unset
|
||||
});
|
||||
|
||||
test(
|
||||
'profile id follows the channel PSK across a slot reshuffle',
|
||||
() async {
|
||||
final store = storeFor(_devA);
|
||||
final slots = <int, String?>{0: 'pskaaaa', 1: 'pskbbbb'};
|
||||
store.channelPskResolver = (i) => slots[i];
|
||||
await store.saveCyr2LatProfileId(0, 'ru');
|
||||
|
||||
slots[0] = 'pskbbbb';
|
||||
slots[1] = 'pskaaaa';
|
||||
|
||||
expect(await store.loadCyr2LatProfileId(1), 'ru');
|
||||
expect(await store.loadCyr2LatProfileId(0), isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'adopts a legacy index-keyed enabled flag once the PSK is known',
|
||||
() async {
|
||||
await storeFor(
|
||||
_devA,
|
||||
).saveCyr2LatEnabled(2, true); // index-keyed (no resolver)
|
||||
|
||||
final store = storeFor(_devA);
|
||||
store.channelPskResolver = (i) => i == 2 ? 'pskcccc' : null;
|
||||
expect(await store.loadCyr2LatEnabled(2), isTrue); // migrated
|
||||
|
||||
store.channelPskResolver = (i) => i == 5 ? 'pskcccc' : null;
|
||||
expect(await store.loadCyr2LatEnabled(5), isTrue); // sticky by PSK
|
||||
expect(await store.loadCyr2LatEnabled(2), isFalse); // index key gone
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
// Region-scope banner on the channel chat (#1258).
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/widgets/region_scope_banner.dart';
|
||||
|
||||
void main() {
|
||||
Future<void> pump(
|
||||
WidgetTester tester, {
|
||||
required String? regionName,
|
||||
VoidCallback? onTap,
|
||||
VoidCallback? onDismiss,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: Scaffold(
|
||||
body: RegionScopeBanner(
|
||||
regionName: regionName,
|
||||
onTap: onTap ?? () {},
|
||||
onDismiss: onDismiss ?? () {},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
testWidgets('shows the scoped region name', (tester) async {
|
||||
await pump(tester, regionName: 'cvg');
|
||||
expect(find.textContaining('cvg'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('tapping the banner opens Set Region Scope (onTap)', (
|
||||
tester,
|
||||
) async {
|
||||
var taps = 0;
|
||||
await pump(tester, regionName: 'cvg', onTap: () => taps++);
|
||||
await tester.tap(find.byKey(const Key('regionScopeBannerBody')));
|
||||
expect(taps, 1);
|
||||
});
|
||||
|
||||
testWidgets('tapping dismiss fires onDismiss', (tester) async {
|
||||
var dismissed = 0;
|
||||
await pump(tester, regionName: 'cvg', onDismiss: () => dismissed++);
|
||||
await tester.tap(find.byKey(const Key('regionScopeBannerDismiss')));
|
||||
expect(dismissed, 1);
|
||||
});
|
||||
|
||||
testWidgets('renders nothing when the channel is not scoped', (tester) async {
|
||||
await pump(tester, regionName: null);
|
||||
expect(find.byKey(const Key('regionScopeBannerBody')), findsNothing);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue