payfrit-app/lib/services/beacon_cache.dart
John Mizerek c4792189dd App Store Version 2: Beacon scanning, preload caching, business selector
Features:
- Beacon scanner service for detecting nearby beacons
- Beacon cache for offline-first beacon resolution
- Preload cache for instant menu display
- Business selector screen for multi-location support
- Rescan button widget for quick beacon refresh
- Sign-in dialog for guest checkout flow
- Task type model for server tasks

Improvements:
- Enhanced menu browsing with category filtering
- Improved cart view with better modifier display
- Order history with detailed order tracking
- Chat screen improvements
- Better error handling in API service

Fixes:
- CashApp payment return crash fix
- Modifier nesting issues resolved
- Auto-expand modifier groups

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 19:51:54 -08:00

47 lines
1.5 KiB
Dart

import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class BeaconCache {
static const _keyBeaconData = 'beacon_cache_data';
static const _keyBeaconTimestamp = 'beacon_cache_timestamp';
static const _cacheDuration = Duration(hours: 24); // Cache for 24 hours
/// Save beacon list to cache
static Future<void> save(Map<String, int> beacons) async {
final prefs = await SharedPreferences.getInstance();
final json = jsonEncode(beacons);
await prefs.setString(_keyBeaconData, json);
await prefs.setInt(_keyBeaconTimestamp, DateTime.now().millisecondsSinceEpoch);
}
/// Load beacon list from cache (returns null if expired or not found)
static Future<Map<String, int>?> load() async {
final prefs = await SharedPreferences.getInstance();
final timestamp = prefs.getInt(_keyBeaconTimestamp);
final data = prefs.getString(_keyBeaconData);
if (timestamp == null || data == null) {
return null;
}
// Check if cache is expired
final cachedTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
if (DateTime.now().difference(cachedTime) > _cacheDuration) {
return null;
}
try {
final decoded = jsonDecode(data) as Map<String, dynamic>;
return decoded.map((k, v) => MapEntry(k, v as int));
} catch (e) {
return null;
}
}
/// Clear the beacon cache
static Future<void> clear() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_keyBeaconData);
await prefs.remove(_keyBeaconTimestamp);
}
}