Load profile information on startup
This commit is contained in:
		@@ -1,10 +1,8 @@
 | 
			
		||||
// ignore_for_file: non_constant_identifier_names
 | 
			
		||||
 | 
			
		||||
import 'package:freezed_annotation/freezed_annotation.dart';
 | 
			
		||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
			
		||||
import 'package:logging/logging.dart';
 | 
			
		||||
import 'package:moneymgr_mobile/services/storage/prefs.dart';
 | 
			
		||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
 | 
			
		||||
 | 
			
		||||
import 'api_client.dart';
 | 
			
		||||
 | 
			
		||||
@@ -40,33 +38,30 @@ abstract class ServerConfig with _$ServerConfig {
 | 
			
		||||
 | 
			
		||||
/// Auth API
 | 
			
		||||
extension ServerApi on ApiClient {
 | 
			
		||||
  /// Get authentication information from server
 | 
			
		||||
  /// Get server configuration
 | 
			
		||||
  Future<ServerConfig> serverConfig() async {
 | 
			
		||||
    final response = await execute("/server/config", method: "GET");
 | 
			
		||||
    return ServerConfig.fromJson(response.data);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Get server configuration, or retrieve cached information (if available, in
 | 
			
		||||
  /// case of failure)
 | 
			
		||||
  Future<ServerConfig> serverConfigOrCache() async {
 | 
			
		||||
    try {
 | 
			
		||||
      final config = await serverConfig();
 | 
			
		||||
      this.prefs.setServerConfig(config);
 | 
			
		||||
      return config;
 | 
			
		||||
    } catch (e, s) {
 | 
			
		||||
      Logger.root.warning("Failed to fetch server configuration! $e $s");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    final cached = this.prefs.serverConfig();
 | 
			
		||||
    if (cached == null) {
 | 
			
		||||
      throw Exception(
 | 
			
		||||
        "Could not fetch server configuration, cached version is unavailable!",
 | 
			
		||||
      );
 | 
			
		||||
    }
 | 
			
		||||
    return cached;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get authentication information from server, or retrieve cached information (if available, in
 | 
			
		||||
/// case of failure)
 | 
			
		||||
@riverpod
 | 
			
		||||
Future<ServerConfig> serverConfigOrCache(Ref ref) async {
 | 
			
		||||
  final client = ref.watch(apiServiceProvider)!;
 | 
			
		||||
  try {
 | 
			
		||||
    final config = await client.serverConfig();
 | 
			
		||||
    client.prefs.setServerConfig(config);
 | 
			
		||||
    return config;
 | 
			
		||||
  } catch (e, s) {
 | 
			
		||||
    Logger.root.warning("Failed to fetch server configuration! $e $s");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  final cached = client.prefs.serverConfig();
 | 
			
		||||
  if (cached == null) {
 | 
			
		||||
    throw Exception(
 | 
			
		||||
      "Could not fetch server configuration, cached version is unavailable!",
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
  return cached;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user