Skip to content

Commit

Permalink
add history
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoole committed Feb 14, 2024
1 parent c1ce41c commit cf5cc61
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 43 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@ Work Time | Break Time | Settings
- toggle display countdown
- toggle dark mode
- material 3 design
- store history in local storage

## Last Changes

- add local history storage
- add duration picker on settings page
- add toggle for different daily worktimes
- refactoring settings menu

## TODO

### History
- add history (button to store daily worktime)
- edit history
- delete history after certain time

### Misc
- add translations
- toggle timer via notification
Expand Down
78 changes: 52 additions & 26 deletions lib/screens/history.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:stempli_flutter/main.dart';

class HistoryScreen extends StatefulWidget {
const HistoryScreen({required this.title, super.key});
Expand All @@ -10,58 +12,82 @@ class HistoryScreen extends StatefulWidget {
}

class _HistoryScreenState extends State<HistoryScreen> {
List<String> _historyList = List.empty(growable: true);

@override
void initState() {
super.initState();
_loadHistory();
}

_deleteHistoryOlderThan1Month();
_loadHistory() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
_historyList = sharedPreferences.getStringList("history")!;
_deleteHistoryExceptLast30Days();
setState(() {});
}

_getHistoryItemWidget(String weekday, String date, String worktime) {
_getHistoryItemWidget(String date, String worktime) {
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(weekday),
Text(date),
Text(worktime),
],
);
}

_getItems() {
List<Widget> widgetList = List.empty(growable: true);

for (var item in _historyList) {
var split = item.split(" ");
widgetList.add(_getHistoryItemWidget(split[0], split[1]));
}

if (widgetList.isEmpty) {
widgetList.add(const Center(child: Text("\nNo Work = No History!")));
}

setState(() {});

return widgetList;
}

@override
Widget build(BuildContext context) {
List<Widget> _items = [];
_items.add(_getHistoryItemWidget("Mo", "05.02.24", "working"));
_items.add(const Divider());
_items.add(_getHistoryItemWidget("Fr", "02.02.24", "07:30h"));
_items.add(_getHistoryItemWidget("Th", "01.02.24", "07:30h"));
_items.add(_getHistoryItemWidget("We", "31.01.24", "07:30h"));
_items.add(_getHistoryItemWidget("Tu", "30.02.24", "07:30h"));
_items.add(_getHistoryItemWidget("Mo", "29.02.24", "07:30h"));
_items.add(const Divider());

return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: [
IconButton(
onPressed: () => _deleteHistory(), icon: const Icon(Icons.delete))
],
// actions: [
// IconButton(
// onPressed: () => _deleteHistory(), icon: const Icon(Icons.delete))
// ],
),
body: SafeArea(
child: ListView(
children: _items,
child: Column(
children: [
Expanded(
child: ListView(
children: _getItems(),
),
),
const SizedBox(
height: 8,
),
const Text("History only stores 30 days."),
const SizedBox(height: 64),
],
),
),
);
}
}

void _deleteHistoryOlderThan1Month() {
print("_deleteHistoryOlderThan1Month");
}

void _deleteHistory() {
print("deleteHistory() = deleteHistory()");
void _deleteHistoryExceptLast30Days() {
if (_historyList.length > 30) {
_historyList.removeAt(0);
sharedPreferences.setStringList("history", _historyList);
}
}
}
29 changes: 17 additions & 12 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,25 @@ class _HomeScreenState extends State<HomeScreen> {
sharedPreferences.setInt('breakTimeTotal', _breakTimeTotal);
sharedPreferences.setInt('lastToggleTimestamp', _lastToggleTimestamp);

// Feierabend
if (!_working) {
var today =
"${DateTime.now().year}-${DateTime.now().month}-${DateTime.now().day}";
var dataToSave = "$today $_workTimeTotal";

List<String> history = sharedPreferences.getStringList("history")!;
if (history.isNotEmpty && history.last.startsWith(today)) {
history.removeLast();
}
_saveToHistory(_workTimeTotal);
}

_saveToHistory(int worktime) {
_workTimeTotalString = _printFormattedTimeTotal(worktime);

history.add(dataToSave);
sharedPreferences.setStringList("history", history);
var day = DateTime.now().day.toString().padLeft(2, '0');
var month = DateTime.now().month.toString().padLeft(2, '0');

var today = "$day.$month.${DateTime.now().year}";
var dataToSave = "$today $_workTimeTotalString";

List<String> history = sharedPreferences.getStringList("history")!;
if (history.isNotEmpty && history.last.startsWith(today)) {
history.removeLast();
}

history.add(dataToSave);
sharedPreferences.setStringList("history", history);
}

Future<void> _displayTimer(Timer? timer) async {
Expand Down

0 comments on commit cf5cc61

Please sign in to comment.