diff --git a/my_app/lib/main.dart b/my_app/lib/main.dart index 71f7c30..1eeecd9 100644 --- a/my_app/lib/main.dart +++ b/my_app/lib/main.dart @@ -3,6 +3,8 @@ import 'dart:convert'; import 'dart:io'; +import 'dart:math'; + import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; @@ -15,16 +17,23 @@ import 'package:path_provider/path_provider.dart'; class ReadWrite { - static Future get_file() async + static Future get_history_file() async { Directory? directory = await getApplicationDocumentsDirectory(); return File("${directory.path}/history.json"); } + static Future get_first_time_file() async + { + Directory? directory = await getApplicationDocumentsDirectory(); + + return File("${directory.path}/first-time.json"); + } + static Future> read_history() async { - final file = await get_file(); + final file = await get_history_file(); List> tuple_history = []; @@ -63,7 +72,7 @@ class ReadWrite static Future writeTextFile(List history) async { - final file = await get_file(); + final file = await get_history_file(); var tuple_history = >[]; @@ -76,6 +85,42 @@ class ReadWrite return null; } + + static Future get_first_time() async + { + final file = await get_first_time_file(); + + DateTime first_time; + + try + { + var text = await file.readAsString(); + + List readable = jsonDecode(text); + + int year = readable[0]; + int month = readable[1]; + int day = readable[2]; + + first_time = DateTime(year, month, day); + + print('first time day found'); + } + on PathNotFoundException + { + print('no first time day found, assuming today'); + + DateTime now = DateTime.now(); + + List writeable = [now.year, now.month, now.day]; + + file.writeAsString(jsonEncode(writeable)); + + first_time = DateTime(now.year, now.month, now.day); + } + + return first_time; + } } void main() @@ -87,6 +132,8 @@ class MyAppState extends ChangeNotifier { var history = []; + DateTime first_time = DateTime(1999, 1, 1); + var percentage = 0.0; MyAppState() @@ -98,12 +145,22 @@ class MyAppState extends ChangeNotifier notifyListeners(); }); + + ReadWrite.get_first_time().then((x) { + first_time = x; + + recalculate_percentage(); + + notifyListeners(); + }); } void recalculate_percentage() { var now = DateTime.now(); + now = DateTime(now.year, now.month, now.day); + var cutoff = DateTime(now.year, now.month, now.day - 120); while (history.length > 0 && history[0].isBefore(cutoff)) @@ -111,14 +168,22 @@ class MyAppState extends ChangeNotifier history.removeAt(0); } - if (history.length > 0) - { - percentage = history.length / 120; - } - else + if (history.length == 0) { percentage = 0.0; } + else if (first_time.isAfter(cutoff)) + { + // print("line 177: ${history.length} / ${now.difference(first_time).inDays}"); + + percentage = history.length / max(now.difference(first_time).inDays, 1); + } + else + { + percentage = history.length / 120; + } + + // print("percentage = ${percentage}"); } void incrementCounter() async @@ -127,7 +192,7 @@ class MyAppState extends ChangeNotifier now = DateTime(now.year, now.month, now.day); - if (!history.contains(now)) + if (history.length == 0 || history[history.length - 1] != now) { history.add(now); @@ -145,7 +210,7 @@ class MyAppState extends ChangeNotifier now = DateTime(now.year, now.month, now.day); - if (history.contains(now)) + if (history.length > 0 && history[history.length - 1] == now) { history.remove(now); @@ -206,7 +271,18 @@ class MyHomePage extends StatelessWidget { var now = DateTime.now(); - var then = DateTime(now.year, now.month, now.day - 120); + var cutoff = DateTime(now.year, now.month, now.day - 120); + + DateTime startDate; + + if (appState.first_time.isAfter(cutoff)) + { + startDate = appState.first_time; + } + else + { + startDate = cutoff; + } var inverse_primary = theme.colorScheme.inversePrimary; @@ -231,14 +307,17 @@ class MyHomePage extends StatelessWidget { HeatMap( defaultColor: inverse_primary, colorMode: ColorMode.opacity, + startDate: startDate, endDate: now, - startDate: then, showText: false, scrollable: false, showColorTip: false, datasets: { + for (int i = 0; i < 120; i++) + DateTime(now.year, now.month, now.day - i): 1, + for (DateTime dt in appState.history) - dt: 1, + dt: 2, }, colorsets: { 1: primary, @@ -288,6 +367,10 @@ class MyHomePage extends StatelessWidget { ), ], ), + + // SizedBox(height: 10), + + // Text("(The denominator is ${now.difference(appState.first_time).inDays})"), ], ), ],