Skip to content

Commit

Permalink
fix: export to calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
SungyeopJeong committed Jul 31, 2023
1 parent 5a81fc4 commit 3f082b0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 56 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:name="${applicationName}"
Expand Down
11 changes: 2 additions & 9 deletions lib/providers/timetable_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:otlplus/models/lecture.dart';
import 'package:otlplus/models/semester.dart';
import 'package:otlplus/models/timetable.dart';
import 'package:otlplus/models/user.dart';
import 'package:otlplus/utils/export_image.dart';
import 'package:otlplus/utils/export_file.dart';

class TimetableModel extends ChangeNotifier {
late User _user;
Expand Down Expand Up @@ -257,14 +257,7 @@ class TimetableModel extends ChangeNotifier {
options: Options(responseType: ResponseType.bytes),

Check warning on line 257 in lib/providers/timetable_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/timetable_model.dart#L257

Added line #L257 was not covered by tests
);

switch (type) {
case ShareType.image:
writeImage(response.data);
break;
case ShareType.ical:
// TODO
break;
}
writeFile(type, response.data);

Check warning on line 260 in lib/providers/timetable_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/providers/timetable_model.dart#L260

Added line #L260 was not covered by tests
return true;
} catch (exception) {
print(exception);
Expand Down
48 changes: 48 additions & 0 deletions lib/utils/export_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dart:io';
import 'dart:ui';

import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:otlplus/constants/url.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:open_app_file/open_app_file.dart';

const MethodChannel _channel = const MethodChannel("org.sparcs.otlplus");

Future<void> exportImage(RenderRepaintBoundary boundary) async {
final image = await boundary.toImage(pixelRatio: 3.0);
final byteData = await image.toByteData(format: ImageByteFormat.png);

Check warning on line 15 in lib/utils/export_file.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/export_file.dart#L13-L15

Added lines #L13 - L15 were not covered by tests

writeFile(ShareType.image, byteData?.buffer.asUint8List());

Check warning on line 17 in lib/utils/export_file.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/export_file.dart#L17

Added line #L17 was not covered by tests
}

Future<void> writeFile(ShareType type, Uint8List? bytes) async {

Check warning on line 20 in lib/utils/export_file.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/export_file.dart#L20

Added line #L20 was not covered by tests
final fileName =
"OTL-${DateTime.now().millisecondsSinceEpoch}.${type == ShareType.image ? 'png' : 'ics'}";

Check warning on line 22 in lib/utils/export_file.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/export_file.dart#L22

Added line #L22 was not covered by tests

if (Platform.isAndroid) {
if (await Permission.storage.request().isGranted) {

Check warning on line 25 in lib/utils/export_file.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/export_file.dart#L24-L25

Added lines #L24 - L25 were not covered by tests
switch (type) {
case ShareType.image:
_channel.invokeMethod("writeImageAsBytes", <String, dynamic>{

Check warning on line 28 in lib/utils/export_file.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/export_file.dart#L27-L28

Added lines #L27 - L28 were not covered by tests
"fileName": fileName,
"bytes": bytes,
});
break;
case ShareType.ical:
final directory = await getExternalStorageDirectory();

Check warning on line 34 in lib/utils/export_file.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/export_file.dart#L33-L34

Added lines #L33 - L34 were not covered by tests
final path =
"${directory?.path ?? '/storage/emulated/0/Android/data/org.sparcs.otlplus/files'}/$fileName";
File(path).writeAsBytesSync(bytes as List<int>);
OpenAppFile.open(path);

Check warning on line 38 in lib/utils/export_file.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/export_file.dart#L36-L38

Added lines #L36 - L38 were not covered by tests
break;
}
}
} else {
final directory = await getApplicationDocumentsDirectory();
final path = "${directory.path}/$fileName";
File(path).writeAsBytesSync(bytes as List<int>);
OpenAppFile.open(path);

Check warning on line 46 in lib/utils/export_file.dart

View check run for this annotation

Codecov / codecov/patch

lib/utils/export_file.dart#L43-L46

Added lines #L43 - L46 were not covered by tests
}
}
35 changes: 0 additions & 35 deletions lib/utils/export_image.dart

This file was deleted.

22 changes: 11 additions & 11 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ packages:
dependency: transitive
description:
name: ffi
sha256: "13a6ccf6a459a125b3fcdb6ec73bd5ff90822e071207c663bfd1f70062d51d18"
sha256: ed5337a5660c506388a9f012be0288fb38b49020ce2b45fe1f8b8323fe429f99
url: "https://pub.dev"
source: hosted
version: "1.2.1"
version: "2.0.2"
file:
dependency: transitive
description:
Expand Down Expand Up @@ -415,14 +415,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.2"
open_file_safe:
open_app_file:
dependency: "direct main"
description:
name: open_file_safe
sha256: "99a43b36307bf6946bdc3b54514bc5081af53f46feaec782c840a116b681647d"
name: open_app_file
sha256: "5dd0af054364c12093947ad3e69370bb22ca535ca17f81f7b72444c7be18cc01"
url: "https://pub.dev"
source: hosted
version: "3.2.3"
version: "4.0.2"
package_config:
dependency: transitive
description:
Expand Down Expand Up @@ -499,10 +499,10 @@ packages:
dependency: transitive
description:
name: path_provider_windows
sha256: a34ecd7fb548f8e57321fd8e50d865d266941b54e6c3b7758cf8f37c24116905
sha256: "1cb68ba4cd3a795033de62ba1b7b4564dace301f952de6bfb3cd91b202b6ee96"
url: "https://pub.dev"
source: hosted
version: "2.0.7"
version: "2.1.7"
permission_handler:
dependency: "direct main"
description:
Expand Down Expand Up @@ -944,10 +944,10 @@ packages:
dependency: transitive
description:
name: win32
sha256: c0e3a4f7be7dae51d8f152230b86627e3397c1ba8c3fa58e63d44a9f3edc9cef
sha256: f2add6fa510d3ae152903412227bda57d0d5a8da61d2c39c1fb022c9429a41c0
url: "https://pub.dev"
source: hosted
version: "2.6.1"
version: "5.0.6"
xdg_directories:
dependency: transitive
description:
Expand All @@ -973,5 +973,5 @@ packages:
source: hosted
version: "3.1.1"
sdks:
dart: ">=3.0.0-0 <4.0.0"
dart: ">=3.0.0 <4.0.0"
flutter: ">=3.7.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies:
flutter_web_browser: ^0.17.1
webview_flutter: ^3.0.4
webview_cookie_manager: ^2.0.6
open_file_safe: ^3.2.3
path_provider: ^2.0.12
permission_handler: ^8.3.0
provider: ^6.0.5
Expand All @@ -37,6 +36,7 @@ dependencies:
flutter_widgetkit: ^1.0.3
flutter_svg: ^2.0.7
dropdown_button2: ^2.1.4
open_app_file: ^4.0.2
# Firebase
firebase_core: ^2.8.0
firebase_analytics: ^10.1.6
Expand Down

0 comments on commit 3f082b0

Please sign in to comment.