Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
enm10k committed Jun 23, 2024
1 parent abf6d8c commit 0d5ce7f
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 211 deletions.
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ PODS:
- Flutter
- image_picker_ios (0.0.1):
- Flutter
- integration_test (0.0.1):
- Flutter
- path_provider_foundation (0.0.1):
- Flutter
- FlutterMacOS
Expand All @@ -60,6 +62,7 @@ DEPENDENCIES:
- flutter_downloader (from `.symlinks/plugins/flutter_downloader/ios`)
- flutter_secure_storage (from `.symlinks/plugins/flutter_secure_storage/ios`)
- image_picker_ios (from `.symlinks/plugins/image_picker_ios/ios`)
- integration_test (from `.symlinks/plugins/integration_test/ios`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- sqflite (from `.symlinks/plugins/sqflite/darwin`)
Expand All @@ -82,6 +85,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/flutter_secure_storage/ios"
image_picker_ios:
:path: ".symlinks/plugins/image_picker_ios/ios"
integration_test:
:path: ".symlinks/plugins/integration_test/ios"
path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin"
shared_preferences_foundation:
Expand All @@ -97,6 +102,7 @@ SPEC CHECKSUMS:
flutter_downloader: b7301ae057deadd4b1650dc7c05375f10ff12c39
flutter_secure_storage: d33dac7ae2ea08509be337e775f6b59f1ff45f12
image_picker_ios: c560581cceedb403a6ff17f2f816d7fea1421fc1
integration_test: 13825b8a9334a850581300559b8839134b124670
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
SDWebImage: dfe95b2466a9823cf9f0c6d01217c06550d7b29a
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
Expand Down
78 changes: 49 additions & 29 deletions lib/common/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,70 @@ import 'package:flutter/foundation.dart';

import 'package:nocodb/common/preferences.dart';

final settings = Settings();
import 'package:nocodb/nocodb_sdk/client.dart';

final settings = _Settings();

class Settings {
Settings({required this.host, required this.token, this.username});

final String? username;
final String host;
final Token token;
}

const _kUsername = 'username';
const _kHost = 'host';
const _kAuthToken = 'auth_token';
const _kApiToken = 'api_token';

class _Settings {
Preferences? prefs;
bool get initialized => prefs != null;
init(Preferences prefs) {
this.prefs = prefs;
}

static const _authToken = 'authToken';
Future<String?> get authToken async =>
await prefs?.getSecure(key: _authToken);
Future<void> save({
required String host,
required Token token,
String? username,
}) async {
await clear();
await prefs?.set(key: _kHost, value: host);
if (username != null) {
await prefs?.set(key: _kUsername, value: username);
}

Future<void> setAuthToken(String v) async =>
await prefs?.set(key: _authToken, value: v, secure: true);
switch (token) {
case AuthToken(authToken: final authToken):
await prefs?.set(key: _kAuthToken, value: authToken, secure: true);
case ApiToken(apiToken: final apiToken):
await prefs?.set(key: _kApiToken, value: apiToken, secure: true);
default:
throw Exception('unsupported token type: ${token.runtimeType}');
}
}

static const _email = 'email';
Future<String?> get email async => await prefs?.get(key: _email);
Future<Settings?> get() async {
final host = await prefs?.get<String>(key: _kHost);
if (host == null) {
return null;
}

Future<void> setEmail(String v) async => await prefs?.set(
key: _email,
value: v,
);
final username = await prefs?.get<String>(key: _kUsername);
final authToken = await prefs?.getSecure(key: _kAuthToken);
if (authToken != null) {
return Settings(username: username, host: host, token: AuthToken(authToken));
}

static const _apiBaseUrl = 'apiBaseUrl';
Future<String?> get apiBaseUrl async {
final v = await prefs?.get<String>(key: _apiBaseUrl);
if (v == null && !kIsWeb && Platform.isAndroid) {
return 'http://10.0.2.2:8080';
final apiToken = await prefs?.getSecure(key: _kApiToken);
if (apiToken != null) {
return Settings(username: username, host: host, token: ApiToken(apiToken));
}
return v;
return null;
}

Future<void> setApiBaseUrl(String v) async =>
await prefs?.set(key: _apiBaseUrl, value: v);

static const _rememberMe = 'rememberMe';
Future<bool> get rememberMe async =>
await prefs?.get<bool>(key: _rememberMe) ?? false;
Future<void> setRememberMe(bool v) async => await prefs?.set(
key: _rememberMe,
value: v,
);

Future<void> clear() async {
await prefs?.clear();
}
Expand Down
Loading

0 comments on commit 0d5ce7f

Please sign in to comment.