Skip to content

Commit

Permalink
new file management
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Oct 25, 2023
1 parent c770a7b commit a347419
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 75 deletions.
2 changes: 1 addition & 1 deletion assets/l10n/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ timetable:
background:
title: 壁纸
opacity: 背景不透明度
pickTip: 选择你最喜欢的图片
pickTip: 选择你喜欢的图片
repeat:
title: 重复图像
desc: 重复图像作为填充壁纸的图案
Expand Down
2 changes: 1 addition & 1 deletion assets/l10n/zh-TW.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ timetable:
background:
title: 桌布
opacity: 背景不透明度
pickTip: 選取你最喜愛的影像
pickTip: 選取你喜愛的影像
repeat:
title: 重複影像
desc: 重複影像作為圖案來填滿桌布
Expand Down
40 changes: 40 additions & 0 deletions lib/files.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:io';

import 'package:path/path.dart';

class Files {
const Files._();

static late final Directory temp;
static late final Directory cache;
static late final Directory internal;
static late final Directory user;

static Directory get screenshot => temp.subDir("screenshot");

static const timetable = TimetableFiles();

static Future<void> init() async {
await screenshot.create(recursive: true);
await timetable.init();
}
}

extension DirectoryX on Directory {
File subFile(String p1, [String? p2, String? p3, String? p4]) => File(join(path, p1, p2, p3, p4));

Directory subDir(String p1, [String? p2, String? p3, String? p4]) => Directory(join(path, p1, p2, p3, p4));
}

class TimetableFiles {
const TimetableFiles();

File get screenshotFile => Files.screenshot.subFile("timetable.png");
File get backgroundFile => Files.user.subFile("timetable", "background.png");

Directory get calendarDir => Files.user.subDir("timetable", "calendar");

Future<void> init() async {
await calendarDir.create(recursive: true);
}
}
47 changes: 0 additions & 47 deletions lib/fs/manager.dart

This file was deleted.

