From 41e0e0ec64d5de4e385c37f37908d7783ce0b36a Mon Sep 17 00:00:00 2001 From: Maximilian Betz <Maximilian.Betz@awi.de> Date: Wed, 19 Jan 2022 12:59:33 +0100 Subject: [PATCH] added singleton element for local event storage --- README.md | 3 +- lib/data_model.dart | 48 +++++++++++---- lib/main.dart | 54 +++++++++-------- pubspec.lock | 139 ++++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 2 + 5 files changed, 207 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index c513fd0..ab870c5 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,5 @@ A cross plattform project for android and ios to log events offline everywhere a ## Questions: -- How to store not uploaded events locally? Dumping jsons to a file? +- How to store not uploaded events locally? Dumping jsons to a file?, ObjectBox, SharedPreferences, + diff --git a/lib/data_model.dart b/lib/data_model.dart index 47d11a5..c690bf1 100644 --- a/lib/data_model.dart +++ b/lib/data_model.dart @@ -1,4 +1,4 @@ - +import 'package:shared_preferences/shared_preferences.dart'; class Event{ int id; @@ -8,14 +8,40 @@ class Event{ String description; String status; - Event({ - required this.id, - required this.urn, - required this.label, - required this.type, - required this.description, - required this.status - }); + Event( + this.id, + this.urn, + this.label, + this.type, + this.description, + this.status + ); + + factory Event.fromJson(Map<String, dynamic> parsedJson){ + return Event( parsedJson['id'] as int, + parsedJson['urn'] as String, + parsedJson['label'] as String, + parsedJson['type'] as String, + parsedJson['description'] as String, + parsedJson['status'] as String ); + } + + @override + String toString(){ + return '{ ${this.id}, ${this.urn}, ${this.label}, ${this.type}, ${this.description}, ${this.status} }'; + } +} + +abstract class EventStoreBase{ + List<Event> store = []; + + void setStore(List<Event> st){ + store = st; + } + + void reset(){ + store = []; + } } class EventType{ @@ -26,4 +52,6 @@ class EventType{ required this.name, required this.id }); -} \ No newline at end of file +} + + diff --git a/lib/main.dart b/lib/main.dart index f4401a0..754cbd5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,7 +2,30 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'data_model.dart'; + +class EventStoreInstance extends EventStoreBase{ + static final EventStoreInstance _instance = EventStoreInstance._internal(); + + factory EventStoreInstance(){ + return _instance; + } + + EventStoreInstance._internal(){ + store = []; + } + +} + void main() { + + //Add some dummy events to eventstore + EventStoreInstance events = EventStoreInstance(); + events.store.add(Event.fromJson({'id': 8311, 'urn':'station:neumayer_iii:awi_snow_sampler_1', 'label':'SML_KO21_SC01', 'type':'Deployment', 'description':'Remi tool Tag1 1 Traverse', 'status':'PENDING' })); + events.store.add(Event.fromJson({'id': 8311, 'urn':'station:neumayer_iii:awi_snow_sampler_1', 'label':'SML_KO21_SC01', 'type':'Deployment', 'description':'Remi tool Tag1 1 Traverse', 'status':'PENDING' })); + events.store.add(Event.fromJson({'id': 8311, 'urn':'station:neumayer_iii:awi_snow_sampler_1', 'label':'SML_KO21_SC01', 'type':'Deployment', 'description':'Remi tool Tag1 1 Traverse', 'status':'PENDING' })); + events.store.add(Event.fromJson({'id': 8311, 'urn':'station:neumayer_iii:awi_snow_sampler_1', 'label':'SML_KO21_SC01', 'type':'Deployment', 'description':'Remi tool Tag1 1 Traverse', 'status':'PENDING' })); + + runApp(MaterialApp( title: 'Mobile Event Log', theme: ThemeData( @@ -82,36 +105,11 @@ class AddEvent extends StatelessWidget { } class ViewEvents extends StatelessWidget { - // Add some dummy events: - List<Event> events = [] - ..add(Event( - id: 8311, - urn: 'station:neumayer_iii:awi_snow_sampler_1', - label: 'SML_KO21_SC01', - type: 'Deployment', - description: 'Remi tool Tag1 1 Traverse', - status: 'PENDING')) - ..add(Event( - id: 8311, - urn: 'station:neumayer_iii:awi_snow_sampler_1', - label: 'SML_KO21_SC01', - type: 'Deployment', - description: 'Remi tool Tag1 1 Traverse', - status: 'PENDING')) - ..add(Event( - id: 8311, - urn: 'station:neumayer_iii:awi_snow_sampler_1', - label: 'SML_KO21_SC01', - type: 'Deployment', - description: 'Remi tool Tag1 1 Traverse', - status: 'PENDING')); - - //for (var i = 0; i<100; i++){ - // events.add(Event(id: i, urn: 'station:neumayer_iii:awi_snow_sampler_1', label: 'SML_KO21_SC01', type: 'Deployment', description: 'Remi tool Tag1 1 Traverse', status: 'PENDING')); - //} @override Widget build(BuildContext context) { + // Get singleton to access locally stored events: + final EventStoreInstance events = EventStoreInstance(); return Scaffold( appBar: AppBar( title: const Text("View Added Events"), @@ -160,7 +158,7 @@ class ViewEvents extends StatelessWidget { ), ], rows: <DataRow>[ - for (var event in events) + for (var event in events.store) DataRow( cells: <DataCell>[ DataCell(Text(event.id.toString())), diff --git a/pubspec.lock b/pubspec.lock index 306911b..b87e8a0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -57,6 +57,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.0" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.2" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.2" flutter: dependency: "direct main" description: flutter @@ -74,6 +88,18 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.3" lints: dependency: transitive description: @@ -102,6 +128,104 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.0" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.5" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.3" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.5" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.2" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.4" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.12" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.10" + shared_preferences_ios: + dependency: transitive + description: + name: shared_preferences_ios + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.9" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" + shared_preferences_macos: + dependency: transitive + description: + name: shared_preferences_macos + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.3" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" sky_engine: dependency: transitive description: flutter @@ -163,5 +287,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.1" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.6" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0" sdks: dart: ">=2.15.1 <3.0.0" + flutter: ">=2.5.0" diff --git a/pubspec.yaml b/pubspec.yaml index 345d44b..e2f48a4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,8 @@ dependencies: flutter: sdk: flutter + shared_preferences: ^2.0.12 + # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. -- GitLab