fbmobile/lib/core/services/storage_service.dart

60 lines
1.5 KiB
Dart
Raw Permalink Normal View History

2021-02-02 14:33:23 +00:00
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/session.dart';
class StorageService {
2023-01-04 20:17:54 +00:00
static const _sessionKey = 'session';
static const _lastUrlKey = 'last_url';
2021-02-02 14:33:23 +00:00
Future<bool> storeLastUrl(String url) {
2023-01-04 20:17:54 +00:00
return _store(_lastUrlKey, url);
2021-02-02 14:33:23 +00:00
}
Future<String?> retrieveLastUrl() async {
2023-01-04 20:17:54 +00:00
return await _retrieve(_lastUrlKey);
2021-02-02 14:33:23 +00:00
}
Future<bool> hasLastUrl() async {
2023-01-04 20:17:54 +00:00
return await _exists(_lastUrlKey);
2021-02-02 14:33:23 +00:00
}
Future<bool> storeSession(Session session) {
2023-01-04 20:17:54 +00:00
return _store(_sessionKey, json.encode(session));
2021-02-02 14:33:23 +00:00
}
Future<Session> retrieveSession() async {
2023-01-04 20:17:54 +00:00
var retrieve = await _retrieve(_sessionKey);
return Session.fromJson(json.decode(retrieve!));
2021-02-02 14:33:23 +00:00
}
Future<bool> hasSession() {
2023-01-04 20:17:54 +00:00
return _exists(_sessionKey);
2021-02-02 14:33:23 +00:00
}
Future<bool> removeSession() {
2023-01-04 20:17:54 +00:00
return _remove(_sessionKey);
2021-02-02 14:33:23 +00:00
}
Future<bool> _exists(String key) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.containsKey(key);
}
Future<bool> _remove(String key) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.remove(key);
}
Future<String?> _retrieve(String key) async {
2021-02-02 14:33:23 +00:00
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(key);
}
Future<bool> _store(String key, String value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setString(key, value);
}
}