Skip to content

Commit

Permalink
Force currencies download when revoke turns off
Browse files Browse the repository at this point in the history
  • Loading branch information
ferraridamiano committed Feb 11, 2024
1 parent 68aa9b0 commit 5c5485f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
50 changes: 31 additions & 19 deletions lib/models/currencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';

class Currencies {
static const defaultExchangeRates = {
Expand Down Expand Up @@ -76,10 +77,34 @@ class Currencies {
}
}

final currenciesProvider = FutureProvider<Currencies>((ref) async {
var pref = await ref.watch(sharedPref.future);
class CurrenciesNotifier extends AsyncNotifier<Currencies> {
static final provider = AsyncNotifierProvider<CurrenciesNotifier, Currencies>(
CurrenciesNotifier.new);
late SharedPreferences pref;

Currencies readSavedCurrencies() {
@override
Future<Currencies> build() async {
pref = await ref.watch(sharedPref.future);

final String now = DateFormat("yyyy-MM-dd").format(DateTime.now());
// Let's search before if we already have downloaded the exchange rates
String? lastUpdate = pref.getString("lastUpdateCurrencies");
// if I have never updated the conversions or if I have updated before today
// I have to update
if (!(ref.read(RevokeInternetNotifier.provider).valueOrNull ?? false) &&
(lastUpdate == null || lastUpdate != now)) {
return _downloadCurrencies();
}
// If I already have the data of today I just use it, no need of read them
// from the web
return _readSavedCurrencies();
}

void forceCurrenciesDownload() async {
state = AsyncData(await _downloadCurrencies());
}

Currencies _readSavedCurrencies() {
String? lastUpdate = pref.getString('lastUpdateCurrencies');
String? currenciesRead = pref.getString('currenciesRates');
if (currenciesRead != null) {
Expand All @@ -91,7 +116,7 @@ final currenciesProvider = FutureProvider<Currencies>((ref) async {

/// Updates the currencies exchange rates with the latest values. It will also
/// update the status at the end (updated or error)
Future<Currencies> downloadCurrencies() async {
Future<Currencies> _downloadCurrencies() async {
final stringRequest =
Currencies.defaultExchangeRates.keys.where((e) => e != 'EUR').join('+');
try {
Expand Down Expand Up @@ -128,19 +153,6 @@ final currenciesProvider = FutureProvider<Currencies>((ref) async {
} catch (e) {
debugPrint(e.toString());
}
return readSavedCurrencies();
}

final String now = DateFormat("yyyy-MM-dd").format(DateTime.now());
// Let's search before if we already have downloaded the exchange rates
String? lastUpdate = pref.getString("lastUpdateCurrencies");
// if I have never updated the conversions or if I have updated before today
// I have to update
if (!(ref.read(RevokeInternetNotifier.provider).valueOrNull ?? false) &&
(lastUpdate == null || lastUpdate != now)) {
return downloadCurrencies();
return _readSavedCurrencies();
}
// If I already have the data of today I just use it, no need of read them
// from the web
return readSavedCurrencies();
});
}
2 changes: 1 addition & 1 deletion lib/models/properties_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final propertiesListProvider = FutureProvider<List<Property>>((ref) async {
removeTrailingZeros: removeTrailingZeros,
name: PROPERTYX.volume),
SimpleCustomProperty(
ref.watch(currenciesProvider).when(
ref.watch(CurrenciesNotifier.provider).when(
data: (currencies) => currencies.exchangeRates,
error: (_, trace) => Currencies.defaultExchangeRates,
loading: () => Currencies.defaultExchangeRates),
Expand Down
3 changes: 2 additions & 1 deletion lib/pages/conversion_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ConversionPage extends ConsumerWidget {

Widget? subtitleWidget;
if (currentProperty == PROPERTYX.currencies) {
Currencies? currencies = ref.watch(currenciesProvider).valueOrNull;
Currencies? currencies =
ref.watch(CurrenciesNotifier.provider).valueOrNull;
if (currencies == null) {
subtitleWidget = const SizedBox(
height: 30,
Expand Down
4 changes: 4 additions & 0 deletions lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'package:converterpro/models/currencies.dart';
import 'package:converterpro/models/settings.dart';
import 'package:converterpro/styles/consts.dart';
import 'package:converterpro/utils/utils_widgets.dart';
Expand Down Expand Up @@ -135,6 +136,9 @@ class SettingsPage extends ConsumerWidget {
);
} else {
ref.read(RevokeInternetNotifier.provider.notifier).set(val);
ref
.read(CurrenciesNotifier.provider.notifier)
.forceCurrenciesDownload();
}
},
shape: const RoundedRectangleBorder(borderRadius: borderRadius),
Expand Down

0 comments on commit 5c5485f

Please sign in to comment.