mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Refactored events to notifications on front-end, added prefutils to read
shared preferences.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:frontend/utils/pref_utils.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'navigation.dart';
|
||||
|
||||
void main() {
|
||||
void main() async {
|
||||
PrefUtil.init();
|
||||
runApp(const MainApp());
|
||||
}
|
||||
|
||||
@@ -17,7 +19,7 @@ final class MainApp extends StatelessWidget {
|
||||
title: 'OOTT',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: Colors.green,
|
||||
seedColor: Colors.deepOrange,
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'notification_type.dart';
|
||||
|
||||
class Event {
|
||||
class Notification {
|
||||
int id;
|
||||
String title;
|
||||
String body;
|
||||
@@ -8,7 +8,7 @@ class Event {
|
||||
NotificationType notificationType;
|
||||
DateTime createdOn;
|
||||
|
||||
Event({
|
||||
Notification({
|
||||
required this.id,
|
||||
required this.title,
|
||||
required this.body,
|
||||
@@ -17,7 +17,7 @@ class Event {
|
||||
this.isNew = true,
|
||||
});
|
||||
|
||||
Event.fromJson(Map<String, dynamic> json)
|
||||
Notification.fromJson(Map<String, dynamic> json)
|
||||
: id = json['id'] as int,
|
||||
createdOn = DateTime.parse(json['created_on'] as String),
|
||||
notificationType = NotificationType.fromString(
|
||||
@@ -1,10 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'events/events_list.dart';
|
||||
import 'notifications/notification_list.dart';
|
||||
|
||||
// Routes definitions
|
||||
final GoRouter router = GoRouter(
|
||||
initialLocation: '/events',
|
||||
initialLocation: '/notifications',
|
||||
routes: [
|
||||
ShellRoute(
|
||||
builder: (context, state, child) {
|
||||
@@ -12,9 +12,9 @@ final GoRouter router = GoRouter(
|
||||
},
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/events',
|
||||
name: 'events',
|
||||
builder: (context, state) => EventsList(),
|
||||
path: '/notifications',
|
||||
name: 'notifications',
|
||||
builder: (context, state) => NotificationList(),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/devices',
|
||||
@@ -65,7 +65,7 @@ class MainShell extends StatelessWidget {
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.notifications_outlined),
|
||||
selectedIcon: Icon(Icons.notifications),
|
||||
label: Text('Events'),
|
||||
label: Text('Notifications'),
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.devices_other_outlined),
|
||||
@@ -105,7 +105,7 @@ class MainShell extends StatelessWidget {
|
||||
|
||||
int _calculateSelectedIndex(BuildContext context) {
|
||||
final location = GoRouterState.of(context).uri.path;
|
||||
if (location.startsWith('/events')) return 0;
|
||||
if (location.startsWith('/notifications')) return 0;
|
||||
if (location.startsWith('/devices')) return 1;
|
||||
if (location.startsWith('/settings')) return 2;
|
||||
if (location.startsWith('/about')) return 3;
|
||||
@@ -115,7 +115,7 @@ int _calculateSelectedIndex(BuildContext context) {
|
||||
void _onDestinationSelected(int index, BuildContext context) {
|
||||
switch (index) {
|
||||
case 0:
|
||||
context.go('/events');
|
||||
context.go('/notifications');
|
||||
break;
|
||||
case 1:
|
||||
context.go('/devices');
|
||||
|
||||
+6
-6
@@ -1,16 +1,16 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../utils/friendly_date_formatter.dart';
|
||||
import '../model/event.dart';
|
||||
import '../model/notification.dart' as oott_model;
|
||||
import '../utils/oott_api.dart';
|
||||
|
||||
class EventsList extends StatefulWidget {
|
||||
class NotificationList extends StatefulWidget {
|
||||
@override
|
||||
_EventListState createState() => _EventListState();
|
||||
_NotificationListState createState() => _NotificationListState();
|
||||
}
|
||||
|
||||
class _EventListState extends State<EventsList> {
|
||||
class _NotificationListState extends State<NotificationList> {
|
||||
bool _isLoading = true;
|
||||
List<Event>? _events;
|
||||
List<oott_model.Notification>? _events;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -27,7 +27,7 @@ class _EventListState extends State<EventsList> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Events')),
|
||||
appBar: AppBar(title: const Text('Notifications')),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: ListView.separated(
|
||||
@@ -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');
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user