dart format
This commit is contained in:
parent
52c1c143b7
commit
62e91feb21
1 changed files with 53 additions and 91 deletions
|
|
@ -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<File> get_history_file() async
|
||||
{
|
||||
class ReadWrite {
|
||||
static Future<File> get_history_file() async {
|
||||
Directory? directory = await getApplicationDocumentsDirectory();
|
||||
|
||||
return File("${directory.path}/history.json");
|
||||
}
|
||||
|
||||
static Future<File> get_first_time_file() async
|
||||
{
|
||||
static Future<File> get_first_time_file() async {
|
||||
Directory? directory = await getApplicationDocumentsDirectory();
|
||||
|
||||
return File("${directory.path}/first-time.json");
|
||||
}
|
||||
|
||||
static Future<List<DateTime>> read_history() async
|
||||
{
|
||||
static Future<List<DateTime>> read_history() async {
|
||||
final file = await get_history_file();
|
||||
|
||||
List<List<int>> tuple_history = [];
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
try {
|
||||
var text = await file.readAsString();
|
||||
|
||||
|
||||
List<dynamic> temp = jsonDecode(text);
|
||||
|
||||
for (dynamic e in temp)
|
||||
{
|
||||
|
||||
for (dynamic e in temp) {
|
||||
List<dynamic> 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 = <DateTime>[];
|
||||
|
||||
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<Null> writeTextFile(List<DateTime> history) async
|
||||
{
|
||||
static Future<Null> writeTextFile(List<DateTime> history) async {
|
||||
final file = await get_history_file();
|
||||
|
||||
var tuple_history = <List<int>>[];
|
||||
|
||||
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<DateTime> get_first_time() async
|
||||
{
|
||||
static Future<DateTime> get_first_time() async {
|
||||
final file = await get_first_time_file();
|
||||
|
||||
DateTime first_time;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
try {
|
||||
var text = await file.readAsString();
|
||||
|
||||
|
||||
List<dynamic> 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<int> writeable = <int>[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>[];
|
||||
|
||||
|
||||
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: <Widget>[
|
||||
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'),
|
||||
),
|
||||
|
|
|
|||
Loading…
Reference in a new issue