if 120 day haven't gone by yet, grade the user on how many days they have been using the app
This commit is contained in:
parent
7f222a9e31
commit
6a1cb62052
1 changed files with 96 additions and 13 deletions
|
|
@ -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<File> get_file() async
|
||||
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
|
||||
{
|
||||
Directory? directory = await getApplicationDocumentsDirectory();
|
||||
|
||||
return File("${directory.path}/first-time.json");
|
||||
}
|
||||
|
||||
static Future<List<DateTime>> read_history() async
|
||||
{
|
||||
final file = await get_file();
|
||||
final file = await get_history_file();
|
||||
|
||||
List<List<int>> tuple_history = [];
|
||||
|
||||
|
|
@ -63,7 +72,7 @@ class ReadWrite
|
|||
|
||||
static Future<Null> writeTextFile(List<DateTime> history) async
|
||||
{
|
||||
final file = await get_file();
|
||||
final file = await get_history_file();
|
||||
|
||||
var tuple_history = <List<int>>[];
|
||||
|
||||
|
|
@ -76,6 +85,42 @@ class ReadWrite
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
static Future<DateTime> get_first_time() async
|
||||
{
|
||||
final file = await get_first_time_file();
|
||||
|
||||
DateTime first_time;
|
||||
|
||||
try
|
||||
{
|
||||
var text = await file.readAsString();
|
||||
|
||||
List<dynamic> 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<int> writeable = <int>[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>[];
|
||||
|
||||
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})"),
|
||||
],
|
||||
),
|
||||
],
|
||||
|
|
|
|||
Loading…
Reference in a new issue