generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: country selection in remittance flow (#157)
- Loading branch information
Showing
11 changed files
with
94 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,54 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:didpay/config/config.dart'; | ||
import 'package:didpay/features/remittance/countries.dart'; | ||
import 'package:didpay/features/storage/storage_service.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
final countriesProvider = | ||
AsyncNotifierProvider<CountriesAsyncNotifier, List<Country>>( | ||
CountriesAsyncNotifier.new, | ||
StateNotifierProvider<CountriesNotifier, List<Country>>( | ||
(ref) => throw UnimplementedError(), | ||
); | ||
|
||
class CountriesAsyncNotifier extends AsyncNotifier<List<Country>> { | ||
final _cacheKey = 'didpay:countries_cache'; | ||
class CountriesNotifier extends StateNotifier<List<Country>> { | ||
static const String prefsKey = 'countries'; | ||
final SharedPreferences prefs; | ||
|
||
@override | ||
FutureOr<List<Country>> build() => _loadFromCache(); | ||
CountriesNotifier(this.prefs, super.state); | ||
|
||
void addCountry(Country newCountry) { | ||
final currentCountries = state.value ?? []; | ||
final updatedCountries = [...currentCountries, newCountry]; | ||
state = AsyncData(updatedCountries); | ||
} | ||
|
||
Future<void> reload() async { | ||
final countries = await _loadFromCache(); | ||
// Show loading indicator if cache is empty | ||
state = countries.isEmpty ? const AsyncLoading() : AsyncData(countries); | ||
Future<Country> add(String code, String name) async { | ||
final country = Country(code: code, name: name); | ||
|
||
final response = await http.get(Uri.parse(Config.pfisJsonUrl)); | ||
if (response.statusCode != 200) { | ||
state = AsyncError('Failed to load countries', StackTrace.current); | ||
return; | ||
} | ||
state = [...state, country]; | ||
await _save(); | ||
return country; | ||
} | ||
|
||
await ref | ||
.read(sharedPreferencesProvider) | ||
.setString(_cacheKey, response.body); | ||
Future<void> remove(Country country) async { | ||
state = state.where((elem) => elem.code != country.code).toList(); | ||
await _save(); | ||
} | ||
|
||
state = AsyncData( | ||
List<Country>.from( | ||
(json.decode(response.body) as List) | ||
.map((item) => Country.fromJson(item as Map<String, dynamic>)), | ||
), | ||
); | ||
Future<void> _save() async { | ||
final toSave = state.map((e) => e.code).toList(); | ||
await prefs.setStringList('countries', toSave); | ||
} | ||
|
||
Future<List<Country>> _loadFromCache() async { | ||
final cachedData = ref.read(sharedPreferencesProvider).getString(_cacheKey); | ||
if (cachedData == null) { | ||
Future<List<Country>> loadSavedCountryCodes() async { | ||
final saved = prefs.getStringList(prefsKey); | ||
|
||
if (saved == null) { | ||
return []; | ||
} | ||
final countries = <Country>[]; | ||
for (final country in saved) { | ||
try { | ||
countries.add(Country.fromJson(jsonDecode(country))); | ||
} on Exception catch (e) { | ||
throw Exception('Failed to load saved country: $e'); | ||
} | ||
} | ||
|
||
return List<Country>.from( | ||
(json.decode(cachedData) as List).map( | ||
(item) => Country.fromJson(item as Map<String, dynamic>), | ||
), | ||
); | ||
return countries; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.