Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGong2013 committed Nov 21, 2020
2 parents 5d64272 + b865cdd commit 54d1377
Show file tree
Hide file tree
Showing 29 changed files with 1,639 additions and 175 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# 0.4.1-pre.0
# 0.4.1-pre.1

* Support Activity animation
* Support Fragment

## 0.4.1-pre.0

* Remove Page.Hidden event
* Custom FlutterActivity
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
|___/
```

![Platform](https://img.shields.io/badge/platform-ios%7Candroid-green)
![Pub_Version](https://img.shields.io/pub/v/g_faraday?style=for-the-badge)
![Platform](https://img.shields.io/badge/platform-ios%7Candroid-green?style=for-the-badge)

一个`Flutter`混合开发解决方案

Expand All @@ -25,18 +26,21 @@ _Flutter **stable channel** 发布后 **一周内**适配发布对应的`g_farad

## Features

- [x] `iOS/Android` 原生页面堆栈与`Flutter Navigator`无缝桥接
- [x] iOS/Android原生页面堆栈与`Flutter Navigator`无缝桥接
- [x] 支持所有`Navigator`特性
- [x] [页面间回调](doc/callback.md)
- [x] [`iOS`导航条自动隐藏/显示](doc/ios_navigation_bar.md)
- [x] [页面间回调](docs/callback.md)
- [x] [iOS导航条自动隐藏/显示](docs/ios_navigation_bar.md)
- [x] iOS完美支持`push``present`
- [x] Android完美支持`Activity``Fragment`
- [ ] Android支持`FlutterView`
- [x] `WillPopScope`拦截滑动返回(ios)或者返回按键键(android)
- [x] [发送/接收全局通知](doc/notification.md)
- [ ] 监听页面生命周期
- [x] 完整的文档(7/9)
- [x] 完整的文档(7/10)

## Requirements

- Flutter 1.23.0-18.1.pre *flutter channel beta*
- Flutter 1.23.0-18.1.pre `beta channel`
- iOS 10.0+ Xcode 12.0+ Swift 5.1+
- Android minSdkVersion 16 Kotlin 1.4.10+

