|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:iccm_eu_app/data/dataProviders/gsheets_provider.dart'; |
| 5 | +import 'package:iccm_eu_app/data/model/communication_data.dart'; |
| 6 | +import 'package:iccm_eu_app/utils/debug.dart'; |
| 7 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 8 | + |
| 9 | +class CommunicationProvider with ChangeNotifier { |
| 10 | + static String get worksheetTitle => "Communication"; |
| 11 | + |
| 12 | + String get _cacheTitle => "_communicationDataCache"; |
| 13 | + final GsheetsProvider _gsheetsProvider; |
| 14 | + |
| 15 | + final List<CommunicationData> _cache = []; |
| 16 | + |
| 17 | + final List<CommunicationData> _items = []; |
| 18 | + |
| 19 | + List<CommunicationData> items() { |
| 20 | + return _items; |
| 21 | + } |
| 22 | + |
| 23 | + CommunicationProvider({ |
| 24 | + required GsheetsProvider gsheetsProvider, |
| 25 | + }) : _gsheetsProvider = gsheetsProvider { |
| 26 | + _gsheetsProvider.addListener(updateCache); |
| 27 | + _loadCache(); |
| 28 | + _populateItemsFromCache(); |
| 29 | + } |
| 30 | + |
| 31 | + void updateCache() { |
| 32 | + // Process raw data from GsheetsProvider and update _tracks |
| 33 | + var data = _gsheetsProvider.getHomeData(); |
| 34 | + if (data != null && data.isNotEmpty) { |
| 35 | + _cacheClear(); |
| 36 | + for (final itemData in data) { |
| 37 | + _cacheAdd(CommunicationData.fromItemData(itemData)); |
| 38 | + } |
| 39 | + _commit(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + Future<void> _loadCache() async { |
| 44 | + final prefs = await SharedPreferences.getInstance(); |
| 45 | + final cacheJson = prefs.getString(_cacheTitle); |
| 46 | + if (cacheJson != null && cacheJson.isNotEmpty) { |
| 47 | + _cacheClear(); |
| 48 | + final List<dynamic> jsonList = jsonDecode(cacheJson); |
| 49 | + jsonList.map((json) => CommunicationData.fromJson(json)).toList().forEach((item) { |
| 50 | + _cacheAdd(item); |
| 51 | + }); |
| 52 | + Debug.msg('Cache loaded: Home'); |
| 53 | + _commit(); |
| 54 | + } else { |
| 55 | + Debug.msg('Cache OMITTED: Home'); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + void _cacheAdd(CommunicationData item) { |
| 60 | + _cache.add(item); |
| 61 | + } |
| 62 | + |
| 63 | + void _cacheClear() { |
| 64 | + _cache.clear(); |
| 65 | + } |
| 66 | + |
| 67 | + void _commit() { |
| 68 | + _cache.sort((a, b) => a.title.compareTo(b.title)); |
| 69 | + _saveCache(); |
| 70 | + _populateItemsFromCache(); |
| 71 | + notifyListeners(); |
| 72 | + } |
| 73 | + |
| 74 | + void _populateItemsFromCache() { |
| 75 | + if (_cache.isNotEmpty) { |
| 76 | + _items.clear(); |
| 77 | + for (var item in _cache) { |
| 78 | + _items.add(item); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + Future<void> _saveCache() async { |
| 84 | + final prefs = await SharedPreferences.getInstance(); |
| 85 | + final cacheJson = jsonEncode(_cache); // Convert _cache to JSON string |
| 86 | + await prefs.setString(_cacheTitle, cacheJson); // Save to SharedPreferences |
| 87 | + } |
| 88 | +} |
0 commit comments