Skip to content

Commit

Permalink
Open fiels via context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
deathblade666 committed Sep 2, 2024
1 parent 8de7429 commit 694c20e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 32 deletions.
69 changes: 38 additions & 31 deletions android/app/src/main/kotlin/com/example/mdeditor/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
package com.deathblade666.mdeditor

//import io.flutter.embedding.android.FlutterActivity

//class MainActivity: FlutterActivity()

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "markdown editor";
import io.flutter.embedding.android.FlutterActivity
import android.content.Intent
import android.os.Bundle
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import java.io.File

class MainActivity: FlutterActivity() {
private val CHANNEL = "Markdown_Editor_Channel"

var openPath: String? = null
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
val channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
channel.setMethodCallHandler { call, result ->
when (call.method) {
"getOpenFileUrl" -> {
result.success(openPath)
}
else -> result.notImplemented()
}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handleOpenFileUrl(intent)
}

new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler((call, result) -> {
if (call.method.equals("getFileUri")) {
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleOpenFileUrl(intent)
}

if (Intent.ACTION_VIEW.equals(action) && data != null) {
result.success(data.toString());
} else {
result.error("UNAVAILABLE", "File URI not available.", null);
}
} else {
result.notImplemented();
}
});
private fun handleOpenFileUrl(intent: Intent?) {
val path = intent?.data?.path
if (path != null) {
openPath = path
}
}
}

27 changes: 26 additions & 1 deletion lib/pages/split_edit.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:core';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:linked_scroll_controller/linked_scroll_controller.dart';
Expand Down Expand Up @@ -32,10 +33,15 @@ class editorState extends State<Editor> {
ScrollController scrollRenderController = ScrollController();
ScrollController userInputController = ScrollController();
bool toolBarToggle = enableToolBar;
static const platform = MethodChannel("markdown editor");
var importedFilePath = '';
var importedFileName = '';
var importedText = '';
static const platform = MethodChannel("Markdown_Editor_Channel");

@override
void initState() {
onStart();
getOpenFileUrl();
super.initState();
}

Expand Down Expand Up @@ -64,6 +70,25 @@ class editorState extends State<Editor> {
}
}

void getOpenFileUrl() async {
await platform.invokeMethod("getOpenFileUrl").then((fileUrl) async {
try {
importedFilePath = fileUrl as String;
String decodeUri = Uri.decodeFull(importedFilePath);
Uri fileUri = Uri.parse(decodeUri);
importedFilePath = fileUri.toFilePath().replaceFirst('/file://', '');
File file = File(importedFilePath);
importedText = await file.readAsString();
openFile.text = importedText;
setState(() {});
} catch (e) {
if (fileUrl == null) {
return;
}
}
});
}

void syncScroll (syncScrolling){
if (syncScrolling == true) {
setState(() {
Expand Down

0 comments on commit 694c20e

Please sign in to comment.