Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGong2013 committed Sep 10, 2020
0 parents commit d8b59df
Show file tree
Hide file tree
Showing 133 changed files with 4,837 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
.dart_tool/
.idea

.packages
.pub/

build/
.flutter-plugins
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: bbfbf1770cca2da7c82e887e4e4af910034800b6
channel: stable

project_type: plugin
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Flutter: Attach to Device",
"type": "dart",
"request": "attach",
"program": "example/lib/main.dart"
},
{
"name": "Flutter",
"program": "example/lib/main.dart",
"request": "launch",
"type": "dart"
}
]
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# g_faraday

## 快速集成

### 创建一个 flutter module 项目

```
flutter create -t module #{your_project_name}
```

### 在module目录下创建一个 flutter plugin 项目

```
cd #{your_project_name}
flutter create -t plugin --platforms ios,android faraday_helper
```

### 在flutter module项目中依赖g_faraday
```
```
8 changes: 8 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.flutter.plugins;

import io.flutter.plugin.common.PluginRegistry;
import com.yuxiaor.flutter.g_faraday.GFaradayPlugin;

/**
* Generated file. Do not edit.
*/
public final class GeneratedPluginRegistrant {
public static void registerWith(PluginRegistry registry) {
if (alreadyRegisteredWith(registry)) {
return;
}
GFaradayPlugin.registerWith(registry.registrarFor("com.yuxiaor.flutter.g_faraday.GFaradayPlugin"));
}

private static boolean alreadyRegisteredWith(PluginRegistry registry) {
final String key = GeneratedPluginRegistrant.class.getCanonicalName();
if (registry.hasPlugin(key)) {
return true;
}
registry.registrarFor(key);
return false;
}
}
43 changes: 43 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
group 'com.yuxiaor.flutter.g_faraday'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 16
}
lintOptions {
disable 'InvalidPackage'
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
4 changes: 4 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
5 changes: 5 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'g_faraday'
23 changes: 23 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.yuxiaor.flutter.g_faraday">

<application tools:node="merge">
<provider
android:name=".utils.NativeContextProvider"
android:authorities="${applicationId}.app.provider"
android:exported="false" />

<activity
android:name=".FaradayActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustResize" />

<meta-data
android:name="flutterEmbedding"
android:value="2" />

</application>
</manifest>
158 changes: 158 additions & 0 deletions android/src/main/kotlin/com/yuxiaor/flutter/g_faraday/Faraday.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package com.yuxiaor.flutter.g_faraday

import android.app.Activity
import android.content.Intent
import androidx.fragment.app.FragmentActivity
import com.yuxiaor.flutter.g_faraday.delegates.DefaultNavigatorDelegate
import com.yuxiaor.flutter.g_faraday.delegates.FaradayNavigator
import com.yuxiaor.flutter.g_faraday.plugins.ActivityAwarePlugin
import com.yuxiaor.flutter.g_faraday.plugins.ResultListener
import com.yuxiaor.flutter.g_faraday.utils.NativeContextProvider
import com.yuxiaor.flutter.g_faraday.utils.getArgs
import com.yuxiaor.flutter.g_faraday.utils.getFlutterArgs
import com.yuxiaor.flutter.g_faraday.utils.startForResult
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.plugins.FlutterPlugin
import java.io.Serializable
import java.util.concurrent.atomic.AtomicInteger

/**
* Author: Edward
* Date: 2020-08-31
* Description:
*/
object Faraday {

//native -> flutter intent args key
internal const val FLUTTER_ARGS_KEY = "_flutter_args_key"
const val ARGS_KEY = "_args_key_"
private val nextCode = AtomicInteger()
var navigator: FaradayNavigator? = null

private val activityAwarePlugin = ActivityAwarePlugin()
private val faradayPlugin = GFaradayPlugin()

@JvmStatic
val engine by lazy { FlutterEngine(NativeContextProvider.context) }

@JvmStatic
fun startFlutterEngine(navigatorDelegate: FaradayNavigator? = null) {
this.navigator = navigatorDelegate ?: DefaultNavigatorDelegate()
//注册插件
registerPlugin(faradayPlugin)
registerPlugin(activityAwarePlugin)
//开始执行dart代码,启动引擎
engine.dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
}

@JvmStatic
fun registerPlugin(plugin: FlutterPlugin) {
engine.plugins.add(plugin)
}

@JvmStatic
fun getCurrentActivity(): Activity? {
return activityAwarePlugin.binding?.activity
}

/**
* When jumped to native page form a flutter page with args,this method is provided to get the args.
* Or you can also call [Intent.getFlutterArgs()] to get args if you are using kotlin.
*/
@JvmStatic
fun getFlutterArgs(intent: Intent): Serializable? {
return intent.getFlutterArgs()
}


@JvmStatic
fun openFlutter(activity: FragmentActivity, url: String, vararg params: Pair<String, Any>, onActivityResult: ((data: HashMap<String, Any?>?) -> Unit)? = null) {
activity.openFlutter(url, *params) {
onActivityResult?.invoke(it)
}
}

@JvmStatic
fun openFlutter(activity: Activity, url: String, vararg params: Pair<String, Any>) {
activity.openFlutter(url, *params)
}

@JvmStatic
fun openFlutter(activity: Activity, url: String, requestCode: Int, vararg params: Pair<String, Any>) {
activity.openFlutter(url, requestCode, *params)
}

internal fun openActivityForResult(intent: Intent, callback: (result: HashMap<String, Any?>?) -> Unit) {
val nextRequestCode = nextCode.getAndIncrement()
getCurrentActivity()?.startActivityForResult(intent, nextRequestCode)
ResultListener(activityAwarePlugin) { requestCode, resultCode, data ->
if (requestCode == nextRequestCode && resultCode == Activity.RESULT_OK) {
callback.invoke(data?.getArgs())
}
}
}


internal fun onPageCreate(args: Any?, callback: (seqId: Int) -> Unit) {
faradayPlugin.onPageCreate(args, callback)
}

internal fun onPageShow(seqId: Int) {
faradayPlugin.onPageShow(seqId)
}

internal fun onPageHidden(seqId: Int) {
faradayPlugin.onPageHidden(seqId)
}

internal fun onPageDealloc(seqId: Int) {
faradayPlugin.onPageDealloc(seqId)
}
}


/**
* Native to flutter
* @param url flutter router
* @param params params from native to flutter
* @param onActivityResult callback for Activity Result
*/
fun FragmentActivity.openFlutter(url: String, vararg params: Pair<String, Any>, onActivityResult: ((data: HashMap<String, Any?>?) -> Unit)? = null) {
val args = hashMapOf(*params).apply { this["name"] = url }
val intent = Intent(this, FaradayActivity::class.java).putExtra(Faraday.FLUTTER_ARGS_KEY, args)
startForResult(intent) { resultCode, data ->
if (resultCode == Activity.RESULT_OK) {
onActivityResult?.invoke(data?.getArgs())
}
}
}


/**
* Native to flutter
* @param url flutter router
* @param params params from native to flutter
*
* no Activity result
*/
fun Activity.openFlutter(url: String, vararg params: Pair<String, Any>) {
val args = hashMapOf(*params).apply { this["name"] = url }
val intent = Intent(this, FaradayActivity::class.java).putExtra(Faraday.FLUTTER_ARGS_KEY, args)
startActivity(intent)
}


/**
* Native to flutter
* @param url flutter router
* @param params params from native to flutter
*
* you need to override [onActivityResult] in your Activity to get the result
*/
fun Activity.openFlutter(url: String, requestCode: Int, vararg params: Pair<String, Any>) {
val args = hashMapOf(*params).apply { this["name"] = url }
val intent = Intent(this, FaradayActivity::class.java).putExtra(Faraday.FLUTTER_ARGS_KEY, args)
startActivityForResult(intent, requestCode)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.yuxiaor.flutter.g_faraday

import android.content.Context
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

/**
* Author: Edward
* Date: 2020-09-01
* Description:
*/
class FaradayActivity : FlutterActivity() {

private var seqId = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//native -> flutter args
val args = intent.getSerializableExtra(Faraday.FLUTTER_ARGS_KEY)
Faraday.onPageCreate(args) {
seqId = it
}
}

override fun provideFlutterEngine(context: Context): FlutterEngine? {
return Faraday.engine
}

override fun onResume() {
super.onResume()
Faraday.onPageShow(seqId)
}


override fun onPause() {
super.onPause()
Faraday.onPageHidden(seqId)

}

override fun onDestroy() {
super.onDestroy()
Faraday.onPageDealloc(seqId)
}


}
Loading

0 comments on commit d8b59df

Please sign in to comment.