2 changes: 1 addition & 1 deletion lib/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ class Init {

static Future<void> _init() async {
debugPrint("Initializing");
await initStorage();
// Initialize the window size before others for a better experience when loading.
if (UniversalPlatform.isDesktop) {
await DesktopInit.init();
}
_registerEditor();
await initStorage();
if (UniversalPlatform.isDesktop) {
final lastWindowSize = Settings.lastWindowSize;
if (lastWindowSize != null) {
Expand Down
9 changes: 7 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sit/files.dart';
import 'package:sit/init.dart';
import 'package:sit/migration/migrations.dart';
import 'package:sit/school/yellow_pages/entity/contact.dart';
Expand All @@ -21,8 +22,12 @@ void main() async {
// debugPaintSizeEnabled = true;
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
R.appDir = await getApplicationDocumentsDirectory();
R.tmpDir = await getTemporaryDirectory();
Files.cache = await getApplicationCacheDirectory();
Files.temp = await getTemporaryDirectory();
Files.internal = await getApplicationSupportDirectory();
Files.user = await getApplicationDocumentsDirectory();
await Files.init();

R.roomList = await _loadRoomNumberList();
R.userAgents = await _loadUserAgents();
R.yellowPages = await _loadYellowPages();
Expand Down
3 changes: 0 additions & 3 deletions lib/r.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:io';
import 'dart:ui';

import 'package:flutter/foundation.dart';
Expand All @@ -25,8 +24,6 @@ class R {
];
static const appId = "life.mysit.SITLife";
static const appName = "SIT Life";
static late final Directory appDir;
static late final Directory tmpDir;
static late List<String> roomList;
static late List<String> userAgents;

Expand Down
4 changes: 4 additions & 0 deletions lib/timetable/page/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:rettulf/rettulf.dart';
import 'package:sit/design/adaptive/foundation.dart';
import 'package:sit/design/widgets/card.dart';
import 'package:sit/design/widgets/common.dart';
import 'package:sit/files.dart';
import 'package:sit/settings/settings.dart';
import 'package:sit/timetable/entity/background.dart';
import "../i18n.dart";
Expand Down Expand Up @@ -59,7 +60,10 @@ class _TimetableBackgroundEditorState extends State<TimetableBackgroundEditor> w
PlatformTextButton(
child: i18n.save.text(),
onPressed: () async {
final backgroundFi = await File(background.path).copy(Files.timetable.backgroundFile.path);
await FileImage(backgroundFi).evict();
Settings.timetable.backgroundImage = background;
if (!mounted) return;
context.pop(background);
},
),
Expand Down
32 changes: 18 additions & 14 deletions lib/timetable/page/screenshot.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:go_router/go_router.dart';
import 'package:open_file/open_file.dart';
import 'package:path/path.dart' show join;
import 'package:rettulf/rettulf.dart';
import 'package:screenshot/screenshot.dart';
import 'package:sit/design/adaptive/foundation.dart';
import 'package:sit/fs/manager.dart';
import 'package:sit/r.dart';
import 'package:sit/files.dart';
import 'package:sit/timetable/entity/timetable.dart';
import "../i18n.dart";
import '../widgets/style.dart';
Expand All @@ -33,10 +29,12 @@ class TimetableScreenshotConfigEditor extends StatefulWidget {
});

@override
State<TimetableScreenshotConfigEditor> createState() => _TimetableScreenshotConfigEditorState();
State<TimetableScreenshotConfigEditor> createState() =>
_TimetableScreenshotConfigEditorState();
}

class _TimetableScreenshotConfigEditorState extends State<TimetableScreenshotConfigEditor> {
class _TimetableScreenshotConfigEditorState
extends State<TimetableScreenshotConfigEditor> {
final $signature = TextEditingController(text: "");
late bool grayOutTakenLessons = widget.initialGrayOut;
var enableBackground = true;
Expand Down Expand Up @@ -166,7 +164,9 @@ class TimetableWeeklyScreenshotFilm extends StatelessWidget {
Widget buildBody(BuildContext context, TimetableStyleData style) {
final today = DateTime.now();
return [
buildTitle().text(style: context.textTheme.titleLarge).padSymmetric(v: 10),
buildTitle()
.text(style: context.textTheme.titleLarge)
.padSymmetric(v: 10),
TimetableOneWeek(
fullSize: fullSize,
timetable: timetable,
Expand All @@ -175,7 +175,9 @@ class TimetableWeeklyScreenshotFilm extends StatelessWidget {
return StyledCourseCell(
style: style,
course: lesson.course,
grayOut: config.grayOutTakenLessons ? lesson.type.endTime.isBefore(today) : false,
grayOut: config.grayOutTakenLessons
? lesson.type.endTime.isBefore(today)
: false,
);
},
),
Expand All @@ -197,10 +199,12 @@ Future<void> takeTimetableScreenshot({
required SitTimetableEntity timetable,
required int weekIndex,
}) async {
final config = await context.show$Sheet$<TimetableScreenshotConfig>((ctx) => TimetableScreenshotConfigEditor(
timetable: timetable,
initialGrayOut: TimetableStyle.of(context).cellStyle.grayOutTakenLessons,
));
final config = await context.show$Sheet$<TimetableScreenshotConfig>(
(ctx) => TimetableScreenshotConfigEditor(
timetable: timetable,
initialGrayOut:
TimetableStyle.of(context).cellStyle.grayOutTakenLessons,
));
if (config == null) return;
if (!context.mounted) return;
var fullSize = context.mediaQuery.size;
Expand All @@ -227,7 +231,7 @@ Future<void> takeTimetableScreenshot({
context: context,
pixelRatio: View.of(context).devicePixelRatio,
);
final imgFi = R.tmpDir.sub("screenshot.png");
final imgFi = Files.timetable.screenshotFile;
await imgFi.writeAsBytes(screenshot);

await OpenFile.open(imgFi.path, type: "image/png");
Expand Down
6 changes: 3 additions & 3 deletions lib/timetable/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:flutter/widgets.dart';
import 'package:ical/serializer.dart';
import 'package:open_file/open_file.dart';
import 'package:sit/design/adaptive/multiplatform.dart';
import 'package:sit/fs/manager.dart';
import 'package:sit/files.dart';
import 'package:sit/l10n/extension.dart';
import 'package:sit/school/entity/school.dart';
import 'package:sanitize_filename/sanitize_filename.dart';
Expand Down Expand Up @@ -204,7 +204,7 @@ Future<void> exportTimetableFileAndShare(
}) async {
final content = jsonEncode(timetable.toJson());
final fileName = sanitizeFilename("${timetable.name}.timetable", replacement: "-");
final timetableFi = R.tmpDir.sub(fileName);
final timetableFi = Files.temp.subFile(fileName);
final sharePositionOrigin = context.getSharePositionOrigin();
await timetableFi.writeAsString(content);
await Share.shareXFiles(
Expand All @@ -226,7 +226,7 @@ Future<void> exportTimetableAsICalendarAndOpen(
required TimetableExportCalendarConfig config,
}) async {
final fileName = _getICalFileName(context, timetable);
final calendarFi = R.appDir.sub(fileName);
final calendarFi = Files.timetable.calendarDir.subFile(fileName);
final data = convertTimetable2ICal(timetable: timetable, config: config);
await calendarFi.writeAsString(data);
// final url = Uri.encodeFull("data:text/calendar,$data");
Expand Down
6 changes: 3 additions & 3 deletions lib/timetable/widgets/timetable/board.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:rettulf/rettulf.dart';
import 'package:sit/files.dart';
import 'package:sit/timetable/entity/background.dart';
import 'package:sit/timetable/widgets/style.dart';

Expand Down Expand Up @@ -94,7 +93,8 @@ class _TimetableBackgroundState extends State<TimetableBackground> with SingleTi
Widget build(BuildContext context) {
final bk = widget.background;
return Image.file(
File(bk.path),
key: ValueKey(bk.path),
Files.timetable.backgroundFile,
opacity: $opacity,
filterQuality: bk.antialias ? FilterQuality.low : FilterQuality.none,
repeat: bk.repeat ? ImageRepeat.repeat : ImageRepeat.noRepeat,
Expand Down

0 comments on commit a347419

Please sign in to comment.