1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-14 11:28:36 +00:00

Add firebase support to web_dashboard (#421)

* add mock data, app state, model classes

* Set up app without ChangeNotifier

* refactor

* add experiments to experimental/

* Add project-agnostic Firebase authentication code

* add sign in button

* add stub firebase API

* add firestore

* refactor code for google_sign_in

* update pubspec.lock

* switch to mocks for non-firebase version

* Add firebase instructions to the README

* fix README

* sign in silently if the user is already signed in

* add json_serializable

* update README

* ignore 'id' field on types

* Implement FirebaseItemApi

* Add build_runner instructions to README

* remove experiments directory

* add EditItemForm

* move types.dart into api.dart

* move mock and firebase configuration into the constructor

* add main_mock entrypoint

* add copyright checks to grinder script

* fix fix-copyright task

* run grind fix-copyright

* add run and generate tasks

* add run tasks to grind script

* add fillWithMockData() fix delete() in mock API

* add edit / new form dialogs

* Add charts that display entries from Firebase

* Add Entries list without editing

* refactor home page

* format

* Add entries page functionality

* Show current day in charts

* cleanup: pubspec.lock, remove type annotation

* Remove _selectedItem from Home page

Add ItemsDropdown
Use ItemsDropdown in NewEntryDialog / NewEntryForm

* rename item-category

* don't wait to show snackbar on delete

* fix circular progress indicator

* Move dialogs into dialogs.dart

* run grind fix-copyright

* remove unused import

* Refactor entry total calculation, add chart_utils library

* fix bug in chart_utils.dart

* convert CategoryChart to a stateless widget

* use a const for number of days in chart

* code review updates

- rename stream -> subscribe
- timeStamp -> timestamp
- remove latest() from API
- use FutureBuilder and StreamBuilder instead of stateful widget
- rename variables in mock_service_test.dart

* use a single collection reference in firebase API

* remove reference to stream in mock API

* Use a new type,  _EntriesEvent to improve filtering in mock API

* add analysis_options.yaml and fix (most) issues

* fix avoid_types_on_closure_parameters lint warnings

* use spread operator in dashboard.dart

* handle case where selected item in the category dropdown goes away

* use StreamBuilder + FutureBuilder on Entries page

* rename method

* use fake firebase configuration

* update pubspec.lock

* update README

* Change categories_dropdown to FutureBuilder + StreamBuilder

* Update minSdkVersion in build.gradle

SDK version 16 was failing: "The number of method references in a .dex
file cannot exceed 64K."

* update README

* Use a collection reference in FirebaseEntryApi

Already added to FirebaseCategoryApi

* Invoke onSelected in CategoriesDropdown when necessary

Also, avoid calling onSelected during a build.

* fix misnamed var

* remove unused import

* Use relative imports

* Use extension methods for DateTime utilities

* remove forms.dart

* Make Firebase instructions specific for this sample

* add copyright headers

* fix grammar

* dartfmt

* avoid setState() during build phase in CategoryDropdown

* add empty test to material_theme_builder
This commit is contained in:
John Ryan
2020-05-26 13:14:21 -07:00
committed by GitHub
parent b518c322cc
commit 395ae8c0bb
39 changed files with 2730 additions and 220 deletions

View File

@@ -1,3 +1,151 @@
// 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 'package:cloud_firestore/cloud_firestore.dart';
import 'api.dart';
class FirebaseDashboardApi implements DashboardApi {
@override
final EntryApi entries;
@override
final CategoryApi categories;
FirebaseDashboardApi(Firestore firestore, String userId)
: entries = FirebaseEntryApi(firestore, userId),
categories = FirebaseCategoryApi(firestore, userId);
}
class FirebaseEntryApi implements EntryApi {
final Firestore firestore;
final String userId;
final CollectionReference _categoriesRef;
FirebaseEntryApi(this.firestore, this.userId)
: _categoriesRef = firestore.collection('users/$userId/categories');
@override
Stream<List<Entry>> subscribe(String categoryId) {
var snapshots = _categoriesRef
.document('$categoryId')
.collection('entries')
.snapshots();
var result = snapshots.map((querySnapshot) {
return querySnapshot.documents.map((snapshot) {
return Entry.fromJson(snapshot.data)..id = snapshot.documentID;
}).toList();
});
return result;
}
@override
Future<Entry> delete(String categoryId, String id) async {
var document = _categoriesRef.document('$categoryId/entries/$id');
var entry = await get(categoryId, document.documentID);
await document.delete();
return entry;
}
@override
Future<Entry> insert(String categoryId, Entry entry) async {
var document = await _categoriesRef
.document('$categoryId')
.collection('entries')
.add(entry.toJson());
return await get(categoryId, document.documentID);
}
@override
Future<List<Entry>> list(String categoryId) async {
var entriesRef =
_categoriesRef.document('$categoryId').collection('entries');
var querySnapshot = await entriesRef.getDocuments();
var entries = querySnapshot.documents
.map((doc) => Entry.fromJson(doc.data)..id = doc.documentID)
.toList();
return entries;
}
@override
Future<Entry> update(String categoryId, String id, Entry entry) async {
var document = _categoriesRef.document('$categoryId/entries/$id');
await document.setData(entry.toJson());
var snapshot = await document.get();
return Entry.fromJson(snapshot.data)..id = snapshot.documentID;
}
@override
Future<Entry> get(String categoryId, String id) async {
var document = _categoriesRef.document('$categoryId/entries/$id');
var snapshot = await document.get();
return Entry.fromJson(snapshot.data)..id = snapshot.documentID;
}
}
class FirebaseCategoryApi implements CategoryApi {
final Firestore firestore;
final String userId;
final CollectionReference _categoriesRef;
FirebaseCategoryApi(this.firestore, this.userId)
: _categoriesRef = firestore.collection('users/$userId/categories');
@override
Stream<List<Category>> subscribe() {
var snapshots = _categoriesRef.snapshots();
var result = snapshots.map((querySnapshot) {
return querySnapshot.documents.map((snapshot) {
return Category.fromJson(snapshot.data)..id = snapshot.documentID;
}).toList();
});
return result;
}
@override
Future<Category> delete(String id) async {
var document = _categoriesRef.document('$id');
var categories = await get(document.documentID);
await document.delete();
return categories;
}
@override
Future<Category> get(String id) async {
var document = _categoriesRef.document('$id');
var snapshot = await document.get();
return Category.fromJson(snapshot.data)..id = snapshot.documentID;
}
@override
Future<Category> insert(Category category) async {
var document = await _categoriesRef.add(category.toJson());
return await get(document.documentID);
}
@override
Future<List<Category>> list() async {
var querySnapshot = await _categoriesRef.getDocuments();
var categories = querySnapshot.documents
.map((doc) => Category.fromJson(doc.data)..id = doc.documentID)
.toList();
return categories;
}
@override
Future<Category> update(Category category, String id) async {
var document = _categoriesRef.document('$id');
await document.setData(category.toJson());
var snapshot = await document.get();
return Category.fromJson(snapshot.data)..id = snapshot.documentID;
}
}