Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/project-violet/violet into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Nov 4, 2023
2 parents 215f929 + eac14d1 commit b1b86e8
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 51 deletions.
81 changes: 81 additions & 0 deletions android/app/src/main/kotlin/xyz/project/violet/MainActivity.kt
@@ -1,5 +1,7 @@
package xyz.project.violet

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Environment
Expand All @@ -13,6 +15,8 @@ import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugins.GeneratedPluginRegistrant
import java.io.FileInputStream
import java.io.FileOutputStream

class MainActivity : FlutterFragmentActivity() {
private val VOLUME_CHANNEL = "xyz.project.violet/volume"
Expand Down Expand Up @@ -82,6 +86,7 @@ class MainActivity : FlutterFragmentActivity() {
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, MISC_CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"finishMainActivity" -> finishMainActivity(call, result)
"exportFile" -> exportFile(call, result)
else -> result.notImplemented()
}
}
Expand All @@ -100,4 +105,80 @@ class MainActivity : FlutterFragmentActivity() {
finish()
result.success(null)
}

private class ExportFileRequest(
val filePath: String,
val call: MethodCall,
val result: MethodChannel.Result,
)

private var nextExportFileRequestCode = 1000001;
private val exportFileRequestMap = hashMapOf<Int, ExportFileRequest>()

private fun exportFile(call: MethodCall, result: MethodChannel.Result) {
val filePath = call.argument<String>("filePath")
val mimeType = call.argument<String>("mimeType")
val fileNameToSaveAs = call.argument<String>("fileNameToSaveAs")

if (filePath == null) {
result.error("noArgument", "filePath", null)
return
}

if (mimeType == null) {
result.error("noArgument", "mimeType", null)
return
}

if (fileNameToSaveAs == null) {
result.error("noArgument", "fileNameToSaveAs", null)
return
}

try {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = mimeType
putExtra(Intent.EXTRA_TITLE, fileNameToSaveAs)
}

exportFileRequestMap[nextExportFileRequestCode] =
ExportFileRequest(filePath, call, result)
startActivityForResult(intent, nextExportFileRequestCode++);
} catch (e: Throwable) {
result.error("exception", e.toString(), e);
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

val request = exportFileRequestMap[requestCode] ?: return

try {
if (resultCode != Activity.RESULT_OK) {
request.result.error("intentResultFail", "resultCode=$resultCode", null)
return
}

try {
val uri = data!!.data!!
val targetFileDescriptor = contentResolver.openFileDescriptor(uri, "w")

val input = FileInputStream(request.filePath)
val output = FileOutputStream(targetFileDescriptor!!.fileDescriptor)

input.copyTo(output)

input.close()
output.close()

request.result.success(null)
} catch (e: Throwable) {
request.result.error("exception", e.toString(), e);
}
} finally {
exportFileRequestMap.remove(requestCode)
}
}
}
58 changes: 34 additions & 24 deletions lib/pages/settings/settings_page.dart
Expand Up @@ -60,6 +60,7 @@ import 'package:violet/pages/settings/tag_rebuild_page.dart';
import 'package:violet/pages/settings/tag_selector.dart';
import 'package:violet/pages/settings/version_page.dart';
import 'package:violet/pages/splash/splash_page.dart';
import 'package:violet/platform/misc.dart';
import 'package:violet/server/violet.dart';
import 'package:violet/settings/settings.dart';
import 'package:violet/style/palette.dart';
Expand Down Expand Up @@ -1994,36 +1995,45 @@ class _SettingsPageState extends State<SettingsPage>
title: Text(Translations.of(context).trans('exportingbookmark')),
trailing: const Icon(Icons.keyboard_arrow_right),
onTap: () async {
if (!await Permission.storage.isGranted) {
if (await Permission.storage.request() ==
PermissionStatus.denied) {
flutterToast.showToast(
child: ToastWrapper(
isCheck: false,
msg: Translations.of(context).trans('noauth'),
),
gravity: ToastGravity.BOTTOM,
toastDuration: const Duration(seconds: 4),
);
final dir = Platform.isIOS
? await getApplicationSupportDirectory()
: (await getApplicationDocumentsDirectory());
final bookmarkDatabaseFile = File('${dir.path}/user.db');

return;
}
}
if (Platform.isAndroid) {
await PlatformMiscMethods.instance.exportFile(
bookmarkDatabaseFile.path,
mimeType: 'application/vnd.sqlite3',
fileNameToSaveAs: 'violet-bookmarks.db',
);
} else {
if (!await Permission.storage.isGranted) {
if (await Permission.storage.request() ==
PermissionStatus.denied) {
flutterToast.showToast(
child: ToastWrapper(
isCheck: false,
msg: Translations.of(context).trans('noauth'),
),
gravity: ToastGravity.BOTTOM,
toastDuration: const Duration(seconds: 4),
);

final selectedPath = await FilePicker.platform.getDirectoryPath();
return;
}
}

if (selectedPath == null) {
return;
}
final selectedPath =
await FilePicker.platform.getDirectoryPath();

final db = Platform.isIOS
? await getApplicationSupportDirectory()
: (await getApplicationDocumentsDirectory());
final dbfile = File('${db.path}/user.db');
if (selectedPath == null) {
return;
}

final extpath = '$selectedPath/bookmark.db';
final extpath = '$selectedPath/bookmark.db';

await dbfile.copy(extpath);
await bookmarkDatabaseFile.copy(extpath);
}

flutterToast.showToast(
child: ToastWrapper(
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/viewer/others/preload_page_view.dart
Expand Up @@ -1014,7 +1014,7 @@ class PageScrollPhysics extends ScrollPhysics {
(velocity >= 0.0 && position.pixels >= position.maxScrollExtent)) {
return super.createBallisticSimulation(position, velocity);
}
final Tolerance tolerance = this.tolerance;
final Tolerance tolerance = toleranceFor(position);
final double target =
_getTargetPixels(position as ScrollPosition, tolerance, velocity);
if (target != position.pixels) {
Expand Down
19 changes: 19 additions & 0 deletions lib/platform/misc.dart
Expand Up @@ -16,4 +16,23 @@ class PlatformMiscMethods {

await _methodChannel.invokeMethod('finishMainActivity');
}

Future<void> exportFile(
String filePath, {
required String mimeType,
required String fileNameToSaveAs,
}) async {
if (!Platform.isAndroid) {
throw UnsupportedError('Android only');
}

await _methodChannel.invokeMethod<String>(
'exportFile',
<String, dynamic>{
'filePath': filePath,
'mimeType': mimeType,
'fileNameToSaveAs': fileNameToSaveAs,
},
);
}
}

0 comments on commit b1b86e8

Please sign in to comment.