Skip to content

Commit

Permalink
feat: fall back to canvas preview for old notes
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Dec 15, 2023
1 parent cb383d2 commit 927d31b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/components/canvas/canvas_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:saber/data/editor/editor_core_info.dart';
import 'package:saber/data/editor/page.dart';
import 'package:saber/data/prefs.dart';

typedef _CacheItem = (EditorCoreInfo coreInfo, double pageHeight);

class CanvasPreview extends StatelessWidget {
const CanvasPreview({
super.key,
Expand All @@ -16,6 +18,41 @@ class CanvasPreview extends StatelessWidget {
final double? height;
final EditorCoreInfo coreInfo;

static final _previewCache = <String, Future<_CacheItem>>{};
static Widget fromFile({
Key? key,
required String filePath,
}) {
final Future<_CacheItem> future;
if (_previewCache.containsKey(filePath)) {
future = _previewCache[filePath]!;
} else {
_previewCache[filePath] = future = () async {
final coreInfo = await EditorCoreInfo.loadFromFilePath(filePath);
final pageHeight = coreInfo.pages.isNotEmpty
? coreInfo.pages[0].previewHeight(lineHeight: coreInfo.lineHeight)
: EditorPage.defaultHeight * 0.1;
return (coreInfo, pageHeight);
}();
}

return FutureBuilder(
key: key,
future: future,
builder: (context, snapshot) {
final data = snapshot.data;
if (data == null) {
return const CircularProgressIndicator.adaptive();
}
return CanvasPreview(
key: key,
coreInfo: data.$1,
height: data.$2,
);
},
);
}

@override
Widget build(BuildContext context) {
Size pageSize = coreInfo.pages.isNotEmpty
Expand Down
19 changes: 19 additions & 0 deletions lib/components/home/preview_card.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:async';
import 'dart:io';

import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:saber/components/canvas/canvas_preview.dart';
import 'package:saber/components/canvas/invert_shader.dart';
import 'package:saber/components/canvas/shader_sampler.dart';
import 'package:saber/components/home/uploading_indicator.dart';
Expand Down Expand Up @@ -114,6 +116,23 @@ class _PreviewCardState extends State<PreviewCard> {
child: Image(
key: ValueKey(thumbnailState.updateCount),
image: thumbnailImage,
errorBuilder: (context, error, stackTrace) {
// If the thumbnail image doesn't exist,
// (i.e. for old notes), render the first page.
if (error is! PathNotFoundException) {
throw error;
}

return FittedBox(
child: ClipRect(
child: CanvasPreview.fromFile(
key: ValueKey(
'CanvasPreview${thumbnailState.updateCount}'),
filePath: widget.filePath,
),
),
);
},
),
),
),
Expand Down

0 comments on commit 927d31b

Please sign in to comment.