Skip to content

Commit 9f1ce33

Browse files
committed
新增趋势模块,采用Flutter与Android混合开发模式
1 parent 0e26500 commit 9f1ce33

File tree

15 files changed

+135
-21
lines changed

15 files changed

+135
-21
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ Model-View-ViewModel,View 指绿色的 Activity/Fragment,主要负责界面
8484
Glide相比起Fresco要轻量很多,api调用起来也很简洁,对图片加载要求不是很高的话建议使用Glide。
8585

8686
# 更新日志
87+
### v2.6
88+
* 添加趋势模块,该模块使用Flutter编写,主要学习Android与Flutter的混合开发模式以及消息通信方法,
89+
趋势模块对应的Flutter工程代码地址:https://github.com/fmtjava/flutter_module,
90+
不熟悉混合开发的小伙伴可参考:https://www.jianshu.com/p/1317aed6cd8c
8791
### v2.5
8892
* 使用ViewPager2替换ViewPager
8993
### v2.4

app/build.gradle

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ android {
1313
applicationId "com.fmt.github"
1414
minSdkVersion 21
1515
targetSdkVersion 29
16-
versionCode 12
17-
versionName "2.5"
16+
versionCode 13
17+
versionName "2.6"
1818
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1919
}
2020
signingConfigs {
@@ -53,17 +53,17 @@ dependencies {
5353
implementation fileTree(dir: 'libs', include: ['*.jar'])
5454
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
5555
//Androidx
56-
implementation 'androidx.appcompat:appcompat:1.1.0'
57-
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
56+
implementation 'androidx.appcompat:appcompat:1.2.0'
57+
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
5858
testImplementation 'junit:junit:4.13'
59-
androidTestImplementation 'androidx.test:runner:1.2.0'
60-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
59+
androidTestImplementation 'androidx.test:runner:1.3.0'
60+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
6161
implementation 'androidx.recyclerview:recyclerview:1.1.0'
62-
implementation 'com.google.android.material:material:1.1.0'
62+
implementation 'com.google.android.material:material:1.2.1'
6363

6464
//协程
65-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6'
66-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6'
65+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
66+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7'
6767

6868
//Android KTX 是一组 Kotlin 扩展程序,属于 Android Jetpack 系列
6969
implementation 'androidx.core:core-ktx:1.3.1'
@@ -87,7 +87,7 @@ dependencies {
8787
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"
8888

8989
//paging
90-
implementation "androidx.paging:paging-runtime:3.0.0-alpha03"
90+
implementation "androidx.paging:paging-runtime:3.0.0-alpha06"
9191

9292
//WorkManager
9393
implementation "androidx.work:work-runtime-ktx:2.4.0"
@@ -122,4 +122,7 @@ dependencies {
122122
implementation 'com.tencent.bugly:crashreport:3.2.33'
123123
implementation project(path: ':LaunchStarter')
124124

125+
//flutter
126+
implementation project(':flutter')
127+
125128
}

app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
android:supportsRtl="true"
2525
android:theme="@style/AppTheme"
2626
tools:ignore="GoogleAppIndexingWarning">
27+
<meta-data
28+
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
29+
android:value="true" />
2730
<activity
2831
android:name=".WelcomeActivity"
2932
android:theme="@style/SplashTheme">
@@ -52,11 +55,9 @@
5255
android:name=".home.activity.HomeActivity"
5356
android:screenOrientation="portrait"
5457
android:theme="@style/ToolBarTheme" />
55-
5658
<activity
5759
android:name=".home.activity.CommonSearchActivity"
5860
android:screenOrientation="portrait" />
59-
6061
<activity
6162
android:name=".repos.activity.ReposDetailActivity"
6263
android:screenOrientation="portrait" />
@@ -69,9 +70,16 @@
6970
<activity
7071
android:name=".user.activity.PhotoPreviewActivity"
7172
android:screenOrientation="portrait"
72-
android:theme="@style/FullScreenTheme" />
73-
74-
<!-- Android 7.0以上 照片、APK等下载保存路径-->
73+
android:theme="@style/FullScreenTheme" /> <!-- Android 7.0以上 照片、APK等下载保存路径 -->
74+
<activity
75+
android:name=".FlutterAppActivity"
76+
android:screenOrientation="portrait" />
77+
<activity
78+
android:name="io.flutter.embedding.android.FlutterActivity"
79+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
80+
android:hardwareAccelerated="true"
81+
android:windowSoftInputMode="adjustResize"
82+
/>
7583
<provider
7684
android:name="androidx.core.content.FileProvider"
7785
android:authorities="${applicationId}.provider"

app/src/main/java/com/fmt/github/App.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package com.fmt.github
22

33
import android.app.Application
44
import android.content.ContextWrapper
5-
import com.fmt.github.tasks.InitBuGlyTask
6-
import com.fmt.github.tasks.InitKoInTask
7-
import com.fmt.github.tasks.InitLiveEventBusTask
8-
import com.fmt.github.tasks.InitSmartRefreshLayoutTask
5+
import com.fmt.github.tasks.*
96
import com.fmt.launch.starter.TaskDispatcher
107

118
lateinit var mApplication: Application
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fmt.github
2+
3+
import android.os.Bundle
4+
import com.fmt.github.plugin.PluginManager
5+
import io.flutter.embedding.android.FlutterActivity
6+
7+
class FlutterAppActivity : FlutterActivity() {
8+
9+
var mInitParam: String? = null
10+
11+
companion object {
12+
const val INIT_PARAMS = "initParams"
13+
}
14+
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
PluginManager.registerWith(flutterEngine!!.dartExecutor, this)
18+
mInitParam = intent.getStringExtra(INIT_PARAMS)
19+
}
20+
21+
//设置显示Flutter对应页面的路由
22+
override fun getInitialRoute(): String =
23+
if (mInitParam.isNullOrEmpty()) super.getInitialRoute() else mInitParam!!
24+
}

app/src/main/java/com/fmt/github/base/fragment/BaseVMFragment.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.fmt.github.base.fragment
22

33
import android.content.Context
44
import android.os.Bundle
5-
import android.util.Log
65
import android.view.LayoutInflater
76
import android.view.View
87
import android.view.ViewGroup

app/src/main/java/com/fmt/github/constant/Constant.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ object Constant {
99
const val AUTHORIZATIONS = "authorizations"
1010
const val AUTHORIZATION = "Authorization"
1111
const val DOWNLOAD = "download"
12-
const val STAR_EVENT_KEY= "star_event"
12+
const val STAR_EVENT_KEY = "star_event"
13+
14+
object Router {
15+
const val ROUTER_TREND = "trend"
16+
}
1317
}

app/src/main/java/com/fmt/github/home/activity/HomeActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import androidx.core.content.ContextCompat
99
import androidx.lifecycle.Observer
1010
import androidx.lifecycle.lifecycleScope
1111
import androidx.navigation.Navigation
12+
import com.fmt.github.FlutterAppActivity
1213
import com.fmt.github.R
1314
import com.fmt.github.base.activity.BaseVMActivity
1415
import com.fmt.github.base.viewmodel.BaseViewModel
16+
import com.fmt.github.constant.Constant
1517
import com.fmt.github.data.storage.Preference
1618
import com.fmt.github.databinding.LayoutNavHeaderBinding
1719
import com.fmt.github.ext.getVersionName
@@ -96,6 +98,13 @@ class HomeActivity : BaseVMActivity(), NavigationView.OnNavigationItemSelectedLi
9698

9799
R.id.item_about -> startActivity<AboutActivity>(false)
98100

101+
R.id.item_trend -> {
102+
Bundle().run {
103+
putString(FlutterAppActivity.INIT_PARAMS, Constant.Router.ROUTER_TREND)
104+
startActivity<FlutterAppActivity>(this)
105+
}
106+
}
107+
99108
R.id.item_logout -> showLogoutPopup()
100109
}
101110
mDrawerLayout.closeDrawers()//选择菜单时,关闭侧滑菜单
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fmt.github.plugin
2+
3+
import android.app.Activity
4+
import com.fmt.github.config.Configs
5+
import com.fmt.github.repos.activity.ReposDetailActivity.Companion.OWNER
6+
import com.fmt.github.repos.activity.ReposDetailActivity.Companion.REPO
7+
import com.fmt.github.repos.activity.ReposDetailActivity.Companion.WEB_URL
8+
import com.fmt.github.repos.activity.go2ReposDetailActivity
9+
import io.flutter.plugin.common.BinaryMessenger
10+
import io.flutter.plugin.common.MethodCall
11+
import io.flutter.plugin.common.MethodChannel
12+
13+
/**
14+
* Android与Flutter通信管理类
15+
*/
16+
class PluginManager private constructor(val activity: Activity) : MethodChannel.MethodCallHandler {
17+
18+
companion object {
19+
fun registerWith(messenger: BinaryMessenger, activity: Activity) {
20+
val methodChannel = MethodChannel(messenger, "MethodChannelPlugin")
21+
val pluginManager = PluginManager(activity)
22+
methodChannel.setMethodCallHandler(pluginManager)
23+
}
24+
}
25+
26+
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
27+
when (call.method) {
28+
"go2ReposDetail" -> {
29+
go2ReposDetail(call.arguments<Map<String, String>>())
30+
//result.success() 如果需要回传数据给Flutter,可使用此方法
31+
}
32+
else -> result.notImplemented()
33+
}
34+
}
35+
36+
private fun go2ReposDetail(arguments: Map<String, String?>) {
37+
go2ReposDetailActivity(
38+
activity, "${Configs.GITHUB_BASE_URL}${arguments[WEB_URL]}",
39+
arguments[REPO] ?: error(""), arguments[OWNER] ?: error("")
40+
)
41+
}
42+
43+
}
44+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".FlutterAppActivity">
8+
9+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)