Refactored events to notifications on front-end, added prefutils to read

shared  preferences.
This commit is contained in:
rzuasti
2026-03-02 13:59:50 -05:00
parent 2d64efa44b
commit e6b5594226
14 changed files with 293 additions and 38 deletions
+32 -18
View File
@@ -1,28 +1,42 @@
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:frontend/model/event.dart';
import 'package:encrypter/encrypter/xor.dart';
import 'package:frontend/utils/pref_utils.dart';
import '../model/notification.dart';
class BackendAPI {
BackendAPI._singleton();
static final BackendAPI _instance = BackendAPI._internal();
static final BackendAPI instance = BackendAPI._singleton();
static BackendAPI get instance => _instance;
static final String _baseUrl = 'http://localhost:3000/api';
static final String _apiKey = 'super_secret';
BackendAPI._internal() {
// _baseUrl =
// PrefUtil.getValue("base_url", "http://localhost:3000/api") as String;
// _apiKey = XOR().xorDecode(PrefUtil.getValue("api_key", "") as String);
final Dio _dio = Dio(
BaseOptions(
baseUrl: _baseUrl,
headers: {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.authorizationHeader: 'Bearer $_apiKey',
},
),
);
_baseUrl = "http://localhost:3000/api";
_apiKey = "super_secret";
Future<List<Event>> listNotifications() async {
print('Base URL: $_baseUrl');
print('API KEY $_apiKey');
_dio = Dio(
BaseOptions(
baseUrl: _baseUrl,
headers: {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.authorizationHeader: 'Bearer $_apiKey',
},
),
);
}
late String _baseUrl;
late String _apiKey;
late Dio _dio;
Future<List<Notification>> listNotifications() async {
print('About to call /notifications');
Response response;
@@ -33,8 +47,8 @@ class BackendAPI {
List<dynamic> list = response.data;
print('List contains ' + list.length.toString() + ' items');
List<Event> events = List<Event>.from(
list.map((item) => Event.fromJson(item)),
List<Notification> events = List<Notification>.from(
list.map((item) => Notification.fromJson(item)),
);
print('Parsed ' + events.length.toString() + ' events');
+40
View File
@@ -0,0 +1,40 @@
import 'package:shared_preferences/shared_preferences.dart';
class PrefUtil {
static late final SharedPreferences preferences;
static bool _init = false;
static Future init() async {
if (_init) return;
preferences = await SharedPreferences.getInstance();
_init = true;
return preferences;
}
static void setValue(String key, Object value) {
switch (value.runtimeType) {
case String:
preferences.setString(key, value as String);
break;
case bool:
preferences.setBool(key, value as bool);
break;
case int:
preferences.setInt(key, value as int);
break;
default:
}
}
static Object getValue(String key, Object defaultValue) {
switch (defaultValue.runtimeType) {
case String:
return preferences.getString(key) ?? "";
case bool:
return preferences.getBool(key) ?? false;
case int:
return preferences.getInt(key) ?? 0;
default:
return defaultValue;
}
}
}