Skip to content

Commit

Permalink
新增 WebFragment 界面
Browse files Browse the repository at this point in the history
  • Loading branch information
lelelongwang committed Jun 28, 2021
1 parent f547e49 commit 4655da0
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 25 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ WanJetpack
### 参考demo

- [Sunflower](https://github.com/android/sunflower)
- [architecture-components-samples](https://github.com/android/architecture-components-samples)
- [views-widgets-samples](https://github.com/android/views-widgets-samples)
- [user-interface-samples](https://github.com/android/user-interface-samples)
- [architecture-samples](https://github.com/android/architecture-samples)
- [compose-samples](https://github.com/android/compose-samples)

### 相关知识点

Expand Down Expand Up @@ -69,7 +74,7 @@ WanJetpack
- [条件导航](https://developer.android.google.cn/guide/navigation/navigation-conditional)
- [Fragment 间用 activityViewModels() 共享数据](https://developer.android.google.cn/topic/libraries/architecture/viewmodel)
- [在 NavGraph 中使用 ViewModel 共享数据](https://mp.weixin.qq.com/s/Hl8Yuf2bkDlVlgdB4M-wrw)
- [Safe Args 导航](https://developer.android.google.cn/guide/navigation/navigation-conditional)
- [Safe Args 导航](https://developer.android.google.cn/guide/navigation/navigation-pass-data)

- [Preference 库](https://developer.android.google.cn/jetpack/androidx/releases/preference?hl=zh_cn)
- [官方指南](https://developer.android.google.cn/guide/topics/ui/settings?hl=zh_cn)
Expand Down Expand Up @@ -147,4 +152,9 @@ WanJetpack

- Cookie
- [CookieManager](https://developer.android.google.cn/reference/kotlin/android/webkit/CookieManager)
- 本demo中,和收藏相关都需要登录操作,建议登录将返回的cookie(其中包含账号、密码)持久化到本地即可。
- 本demo中,和收藏相关都需要登录操作,建议登录将返回的cookie(其中包含账号、密码)持久化到本地即可。

- [Webview 库](https://developer.android.google.cn/jetpack/androidx/releases/webkit)
- [官方文档](https://developer.android.google.cn/guide/webapps/webview)
- [官方demo](https://github.com/android/views-widgets-samples/tree/main/WebView)
- 本demo中跳转到WebFragment是通过 Bundle 传递参数,**没有**用通过 Navigation 的 Safe Args 导航实现
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ dependencies {
implementation "com.github.bumptech.glide:glide:4.11.0"
kapt "com.github.bumptech.glide:compiler:4.11.0"

//WebView
implementation("androidx.webkit:webkit:1.4.0")

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.navigation.findNavController
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.longjunhao.wanjetpack.R
import com.longjunhao.wanjetpack.adapter.CollectionArticleAdapter.ApiArticleViewHolder
import com.longjunhao.wanjetpack.data.ApiArticle
import com.longjunhao.wanjetpack.databinding.ListItemCollectionBinding
Expand Down Expand Up @@ -42,9 +45,8 @@ class CollectionArticleAdapter : PagingDataAdapter<ApiArticle, ApiArticleViewHol
init {
binding.setClickListener {
binding.article?.let { homeArticle ->
val uri = Uri.parse(homeArticle.link)
val intent = Intent(Intent.ACTION_VIEW, uri)
it.context.startActivity(intent)
val bundle = bundleOf("link" to homeArticle.link, "title" to homeArticle.title)
it.findNavController().navigate(R.id.webFragment, bundle)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.longjunhao.wanjetpack.adapter

import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.navigation.findNavController
Expand Down Expand Up @@ -78,9 +77,8 @@ class HomeArticleAdapter(
init {
binding.setClickListener {
binding.article?.let { homeArticle ->
val uri = Uri.parse(homeArticle.link)
val intent = Intent(Intent.ACTION_VIEW, uri)
it.context.startActivity(intent)
val bundle = bundleOf("link" to homeArticle.link, "title" to homeArticle.title)
it.findNavController().navigate(R.id.webFragment, bundle)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.longjunhao.wanjetpack.R
import com.longjunhao.wanjetpack.data.home.ApiBanner
import com.longjunhao.wanjetpack.databinding.ListItemBannerBinding

Expand Down Expand Up @@ -52,9 +55,8 @@ class BannerViewHolder(
init {
binding.bannerImage.setOnClickListener {
binding.banner?.let { bannerItem ->
val uri = Uri.parse(bannerItem.url)
val intent = Intent(Intent.ACTION_VIEW, uri)
it.context.startActivity(intent)
val bundle = bundleOf("link" to bannerItem.url, "title" to bannerItem.title)
it.findNavController().navigate(R.id.webFragment, bundle)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.navigation.findNavController
Expand Down Expand Up @@ -74,9 +75,8 @@ class ProjectAdapter(
init {
binding.setClickListener {
binding.project?.let { projectArticle ->
val uri = Uri.parse(projectArticle.link)
val intent = Intent(Intent.ACTION_VIEW, uri)
it.context.startActivity(intent)
val bundle = bundleOf("link" to projectArticle.link, "title" to projectArticle.title)
it.findNavController().navigate(R.id.webFragment, bundle)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.navigation.findNavController
Expand Down Expand Up @@ -75,9 +76,8 @@ class SearchAdapter(
init {
binding.setClickListener {
binding.article?.let { homeArticle ->
val uri = Uri.parse(homeArticle.link)
val intent = Intent(Intent.ACTION_VIEW, uri)
it.context.startActivity(intent)
val bundle = bundleOf("link" to homeArticle.link, "title" to homeArticle.title)
it.findNavController().navigate(R.id.webFragment, bundle)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.navigation.findNavController
Expand Down Expand Up @@ -74,9 +75,8 @@ class WechatAdapter(
init {
binding.setClickListener {
binding.wechat?.let { wechat ->
val uri = Uri.parse(wechat.link)
val intent = Intent(Intent.ACTION_VIEW, uri)
it.context.startActivity(intent)
val bundle = bundleOf("link" to wechat.link, "title" to wechat.title)
it.findNavController().navigate(R.id.webFragment, bundle)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.navigation.findNavController
Expand Down Expand Up @@ -74,9 +75,8 @@ class WendaAdapter(
init {
binding.setClickListener {
binding.wenda?.let { wenda ->
val uri = Uri.parse(wenda.link)
val intent = Intent(Intent.ACTION_VIEW, uri)
it.context.startActivity(intent)
val bundle = bundleOf("link" to wenda.link, "title" to wenda.title)
it.findNavController().navigate(R.id.webFragment, bundle)
}
}
}
Expand Down
114 changes: 114 additions & 0 deletions app/src/main/java/com/longjunhao/wanjetpack/ui/web/WebFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.longjunhao.wanjetpack.ui.web

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import androidx.navigation.findNavController
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewFeature
import com.longjunhao.wanjetpack.databinding.FragmentWebBinding
import dagger.hilt.android.AndroidEntryPoint

/**
* todo:
* 1. 没有用webViewClient,没有仔细研究webViewClient的作用
* 2. 深色主题下,加载WebFragment页面的过程中,界面是白色的,待优化适配,且需要加上加载进度条
* 3. 某些文章的界面显示内容后瞬间内容有消失,不知什么原因,待分析。eg:link:https://www.jianshu.com/p/ded52111cc31 title:Android 四大组件通信核心
* 4. 本页面需要新增FloatingActionButton,实现收藏功能
* 5. WebFragment 打开、关闭动画没有实现
* 6. WebFragment暂时没有对应的ViewModel,不确定后期还需不需要加上
*/
@AndroidEntryPoint
class WebFragment : Fragment() {

private lateinit var binding: FragmentWebBinding

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentWebBinding.inflate(inflater,container,false)
subscribeUi()
return binding.root
}

private fun subscribeUi() {
binding.toolbar.setNavigationOnClickListener { view ->
view.findNavController().navigateUp()
}

binding.toolbarText.text = arguments?.getString("title")

configureWebView(arguments?.getString("link") ?: "")
}

@SuppressLint("SetJavaScriptEnabled")
private fun configureWebView(link: String) {

// Configuring Dark Theme
// *NOTE* : The force dark setting is not persistent. You must call the static
// method every time your app process is started.
// *NOTE* : The change from day<->night mode is a
// configuration change so by default the activity will be restarted
// (and pickup the new values to apply the theme). Take care when overriding this
// default behavior to ensure this method is still called when changes are made.
val nightModeFlag = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
// Check if the system is set to light or dark mode
if (nightModeFlag == Configuration.UI_MODE_NIGHT_YES) {
// Switch WebView to dark mode; uses default dark theme
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(
binding.webView.settings,
WebSettingsCompat.FORCE_DARK_ON
)
}

/* Set how WebView content should be darkened. There are three options for how to darken
* a WebView.
* PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING- checks for the "color-scheme" <meta> tag.
* If present, it uses media queries. If absent, it applies user-agent (automatic)
* darkening DARK_STRATEGY_WEB_THEME_DARKENING_ONLY - uses media queries always, even
* if there's no "color-scheme" <meta> tag present.
* DARK_STRATEGY_USER_AGENT_DARKENING_ONLY - it ignores web page theme and always
* applies user-agent (automatic) darkening.
* More information about Force Dark Strategy can be found here:
* https://developer.android.com/reference/androidx/webkit/WebSettingsCompat#setForceDarkStrategy(android.webkit.WebSettings,%20int)
*/
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) {
WebSettingsCompat.setForceDarkStrategy(
binding.webView.settings,
WebSettingsCompat.DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
)
}
}

// Setup debugging; See https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews for reference
/*if (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0) {
WebView.setWebContentsDebuggingEnabled(true)
}*/
WebView.setWebContentsDebuggingEnabled(true)

// Enable Javascript
binding.webView.settings.javaScriptEnabled = true

binding.webView.loadUrl(link)

}


// Creating the custom WebView Client Class
/*private class MyWebViewClient(private val assetLoader: WebViewAssetLoader) :
WebViewClientCompat() {
override fun shouldInterceptRequest(
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
return assetLoader.shouldInterceptRequest(request.url)
}
}*/
}
59 changes: 59 additions & 0 deletions app/src/main/res/layout/fragment_web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

</data>

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.web.WebFragment">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/Theme.WanJetpack.AppBarOverlay">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_back_no_circle"
app:contentInsetStart="0dp"
app:layout_collapseMode="parallax">

<TextView
android:id="@+id/toolbarText"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:gravity="center"
android:text=""
android:singleLine="true"
android:ellipsize="end"
android:textAppearance="?attr/textAppearanceHeadline5"/>

</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
tools:context=".ui.web.WebFragment">

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
6 changes: 6 additions & 0 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,10 @@
android:id="@+id/searchFragment"
android:name="com.longjunhao.wanjetpack.ui.home.SearchFragment"
android:label="SearchFragment" />

<fragment
android:id="@+id/webFragment"
android:name="com.longjunhao.wanjetpack.ui.web.WebFragment"
android:label="fragment_web"
tools:layout="@layout/fragment_web" />
</navigation>

0 comments on commit 4655da0

Please sign in to comment.