mirror of
https://github.com/flutter/samples.git
synced 2025-11-11 23:39:14 +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:
65
experimental/web_dashboard/lib/src/utils/chart_utils.dart
Normal file
65
experimental/web_dashboard/lib/src/utils/chart_utils.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
// 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 '../api/api.dart';
|
||||
import 'day_helpers.dart';
|
||||
|
||||
/// The total value of one or more [Entry]s on a given day.
|
||||
class EntryTotal {
|
||||
final DateTime day;
|
||||
int value;
|
||||
|
||||
EntryTotal(this.day, this.value);
|
||||
}
|
||||
|
||||
/// Returns a list of [EntryTotal] objects. Each [EntryTotal] is the sum of
|
||||
/// the values of all the entries on a given day.
|
||||
List<EntryTotal> entryTotalsByDay(List<Entry> entries, int daysAgo,
|
||||
{DateTime today}) {
|
||||
today ??= DateTime.now();
|
||||
return _entryTotalsByDay(entries, daysAgo, today).toList();
|
||||
}
|
||||
|
||||
Iterable<EntryTotal> _entryTotalsByDay(
|
||||
List<Entry> entries, int daysAgo, DateTime today) sync* {
|
||||
var start = today.subtract(Duration(days: daysAgo));
|
||||
var entriesByDay = _entriesInRange(start, today, entries);
|
||||
|
||||
for (var i = 0; i < entriesByDay.length; i++) {
|
||||
var list = entriesByDay[i];
|
||||
var entryTotal = EntryTotal(start.add(Duration(days: i)), 0);
|
||||
|
||||
for (var entry in list) {
|
||||
entryTotal.value += entry.value;
|
||||
}
|
||||
|
||||
yield entryTotal;
|
||||
}
|
||||
}
|
||||
|
||||
/// Groups entries by day between [start] and [end]. The result is a list of
|
||||
/// lists. The outer list represents the number of days since [start], and the
|
||||
/// inner list is the group of entries on that day.
|
||||
List<List<Entry>> _entriesInRange(
|
||||
DateTime start, DateTime end, List<Entry> entries) =>
|
||||
_entriesInRangeImpl(start, end, entries).toList();
|
||||
|
||||
Iterable<List<Entry>> _entriesInRangeImpl(
|
||||
DateTime start, DateTime end, List<Entry> entries) sync* {
|
||||
start = start.atMidnight;
|
||||
end = end.atMidnight;
|
||||
var d = start;
|
||||
|
||||
while (d.compareTo(end) <= 0) {
|
||||
var es = <Entry>[];
|
||||
for (var entry in entries) {
|
||||
if (d.isSameDay(entry.time.atMidnight)) {
|
||||
es.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
yield es;
|
||||
d = d.add(Duration(days: 1));
|
||||
}
|
||||
}
|
||||
15
experimental/web_dashboard/lib/src/utils/day_helpers.dart
Normal file
15
experimental/web_dashboard/lib/src/utils/day_helpers.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
// 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.
|
||||
|
||||
extension DayUtils on DateTime {
|
||||
/// The UTC date portion of a datetime, without the minutes, seconds, etc.
|
||||
DateTime get atMidnight {
|
||||
return DateTime.utc(year, month, day);
|
||||
}
|
||||
|
||||
/// Checks that the two [DateTime]s share the same date.
|
||||
bool isSameDay(DateTime d2) {
|
||||
return this.year == d2.year && this.month == d2.month && this.day == d2.day;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user