diff --git a/src/lib/homepage/body.dart b/src/lib/homepage/body.dart index 217201a..44255aa 100644 --- a/src/lib/homepage/body.dart +++ b/src/lib/homepage/body.dart @@ -6,6 +6,7 @@ import 'package:Junior/homepage/components/title.dart'; import 'package:Junior/model/changelog.dart'; import 'package:Junior/model/novel.dart'; import 'package:Junior/model/preferences.dart'; +import 'package:Junior/settings_page/components/export_data.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -41,17 +42,19 @@ class _HomePageState extends State { novelList = await getNovelList(); preferences = await loadPreferences(); + if(preferences.exportAutomatically) autoSave(); + searchList.addAll(novelList); sortBy(preferences.sortBy); } showChangelog(BuildContext context) async { SharedPreferences prefs = await SharedPreferences.getInstance(); - bool hasSeenChangelog = prefs.getBool('seenChangeLog-1.2.3') ?? false; + bool hasSeenChangelog = prefs.getBool('seenChangeLog-1.2.4') ?? false; if (!hasSeenChangelog) { showChangeDialog(context); - prefs.setBool('seenChangeLog-1.2.3', true); + prefs.setBool('seenChangeLog-1.2.4', true); } } @@ -157,4 +160,27 @@ class _HomePageState extends State { }); } } + + void autoSave() async { + bool moreThanADay(int milliseconds) { + return DateTime.now().millisecondsSinceEpoch - milliseconds > 86400000; + } + + SharedPreferences prefs = await SharedPreferences.getInstance(); + int milliseconds = prefs.getInt('lastSaved') ?? DateTime.now().millisecondsSinceEpoch - 86500000; + if(moreThanADay(milliseconds)) { + await exportData('autosaves/novelList-autosave-' + getRandomNumbers() + '.txt'); + showDialog( + context: context, + builder: (BuildContext context) => AlertDialog( + content: Text( + 'Autosaved Data to Download/Junior/autosaves/', + style: TextStyle(color: textColor), + ), + backgroundColor: tileColor.withAlpha(255), + ), + ); + prefs.setInt('lastSaved', DateTime.now().millisecondsSinceEpoch); + } + } } diff --git a/src/lib/model/changelog.dart b/src/lib/model/changelog.dart index be4753c..efd0d90 100644 --- a/src/lib/model/changelog.dart +++ b/src/lib/model/changelog.dart @@ -7,11 +7,11 @@ showChangeDialog(BuildContext context) { context: context, builder: (BuildContext context) => AlertDialog( title: Text( - 'Version 1.2.3', + 'Version 1.2.4', style: TextStyle(color: textColor), ), content: Text( - 'The previous update caused a lot of savefiles in Downloads. A fix for now is storing it all in a dedicated folder', + 'Along with it\'s dedicated folder, AutoSaves are now only saved once a day', style: TextStyle(color: textColor), ), backgroundColor: tileColor.withAlpha(255), diff --git a/src/lib/model/novel.dart b/src/lib/model/novel.dart index 837899a..2eb263d 100644 --- a/src/lib/model/novel.dart +++ b/src/lib/model/novel.dart @@ -1,8 +1,6 @@ import 'dart:convert'; import 'dart:io'; -import 'package:Junior/model/preferences.dart'; -import 'package:Junior/settings_page/components/export_data.dart'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; @@ -102,10 +100,6 @@ Future saveNovelList(novelList) async { final file = await _localFile; final novelListJSON = jsonEncode(novelList); - final prefs = await loadPreferences(); - if (prefs.exportAutomatically) - exportData('autosaves/novelList-autosave-' + getRandomNumbers() + '.txt'); - return file.writeAsString(novelListJSON); } diff --git a/src/lib/settings_page/body.dart b/src/lib/settings_page/body.dart index f83965c..effcac6 100644 --- a/src/lib/settings_page/body.dart +++ b/src/lib/settings_page/body.dart @@ -47,7 +47,7 @@ class _SettingsPageState extends State { backgroundColor: backgroundColor, body: SingleChildScrollView( child: Padding( - padding: EdgeInsets.only(top: 80, bottom: 20), + padding: EdgeInsets.only(top: 80), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ @@ -246,7 +246,7 @@ class _SettingsPageState extends State { }, ), ], - ) + ), ], ), ],