Skip to content

Commit

Permalink
Non fatal fixes (#228)
Browse files Browse the repository at this point in the history
* Hard code aspect ratio

* Delayed future instead of scheduled task

* Clone image in copy paster

* Save project resolution as int

* Prevent reopening the project when it is in the middle of freeing memory

* Bump version
  • Loading branch information
ruskakimov authored Aug 22, 2021
1 parent 984ee36 commit 947d23c
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 39 deletions.
3 changes: 2 additions & 1 deletion lib/common/data/copy_paster_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class CopyPasterModel extends ChangeNotifier {
ui.Image? _copiedImage;

void copyImage(ui.Image? image) {
_copiedImage = image;
_copiedImage?.dispose();
_copiedImage = image?.clone();
notifyListeners();
}

Expand Down
47 changes: 23 additions & 24 deletions lib/common/data/project/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,19 @@ class Project extends ChangeNotifier {
List<SoundClip> get soundClips => _soundClips;
List<SoundClip> _soundClips = [];

Size get frameSize => _frameSize;
late Size _frameSize;

late bool _shouldFreeMemory;
late int width;
late int height;

String? _saveDataOnDisk;
Timer? _autosaveTimer;

bool get isOpen => _scenes != null;
bool get isOpen => _open;
bool _open = false;

/// Loads project files into memory.
Future<void> open() async {
if (isOpen) {
// Prevent freeing memory after closing and quickly opening the project again.
_shouldFreeMemory = false;
return;
}
if (_open) return;
_open = true;

if (await _dataFile.exists()) {
await _openProjectFromDisk();
Expand All @@ -157,7 +153,8 @@ class Project extends ChangeNotifier {
directory.path,
_getSoundDirectoryPath(),
);
_frameSize = Size(data.width, data.height);
width = data.width;
height = data.height;
_scenes = Sequence<Scene>(data.scenes);

// TODO: Loading all frames into memory doesn't scale.
Expand All @@ -168,19 +165,21 @@ class Project extends ChangeNotifier {
}

Future<void> _openNewProject() async {
_frameSize = const Size(1280, 720);
width = 1280;
height = 720;
_scenes = Sequence<Scene>([await createNewScene()]);
_soundClips = [];
}

Future<void> saveAndClose() async {
_shouldFreeMemory = true;
_autosaveTimer?.cancel();
await save();

// User might have opened the project again while it was writing to disk.
if (_shouldFreeMemory) {
try {
_autosaveTimer?.cancel();
await save();
_freeMemory();
} catch (_) {
rethrow;
} finally {
_open = false;
}
}

Expand All @@ -197,8 +196,8 @@ class Project extends ChangeNotifier {
// Write thumbnail.
final image = await generateImage(
CompositeImagePainter(videoExportFrames.first.compositeImage),
_frameSize.width.toInt(),
_frameSize.height.toInt(),
width,
height,
);
await pngWrite(thumbnail, image);
image.dispose();
Expand All @@ -220,8 +219,8 @@ class Project extends ChangeNotifier {

String _generateSaveData() {
final data = ProjectSaveData(
width: _frameSize.width,
height: _frameSize.height,
width: width,
height: height,
scenes: _scenes!.iterable.toList(),
sounds: _soundClips,
);
Expand Down Expand Up @@ -262,8 +261,8 @@ class Project extends ChangeNotifier {
Future<Frame> createNewFrame() async {
final image = await generateImage(
null,
_frameSize.width.toInt(),
_frameSize.height.toInt(),
width,
height,
);
final file = _getFrameFile(DateTime.now().millisecondsSinceEpoch);
await pngWrite(file, image);
Expand Down
8 changes: 4 additions & 4 deletions lib/common/data/project/project_save_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ProjectSaveData {
Map<String, dynamic> json,
String frameDirPath,
String soundDirPath,
) : width = json[widthKey],
height = json[heightKey],
) : width = (json[widthKey] as num).toInt(),
height = (json[heightKey] as num).toInt(),
scenes = (json[scenesKey] as List<dynamic>)
.map((d) => Scene.fromJson(d, frameDirPath))
.toList(),
Expand All @@ -31,8 +31,8 @@ class ProjectSaveData {
soundsKey: sounds.map((d) => d.toJson()).toList(),
};

final double width;
final double height;
final int width;
final int height;
final List<Scene> scenes;
final List<SoundClip> sounds;

Expand Down
6 changes: 4 additions & 2 deletions lib/drawing/data/frame_reel_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:mooltik/common/data/project/project.dart';
Expand Down Expand Up @@ -68,9 +70,9 @@ class FrameReelModel extends ChangeNotifier {
void deleteCurrent() {
final removedFrame = frameSeq.removeAt(_currentIndex);

SchedulerBinding.instance?.scheduleTask(
Future.delayed(
Duration(seconds: 1),
() => removedFrame.dispose(),
Priority.idle,
);

_currentIndex = _currentIndex.clamp(0, frameSeq.length - 1);
Expand Down
4 changes: 2 additions & 2 deletions lib/drawing/data/reel_stack_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ class ReelStackModel extends ChangeNotifier {
reels.removeAt(layerIndex);
final removedLayer = _scene.layers.removeAt(layerIndex);

SchedulerBinding.instance?.scheduleTask(
Future.delayed(
Duration(seconds: 1),
() => removedLayer.dispose(),
Priority.idle,
);

if (layerIndex == _activeReelIndex) {
Expand Down
2 changes: 1 addition & 1 deletion lib/drawing/ui/frame_window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FrameWindow extends StatelessWidget {
Widget build(BuildContext context) {
return RepaintBoundary(
child: AspectRatio(
aspectRatio: frame.image.width! / frame.image.height!,
aspectRatio: 16 / 9,
child: ColoredBox(
color: frostedGlassColor,
child: CustomPaint(
Expand Down
4 changes: 2 additions & 2 deletions lib/editing/data/timeline_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ class TimelineViewModel extends ChangeNotifier {
final removedSliver =
selectedSliverSequence!.removeAt(_selectedSliverId!.spanIndex);

SchedulerBinding.instance?.scheduleTask(
Future.delayed(
Duration(seconds: 1),
() => removedSliver.dispose(),
Priority.idle,
);

removeSliverSelection();
Expand Down
1 change: 1 addition & 0 deletions lib/home/data/gallery_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class GalleryModel extends ChangeNotifier {
/// Returns a future when the current project has been closed and another project can be opened.
Future<void> openProject(Project project, BuildContext context) async {
if (_openedProject != null) return;
if (project.isOpen) return;

_openedProject = project;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.17.0
version: 1.17.1

environment:
sdk: '>=2.12.0 <3.0.0'
Expand Down
34 changes: 34 additions & 0 deletions test/project_data/a_v1_17.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"width": 1280,
"height": 720,
"scenes": [
{
"layers": [
{
"frames": [
{
"file_name": "frame0.png",
"duration": "0:00:03.000000"
},
{
"file_name": "frame1.png",
"duration": "0:00:01.000000"
}
],
"play_mode": 1,
"visible": true,
"name": "layer 1"
}
],
"duration": "0:00:04.000000",
"description": ""
}
],
"sounds": [
{
"file_name": "01234.mp3",
"start_time": "0:00:01.000000",
"duration": "0:00:02.000000"
}
]
}
125 changes: 125 additions & 0 deletions test/project_data/b_v1_17.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"width": 1280,
"height": 720,
"scenes": [
{
"layers": [
{
"frames": [
{
"file_name": "frame1620719884248.png",
"duration": "0:00:01.000000"
}
],
"play_mode": 0,
"visible": true,
"name": "top layer"
},
{
"frames": [
{
"file_name": "frame1620719994150.png",
"duration": "0:00:00.120000"
},
{
"file_name": "frame1620720004265.png",
"duration": "0:00:00.120000"
}
],
"play_mode": 1,
"visible": true,
"name": "middle layer"
},
{
"frames": [
{
"file_name": "frame1620722450872.png",
"duration": "0:00:01.000000"
}
],
"play_mode": 0,
"visible": true,
"name": "background"
}
],
"duration": "0:00:01.560000",
"description": "Hello"
},
{
"layers": [
{
"frames": [
{
"file_name": "frame1620727959131.png",
"duration": "0:00:01.000000"
}
],
"play_mode": 0,
"visible": true,
"name": "layer A"
},
{
"frames": [
{
"file_name": "frame1620727959144.png",
"duration": "0:00:00.120000"
},
{
"file_name": "frame1620727959153.png",
"duration": "0:00:00.120000"
}
],
"play_mode": 1,
"visible": false,
"name": "layer B"
},
{
"frames": [
{
"file_name": "frame1620727959161.png",
"duration": "0:00:01.000000"
}
],
"play_mode": 0,
"visible": true,
"name": null
}
],
"duration": "0:00:01.600000",
"description": "World"
},
{
"layers": [
{
"frames": [
{
"file_name": "frame1620728099764.png",
"duration": "0:00:00.160000"
},
{
"file_name": "frame1620728186420.png",
"duration": "0:00:00.160000"
}
],
"play_mode": 1,
"visible": true,
"name": ""
},
{
"frames": [
{
"file_name": "frame1620728082242.png",
"duration": "0:00:01.000000"
}
],
"play_mode": 0,
"visible": true,
"name": "😭"
}
],
"duration": "0:00:01.600000",
"description": "And friends"
}
],
"sounds": []
}
Loading

0 comments on commit 947d23c

Please sign in to comment.