From 62e91feb21915b95797431e9d7e1154b4b6ed606 Mon Sep 17 00:00:00 2001 From: Zander Thannhauser Date: Thu, 1 May 2025 19:58:58 -0500 Subject: [PATCH] dart format --- my_app/lib/main.dart | 144 ++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 91 deletions(-) diff --git a/my_app/lib/main.dart b/my_app/lib/main.dart index 922e96c..d0db449 100644 --- a/my_app/lib/main.dart +++ b/my_app/lib/main.dart @@ -1,4 +1,3 @@ - import 'dart:convert'; import 'dart:io'; @@ -14,37 +13,30 @@ import 'package:flutter_heatmap_calendar/flutter_heatmap_calendar.dart'; import 'package:path_provider/path_provider.dart'; - -class ReadWrite -{ - static Future get_history_file() async - { +class ReadWrite { + static Future get_history_file() async { Directory? directory = await getApplicationDocumentsDirectory(); return File("${directory.path}/history.json"); } - static Future get_first_time_file() async - { + static Future get_first_time_file() async { Directory? directory = await getApplicationDocumentsDirectory(); return File("${directory.path}/first-time.json"); } - static Future> read_history() async - { + static Future> read_history() async { final file = await get_history_file(); List> tuple_history = []; - - try - { + + try { var text = await file.readAsString(); - + List temp = jsonDecode(text); - - for (dynamic e in temp) - { + + for (dynamic e in temp) { List l = e; int a = l[0]; @@ -55,29 +47,25 @@ class ReadWrite } print('history found, and it has ${tuple_history.length} elements'); - } - on PathNotFoundException { + } on PathNotFoundException { print('no history found'); } var history = []; - for (var e in tuple_history) - { + for (var e in tuple_history) { history.add(DateTime(e[0], e[1], e[2])); } return history; } - static Future writeTextFile(List history) async - { + static Future writeTextFile(List history) async { final file = await get_history_file(); var tuple_history = >[]; - for (DateTime dt in history) - { + for (DateTime dt in history) { tuple_history.add([dt.year, dt.month, dt.day]); } @@ -86,18 +74,16 @@ class ReadWrite return null; } - static Future get_first_time() async - { + static Future get_first_time() async { final file = await get_first_time_file(); DateTime first_time; - - try - { + + try { var text = await file.readAsString(); - + List readable = jsonDecode(text); - + int year = readable[0]; int month = readable[1]; int day = readable[2]; @@ -105,11 +91,9 @@ class ReadWrite first_time = DateTime(year, month, day); print('first time day found'); - } - on PathNotFoundException - { + } on PathNotFoundException { print('no first time day found, assuming today'); - + DateTime now = DateTime.now(); List writeable = [now.year, now.month, now.day]; @@ -123,21 +107,18 @@ class ReadWrite } } -void main() -{ +void main() { runApp(const MyApp()); } -class MyAppState extends ChangeNotifier -{ +class MyAppState extends ChangeNotifier { var history = []; - + DateTime first_time = DateTime(1999, 1, 1); var percentage = 0.0; - MyAppState() - { + MyAppState() { ReadWrite.read_history().then((x) { history = x; @@ -155,45 +136,36 @@ class MyAppState extends ChangeNotifier }); } - void recalculate_percentage() - { + 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)) - { + while (history.length > 0 && history[0].isBefore(cutoff)) { history.removeAt(0); } - if (history.length == 0) - { + if (history.length == 0) { percentage = 0.0; - } - else if (first_time.isAfter(cutoff)) - { + } else if (first_time.isAfter(cutoff)) { // print("line 177: ${history.length} / ${now.difference(first_time).inDays}"); percentage = history.length / (now.difference(first_time).inDays + 1); - } - else - { + } else { percentage = history.length / 120; } // print("percentage = ${percentage}"); } - void incrementCounter() async - { + void incrementCounter() async { var now = DateTime.now(); now = DateTime(now.year, now.month, now.day); - if (history.length == 0 || history[history.length - 1] != now) - { + if (history.length == 0 || history[history.length - 1] != now) { history.add(now); await ReadWrite.writeTextFile(history); @@ -204,14 +176,12 @@ class MyAppState extends ChangeNotifier notifyListeners(); } - void decrementCounter() async - { + void decrementCounter() async { var now = DateTime.now(); now = DateTime(now.year, now.month, now.day); - if (history.length > 0 && history[history.length - 1] == now) - { + if (history.length > 0 && history[history.length - 1] == now) { history.remove(now); await ReadWrite.writeTextFile(history); @@ -222,8 +192,7 @@ class MyAppState extends ChangeNotifier notifyListeners(); } - void clear_counter() - { + void clear_counter() { history.clear(); recalculate_percentage(); @@ -232,7 +201,6 @@ class MyAppState extends ChangeNotifier } } - class MyApp extends StatelessWidget { const MyApp({super.key}); @@ -240,17 +208,15 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return ChangeNotifierProvider( - create: (context) => new MyAppState(), - - child: MaterialApp( - title: 'Flutter Demo', - theme: ThemeData( - colorScheme: ColorScheme.fromSeed(seedColor: Colors.red), - useMaterial3: true, - ), - home: const MyHomePage(title: 'Habit Tracker (v0.01)'), - ) - ); + create: (context) => new MyAppState(), + child: MaterialApp( + title: 'Flutter Demo', + theme: ThemeData( + colorScheme: ColorScheme.fromSeed(seedColor: Colors.red), + useMaterial3: true, + ), + home: const MyHomePage(title: 'Habit Tracker (v0.01)'), + )); } } @@ -275,12 +241,9 @@ class MyHomePage extends StatelessWidget { DateTime startDate; - if (appState.first_time.isAfter(cutoff)) - { + if (appState.first_time.isAfter(cutoff)) { startDate = appState.first_time; - } - else - { + } else { startDate = cutoff; } @@ -293,16 +256,13 @@ class MyHomePage extends StatelessWidget { return Scaffold( appBar: AppBar( backgroundColor: inverse_primary, - title: Text(title), ), - body: Center( child: ListView( children: [ Column( mainAxisAlignment: MainAxisAlignment.center, - children: [ HeatMap( defaultColor: inverse_primary, @@ -315,9 +275,7 @@ class MyHomePage extends StatelessWidget { datasets: { for (int i = 0; i < 120; i++) DateTime(now.year, now.month, now.day - i): 1, - - for (DateTime dt in appState.history) - dt: 2, + for (DateTime dt in appState.history) dt: 2, }, colorsets: { 1: primary, @@ -355,13 +313,17 @@ class MyHomePage extends StatelessWidget { mainAxisSize: MainAxisSize.min, children: [ ElevatedButton.icon( - onPressed: () async { appState.decrementCounter(); }, + onPressed: () async { + appState.decrementCounter(); + }, icon: Icon(Icons.remove), label: Text('Failure'), ), SizedBox(width: 10), ElevatedButton.icon( - onPressed: () async { appState.incrementCounter(); }, + onPressed: () async { + appState.incrementCounter(); + }, icon: Icon(Icons.add), label: Text('Success'), ),