Skip to content

Commit 120c9fb

Browse files
committed
仓库详情页面代码优化
1 parent 2c6192d commit 120c9fb

File tree

3 files changed

+105
-69
lines changed

3 files changed

+105
-69
lines changed

app/src/main/java/com/fmt/github/repos/activity/ReposDetailActivity.kt

+9-19
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import android.app.Activity
44
import android.os.Bundle
55
import android.view.KeyEvent
66
import android.view.View
7-
import android.widget.LinearLayout
8-
import androidx.core.content.ContextCompat
97
import androidx.lifecycle.Observer
108
import com.fmt.github.R
119
import com.fmt.github.base.activity.BaseVMActivity
@@ -14,9 +12,9 @@ import com.fmt.github.constant.Constant
1412
import com.fmt.github.ext.*
1513
import com.fmt.github.ext.startActivity
1614
import com.fmt.github.home.event.ReposStarEvent
15+
import com.fmt.github.repos.delegate.WebDelegate
1716
import com.fmt.github.repos.viewmodel.ReposViewModel
1817
import com.jeremyliao.liveeventbus.LiveEventBus
19-
import com.just.agentweb.AgentWeb
2018
import com.like.LikeButton
2119
import com.like.OnLikeListener
2220
import kotlinx.android.synthetic.main.activity_repos_detail.*
@@ -25,7 +23,7 @@ import org.koin.androidx.viewmodel.ext.android.viewModel
2523
class ReposDetailActivity : BaseVMActivity() {
2624

2725
private val mViewModel: ReposViewModel by viewModel()
28-
private lateinit var mAgentWeb: AgentWeb
26+
private lateinit var mWebDelegate: WebDelegate
2927
private lateinit var mOwner: String
3028
private lateinit var mRepos: String
3129
private lateinit var mWebUrl: String
@@ -42,24 +40,16 @@ class ReposDetailActivity : BaseVMActivity() {
4240
mOwner = intent.getStringExtra(OWNER)
4341
mRepos = intent.getStringExtra(REPO)
4442
mWebUrl = intent.getStringExtra(WEB_URL)
45-
initAgentWeb()
43+
mWebDelegate = WebDelegate.create(this, mRootView, mWebUrl)
44+
mWebDelegate.onCreate()
4645
initListener()
4746
}
4847

4948
override fun getViewModel(): BaseViewModel = mViewModel
5049

51-
private fun initAgentWeb() {
52-
mAgentWeb = AgentWeb.with(this)
53-
.setAgentWebParent(mRootView, LinearLayout.LayoutParams(-1, -1))
54-
.useDefaultIndicator(ContextCompat.getColor(this, R.color.indicator_color))
55-
.createAgentWeb()
56-
.ready()
57-
.go(mWebUrl)
58-
}
59-
6050
private fun initListener() {
6151
mBackIB.setOnClickListener {
62-
mAgentWeb.back().no {
52+
mWebDelegate.back().no {
6353
finish()
6454
}
6555
}
@@ -104,22 +94,22 @@ class ReposDetailActivity : BaseVMActivity() {
10494
}
10595

10696
override fun onPause() {
107-
mAgentWeb.webLifeCycle.onPause()
97+
mWebDelegate.onPause()
10898
super.onPause()
10999
}
110100

111101
override fun onResume() {
112-
mAgentWeb.webLifeCycle.onResume()
102+
mWebDelegate.onResume()
113103
super.onResume()
114104
}
115105

116106
override fun onDestroy() {
117-
mAgentWeb.webLifeCycle.onDestroy()
107+
mWebDelegate.onDestroy()
118108
super.onDestroy()
119109
}
120110

121111
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean =
122-
(mAgentWeb.handleKeyEvent(keyCode, event)).yes {
112+
(mWebDelegate.handleKeyEvent(keyCode, event)).yes {
123113
true
124114
}.otherwise {
125115
super.onKeyDown(keyCode, event)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fmt.github.repos.delegate
2+
3+
import android.app.Activity
4+
import android.view.KeyEvent
5+
import android.view.ViewGroup
6+
import android.widget.LinearLayout
7+
import androidx.core.content.ContextCompat
8+
import com.fmt.github.R
9+
import com.just.agentweb.AgentWeb
10+
11+
class WebDelegate(
12+
private val mActivity: Activity,
13+
private val mViewGroup: ViewGroup,
14+
private val mWebUrl: String
15+
) {
16+
17+
private lateinit var mAgentWeb: AgentWeb
18+
19+
companion object {
20+
fun create(activity: Activity, viewGroup: ViewGroup, webUrl: String): WebDelegate =
21+
WebDelegate(activity, viewGroup, webUrl)
22+
}
23+
24+
fun onCreate() {
25+
mAgentWeb = AgentWeb.with(mActivity)
26+
.setAgentWebParent(mViewGroup, LinearLayout.LayoutParams(-1, -1))
27+
.useDefaultIndicator(ContextCompat.getColor(mActivity, R.color.indicator_color))
28+
.createAgentWeb()
29+
.ready()
30+
.go(mWebUrl)
31+
}
32+
33+
fun onPause() {
34+
mAgentWeb.webLifeCycle.onPause()
35+
}
36+
37+
fun onResume() {
38+
mAgentWeb.webLifeCycle.onResume()
39+
}
40+
41+
fun onDestroy() {
42+
mAgentWeb.webLifeCycle.onDestroy()
43+
}
44+
45+
fun back() = mAgentWeb.back()
46+
47+
fun handleKeyEvent(keyCode: Int, event: KeyEvent) = mAgentWeb.handleKeyEvent(keyCode, event)
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,52 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
android:id="@+id/mRootView"
5-
android:orientation="vertical"
6-
android:layout_width="match_parent"
7-
android:layout_height="match_parent">
8-
9-
<RelativeLayout
10-
android:layout_width="match_parent"
11-
android:layout_height="@dimen/dp_48"
12-
android:background="@color/colorPrimary">
13-
14-
<ImageButton
15-
android:id="@+id/mBackIB"
16-
android:layout_width="wrap_content"
17-
android:layout_height="wrap_content"
18-
android:src="@mipmap/icon_back"
19-
android:layout_centerVertical="true"
20-
android:paddingStart="@dimen/dp_15"
21-
android:background="@null"
22-
/>
23-
24-
<com.like.LikeButton
25-
android:id="@+id/mFavorIb"
26-
app:icon_type="heart"
27-
app:icon_size="@dimen/dp_20"
28-
app:unlike_drawable="@mipmap/icon_favorite_uncheck"
29-
app:like_drawable="@mipmap/icon_favorite_check"
30-
android:layout_alignParentEnd="true"
31-
android:layout_centerVertical="true"
32-
android:layout_width="wrap_content"
33-
android:layout_height="wrap_content"
34-
android:visibility="gone"
35-
/>
36-
37-
<TextView
38-
android:id="@+id/mReposNameTv"
39-
android:layout_width="wrap_content"
40-
android:layout_height="wrap_content"
41-
android:textColor="@android:color/white"
42-
android:layout_toRightOf="@id/mBackIB"
43-
android:layout_toLeftOf="@id/mFavorIb"
44-
android:singleLine="true"
45-
android:ellipsize="start"
46-
android:textSize="@dimen/sp_20"
47-
android:gravity="center"
48-
android:layout_centerInParent="true"
49-
/>
50-
51-
52-
</RelativeLayout>
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:id="@+id/mRootView"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical">
8+
9+
<RelativeLayout
10+
android:layout_width="match_parent"
11+
android:layout_height="@dimen/dp_48"
12+
android:background="@color/colorPrimary">
13+
14+
<ImageButton
15+
android:id="@+id/mBackIB"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:layout_centerVertical="true"
19+
android:background="@null"
20+
android:paddingStart="@dimen/dp_15"
21+
android:src="@mipmap/icon_back" />
22+
23+
<com.like.LikeButton
24+
android:id="@+id/mFavorIb"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_alignParentEnd="true"
28+
android:layout_centerVertical="true"
29+
android:visibility="gone"
30+
app:icon_size="@dimen/dp_20"
31+
app:icon_type="heart"
32+
app:like_drawable="@mipmap/icon_favorite_check"
33+
app:unlike_drawable="@mipmap/icon_favorite_uncheck" />
34+
35+
<TextView
36+
android:id="@+id/mReposNameTv"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:layout_centerInParent="true"
40+
android:layout_toStartOf="@id/mFavorIb"
41+
android:layout_toEndOf="@id/mBackIB"
42+
android:ellipsize="start"
43+
android:gravity="center"
44+
android:singleLine="true"
45+
android:textColor="@android:color/white"
46+
android:textSize="@dimen/sp_20" />
47+
48+
49+
</RelativeLayout>
5350

5451

5552
</LinearLayout>

0 commit comments

Comments
 (0)