mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 23:08:59 +00:00
add web_dashboard API (#333)
* add web_dashboard sample * add docs * address code review comments * restructure web_dashboard * set up provider and mock service * add copyright headers, use relative imports * add API class, add tests * fmt * rename services -> API, remove data library * use new API in app * add stream to items api * convert from StreamBuilder to StreamProvider * add subscription to Entry API * rename API classes * address comments * Update README.md * update README * remove routing_demo
This commit is contained in:
45
experimental/web_dashboard/lib/src/api/api.dart
Normal file
45
experimental/web_dashboard/lib/src/api/api.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Manipulates app data,
|
||||
abstract class DashboardApi {
|
||||
ItemApi get items;
|
||||
EntryApi get entries;
|
||||
}
|
||||
|
||||
/// Manipulates [Item] data.
|
||||
abstract class ItemApi {
|
||||
Future<Item> delete(String id);
|
||||
Future<Item> get(String id);
|
||||
Future<Item> insert(Item item);
|
||||
Future<List<Item>> list();
|
||||
Future<Item> update(Item item, String id);
|
||||
Stream<List<Item>> allItemsStream();
|
||||
}
|
||||
|
||||
/// Something being tracked.
|
||||
class Item {
|
||||
final String name;
|
||||
String id;
|
||||
|
||||
Item(this.name);
|
||||
}
|
||||
|
||||
/// Manipulates [Entry] data.
|
||||
abstract class EntryApi {
|
||||
Future<Entry> delete(String itemId, String id);
|
||||
Future<Entry> insert(String itemId, Entry entry);
|
||||
Future<List<Entry>> list(String itemId);
|
||||
Future<Entry> update(String itemId, String id, Entry entry);
|
||||
Stream<List<Entry>> allEntriesStream(String itemId);
|
||||
}
|
||||
|
||||
/// A number tracked at a point in time.
|
||||
class Entry {
|
||||
final int value;
|
||||
final DateTime time;
|
||||
String id;
|
||||
|
||||
Entry(this.value, this.time);
|
||||
}
|
||||
3
experimental/web_dashboard/lib/src/api/firebase.dart
Normal file
3
experimental/web_dashboard/lib/src/api/firebase.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
106
experimental/web_dashboard/lib/src/api/mock.dart
Normal file
106
experimental/web_dashboard/lib/src/api/mock.dart
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:uuid/uuid.dart' as uuid;
|
||||
|
||||
import 'api.dart';
|
||||
|
||||
class MockDashboardApi implements DashboardApi {
|
||||
@override
|
||||
final EntryApi entries = MockEntryApi();
|
||||
|
||||
@override
|
||||
final ItemApi items = MockItemApi();
|
||||
}
|
||||
|
||||
class MockItemApi implements ItemApi {
|
||||
Map<String, Item> _storage = {};
|
||||
StreamController<List<Item>> _streamController =
|
||||
StreamController<List<Item>>.broadcast();
|
||||
|
||||
@override
|
||||
Future<Item> delete(String id) async {
|
||||
_emit();
|
||||
return _storage.remove(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Item> get(String id) async {
|
||||
return _storage[id];
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Item> insert(Item item) async {
|
||||
var id = uuid.Uuid().v4();
|
||||
var newItem = Item(item.name)..id = id;
|
||||
_storage[id] = newItem;
|
||||
_emit();
|
||||
return newItem;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Item>> list() async {
|
||||
return _storage.values.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Item> update(Item item, String id) async {
|
||||
_storage[id] = item;
|
||||
return item..id = id;
|
||||
}
|
||||
|
||||
Stream<List<Item>> allItemsStream() {
|
||||
return _streamController.stream;
|
||||
}
|
||||
|
||||
void _emit() {
|
||||
_streamController.add(_storage.values.toList());
|
||||
}
|
||||
}
|
||||
|
||||
class MockEntryApi implements EntryApi {
|
||||
Map<String, Entry> _storage = {};
|
||||
StreamController<List<Entry>> _streamController =
|
||||
StreamController<List<Entry>>.broadcast();
|
||||
|
||||
@override
|
||||
Future<Entry> delete(String itemId, String id) async {
|
||||
_emit();
|
||||
return _storage.remove('$itemId-$id');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Entry> insert(String itemId, Entry entry) async {
|
||||
var id = uuid.Uuid().v4();
|
||||
var newEntry = Entry(entry.value, entry.time)..id = id;
|
||||
_storage['$itemId-$id'] = newEntry;
|
||||
_emit();
|
||||
return newEntry;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Entry>> list(String itemId) async {
|
||||
return _storage.keys
|
||||
.where((k) => k.startsWith(itemId))
|
||||
.map((k) => _storage[k])
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Entry> update(String itemId, String id, Entry entry) async {
|
||||
_storage['$itemId-$id'] = entry;
|
||||
return entry..id = id;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<List<Entry>> allEntriesStream(String itemId) {
|
||||
return _streamController.stream;
|
||||
}
|
||||
|
||||
void _emit() {
|
||||
_streamController.add(_storage.values.toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user