Expand All @@ -49,7 +53,7 @@ _Flutter **stable channel** 发布后 **一周内**适配发布对应的`g_farad
``` yaml
dependencies:
# 请确认与本地Flutter兼容的版本
g_faraday: ^0.4.0
g_faraday: ^0.4.1.pre.1
```
### Flutter 端集成
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ object Faraday {
CommonChannel(engine.dartExecutor, handler)
}

internal fun genPageId(): Int {
return nextCode.getAndIncrement()
}

/**
* The current flutter container Activity
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package com.yuxiaor.flutter.g_faraday

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
//import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.XFlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import java.io.Serializable
Expand All @@ -16,35 +13,36 @@ import java.io.Serializable
*/
class FaradayActivity : XFlutterActivity(), ResultProvider {

private var seqId: Int? = null
private val pageId by lazy { intent.getIntExtra(ID, 0) }
private var resultListener: ((requestCode: Int, resultCode: Int, data: Intent?) -> Unit)? = null

companion object {

private const val ARGS_KEY = "_flutter_args"
private const val ROUTE_KEY = "_flutter_route"
private const val ID = "_flutter_id"
private const val ARGS = "_flutter_args"
private const val ROUTE = "_flutter_route"

fun build(context: Context, routeName: String, params: Serializable? = null): Intent {
return Intent(context, FaradayActivity::class.java).apply {
putExtra(ROUTE_KEY, routeName)
putExtra(ARGS_KEY, params)
}
val pageId = Faraday.genPageId()
Faraday.plugin?.onPageCreate(routeName, params, pageId)
val intent = Intent(context, FaradayActivity::class.java)
intent.putExtra(ID, pageId)
intent.putExtra(ARGS, params)
intent.putExtra(ROUTE, routeName)
return intent
}
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
createFlutterPage()
Faraday.plugin?.onPageShow(pageId)
}

internal fun createFlutterPage() {
val route = intent.getStringExtra(ROUTE_KEY)
internal fun buildFlutterPage() {
val route = intent.getStringExtra(ROUTE)
require(route != null) { "route must not be null!" }
val args = intent.getSerializableExtra(ARGS_KEY)
Faraday.plugin?.onPageCreate(route, args, seqId) {
seqId = it
Faraday.plugin?.onPageShow(it)
}
val args = intent.getSerializableExtra(ARGS)
Faraday.plugin?.onPageCreate(route, args, pageId)
}

override fun provideFlutterEngine(context: Context): FlutterEngine? {
Expand All @@ -60,11 +58,11 @@ class FaradayActivity : XFlutterActivity(), ResultProvider {

override fun onResume() {
super.onResume()
seqId?.let { Faraday.plugin?.onPageShow(it) }
Faraday.plugin?.onPageShow(pageId)
}

override fun onDestroy() {
seqId?.let { Faraday.plugin?.onPageDealloc(it) }
Faraday.plugin?.onPageDealloc(pageId)
super.onDestroy()
}

Expand All @@ -77,5 +75,4 @@ class FaradayActivity : XFlutterActivity(), ResultProvider {
resultListener?.invoke(requestCode, resultCode, data)
resultListener = null
}

}
Original file line number Diff line number Diff line change
@@ -1,77 +1,84 @@
package com.yuxiaor.flutter.g_faraday

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.android.TransparencyMode
import io.flutter.embedding.android.XFlutterFragment
import io.flutter.embedding.engine.FlutterEngine

/**
* Author: Edward
* Date: 2020-09-07
* Description:
*/
class FaradayFragment : FlutterFragment(), ResultProvider {
class FaradayFragment : XFlutterFragment(), ResultProvider {

private var seqId: Int? = null
private val pageId by lazy { arguments?.getInt(ID) ?: 0 }
private var resultListener: ((requestCode: Int, resultCode: Int, data: Intent?) -> Unit)? = null

companion object {

private const val ARGS_KEY = "_flutter_args"
private const val ROUTE_KEY = "_flutter_route"
private const val ID = "_flutter_id"
private const val ARGS = "_flutter_args"
private const val ROUTE = "_flutter_route"

@JvmStatic
fun newInstance(routeName: String, params: HashMap<String, Any>? = null): FaradayFragment {
val pageId = Faraday.genPageId()
Faraday.plugin?.onPageCreate(routeName, params, pageId)
val bundle = Bundle().apply {
putString(ROUTE_KEY, routeName)
putSerializable(ARGS_KEY, params)
putInt(ID, pageId)
putString(ROUTE, routeName)
putSerializable(ARGS, params)
putString(ARG_FLUTTERVIEW_TRANSPARENCY_MODE, TransparencyMode.opaque.name)
}
return FaradayFragment().apply { arguments = bundle }
}
}

override fun onAttach(context: Context) {
createFlutterPage()
super.onAttach(context)
internal fun rebuild() {
val route = arguments?.getString(ROUTE)
require(route != null) { "route must not be null!" }
val args = arguments?.getSerializable(ARGS)
Faraday.plugin?.onPageCreate(route, args, pageId)
}

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

internal fun createFlutterPage() {
val route = arguments?.getString(ROUTE_KEY)
require(route != null) { "route must not be null!" }
val args = arguments?.getSerializable(ARGS_KEY)
Faraday.plugin?.onPageCreate(route, args, seqId) {
seqId = it
Faraday.plugin?.onPageShow(it)
override fun onStart() {
super.onStart()
if (!isHidden) {
Faraday.plugin?.onPageShow(pageId)
}
}

override fun onHiddenChanged(hidden: Boolean) {
super.onHiddenChanged(hidden)
if (!hidden) {
seqId?.let { Faraday.plugin?.onPageShow(it) }
Faraday.plugin?.onPageShow(pageId)
}
super.onHiddenChanged(hidden)
}

override fun onResume() {
super.onResume()
seqId?.let { Faraday.plugin?.onPageShow(it) }
if (!isHidden) {
Faraday.plugin?.onPageShow(pageId)
}
}

override fun onDetach() {
super.onDetach()
seqId?.let { Faraday.plugin?.onPageDealloc(it) }
Faraday.plugin?.onPageDealloc(pageId)
}

// override fun shouldAttachEngineToActivity(): Boolean {
// return true
// }
override fun shouldAttachEngineToActivity(): Boolean {
return true
}

override fun addResultListener(resultListener: (requestCode: Int, resultCode: Int, data: Intent?) -> Unit) {
this.resultListener = resultListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.yuxiaor.flutter.g_faraday

import androidx.annotation.NonNull
import androidx.fragment.app.FragmentActivity
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
Expand All @@ -21,7 +20,7 @@ class GFaradayPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
private var navigator: FaradayNavigator? = null
internal var binding: ActivityPluginBinding? = null

private var pageCount = 0;
private var pageCount = 0;

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) {
Expand Down Expand Up @@ -49,12 +48,12 @@ class GFaradayPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
"reCreateLastPage" -> {
when (val activity = Faraday.getCurrentActivity()) {
is FaradayActivity -> {
activity.createFlutterPage()
activity.buildFlutterPage()
}
is FragmentActivity -> {
val fragment = activity.supportFragmentManager.fragments.first { it.isVisible }
if (fragment is FaradayFragment) {
fragment.createFlutterPage()
fragment.rebuild()
}
}
}
Expand All @@ -64,17 +63,15 @@ class GFaradayPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}
}

internal fun onPageCreate(route: String, args: Any?, seq: Int?, callback: (seqId: Int) -> Unit) {
internal fun onPageCreate(route: String, args: Any?, id: Int) {
val data = hashMapOf<String, Any>()
data["name"] = route
if (args != null) {
data["args"] = args
}
data["seq"] = seq ?: -1
data["id"] = id
pageCount++
channel.invoke("pageCreate", data) {
callback.invoke(it as Int)
}
channel.invoke("pageCreate", data)
}

internal fun onPageShow(seqId: Int) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.flutter.embedding.android;

import android.animation.Animator;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.flutter.embedding.engine.FlutterEngine;

public class FlutterViewSnapshotSplashScreen implements SplashScreen {

@NonNull private final Bitmap flutterViewSnapshot;
@Nullable private View splashView;

public FlutterViewSnapshotSplashScreen(@NonNull FlutterEngine flutterEngine) {
flutterViewSnapshot = flutterEngine.getRenderer().getBitmap();
}

@Nullable
@Override
public View createSplashView(@NonNull Context context, @Nullable Bundle savedInstanceState) {
ImageView splash = new ImageView(context);
splash.setImageBitmap(flutterViewSnapshot);
splashView = splash;
return splash;
}

@Override
public void transitionToFlutter(@NonNull final Runnable onTransitionComplete) {
if (splashView == null) {
onTransitionComplete.run();
return;
}

splashView
.animate()
.alpha(0.0f)
.setDuration(500)
.setListener(
new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {}

@Override
public void onAnimationEnd(Animator animation) {
onTransitionComplete.run();
}

@Override
public void onAnimationCancel(Animator animation) {
onTransitionComplete.run();
}

@Override
public void onAnimationRepeat(Animator animation) {}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ protected void onStart() {
super.onStart();
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
if (delegate.isDetached()) {
delegate.reAttach();
delegate.reattach();
}
delegate.onStart();
}
Expand Down Expand Up @@ -610,7 +610,6 @@ protected void onDestroy() {
super.onDestroy();
if (stillAttachedForEvent("onDestroy")) {
release();
delegate = null;
}
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
}
Expand Down
Loading

0 comments on commit 54d1377

Please sign in to comment.