Skip to content

Commit

Permalink
fixed critical problem with addressbar focus;
Browse files Browse the repository at this point in the history
added zoom controls to bottom bar;
  • Loading branch information
Fedir Tsapana committed Aug 11, 2020
1 parent 1f356a3 commit 489e982
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 36 deletions.
9 changes: 0 additions & 9 deletions .idea/misc.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/modules.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,33 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
ibFavorites.setOnClickListener { showFavorites() }
ibHistory.setOnClickListener { showHistory() }
ibSettings.setOnClickListener { showSettings() }
ibZoomIn.setOnClickListener {
viewModel.currentTab.value?.webView?.apply {
if (this.canZoomIn()) this.zoomIn()
onWebViewUpdated(viewModel.currentTab.value!!)
if (!this.canZoomIn()) {
ibZoomOut.requestFocus()
}
}
}
ibZoomOut.setOnClickListener {
viewModel.currentTab.value?.webView?.apply {
if (this.canZoomOut()) this.zoomOut()
onWebViewUpdated(viewModel.currentTab.value!!)
if (!this.canZoomOut()) {
ibZoomIn.requestFocus()
}
}
}

etUrl.onFocusChangeListener = etUrlFocusChangeListener

etUrl.setOnKeyListener(etUrlKeyListener)

ibForward.onFocusChangeListener = bottomButtonsFocusListener
ibBack.onFocusChangeListener = bottomButtonsFocusListener
ibRefresh.onFocusChangeListener = bottomButtonsFocusListener
ibHome.onFocusChangeListener = bottomButtonsFocusListener
ibCloseTab.onFocusChangeListener = bottomButtonsFocusListener
ibForward.setOnKeyListener(bottomButtonsKeyListener)
ibBack.setOnKeyListener(bottomButtonsKeyListener)
ibRefresh.setOnKeyListener(bottomButtonsKeyListener)
ibHome.setOnKeyListener(bottomButtonsKeyListener)
ibCloseTab.setOnKeyListener(bottomButtonsKeyListener)
llBottomPanel.childs.forEach {
it.onFocusChangeListener = bottomButtonsFocusListener
it.setOnKeyListener(bottomButtonsKeyListener)
}

settingsViewModel.uaString.observe(this, Observer<String> { uas ->
for (tab in viewModel.tabsStates) {
Expand Down Expand Up @@ -303,8 +315,8 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
if (keyEvent.action == KeyEvent.ACTION_UP) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(etUrl.windowToken, 0)
hideFloatAddressBar()
search(etUrl.text.toString())
hideFloatAddressBar()
viewModel.currentTab.value!!.webView?.requestFocus()
}
return@OnKeyListener true
Expand Down Expand Up @@ -517,8 +529,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
wv.setNetworkAvailable(Utils.isNetworkConnected(this))

etUrl.setText(newTab.currentOriginalUrl)
ibBack.isEnabled = wv.canGoBack() == true
ibForward.isEnabled = wv.canGoForward() == true
onWebViewUpdated(newTab)
}

@SuppressLint("SetJavaScriptEnabled")
Expand Down Expand Up @@ -809,8 +820,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
Log.d(TAG, "onPageStarted url: $url")
ibBack.isEnabled = tab.webView?.canGoBack() == true
ibForward.isEnabled = tab.webView?.canGoForward() == true
onWebViewUpdated(tab)
if (tab.webView?.url != null) {
tab.currentOriginalUrl = tab.webView?.url
} else if (url != null) {
Expand All @@ -827,8 +837,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
if (tab.webView == null || viewModel.currentTab.value == null || view == null) {
return
}
ibBack!!.isEnabled = tab.webView?.canGoBack() == true
ibForward!!.isEnabled = tab.webView?.canGoForward() == true
onWebViewUpdated(tab)

if (tab.webView?.url != null) {
tab.currentOriginalUrl = tab.webView?.url
Expand Down Expand Up @@ -892,6 +901,16 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
return webView
}

private fun onWebViewUpdated(tab: WebTabState) {
ibBack.isEnabled = tab.webView?.canGoBack() == true
ibForward.isEnabled = tab.webView?.canGoForward() == true
val zoomPossible = tab.webView?.canZoomIn() == true || tab.webView?.canZoomOut() == true
ibZoomIn.visibility = if (zoomPossible) View.VISIBLE else View.GONE
ibZoomOut.visibility = if (zoomPossible) View.VISIBLE else View.GONE
ibZoomIn.isEnabled = tab.webView?.canZoomIn() == true
ibZoomOut.isEnabled = tab.webView?.canZoomOut() == true
}

private fun showCertificateErrorPage(error: SslError) {
val tab = viewModel.currentTab.value ?: return
val webView = tab.webView ?: return
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/phlox/tvwebbrowser/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.phlox.tvwebbrowser.utils
import android.app.Activity
import android.content.ContextWrapper
import android.view.View
import android.view.ViewGroup
import java.util.*
import kotlin.collections.ArrayList

val View.activity: Activity?
get() {
Expand All @@ -23,4 +25,13 @@ fun Calendar.sameDay(other: Calendar): Boolean {
return this.get(Calendar.YEAR) == other.get(Calendar.YEAR) &&
this.get(Calendar.MONTH) == other.get(Calendar.MONTH) &&
this.get(Calendar.DAY_OF_MONTH) == other.get(Calendar.DAY_OF_MONTH)
}
}

val ViewGroup.childs: ArrayList<View>
get() {
val result = ArrayList<View>()
for (i in 0 until this.childCount) {
result.add(this.getChildAt(i))
}
return result
}
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_zoom_in_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"
android:fillColor="#212121"/>
<path
android:pathData="M12,10h-2v2H9v-2H7V9h2V7h1v2h2v1z"
android:fillColor="#212121"/>
</vector>
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_zoom_in_gray_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"
android:fillColor="#BDBDBD"/>
<path
android:pathData="M12,10h-2v2H9v-2H7V9h2V7h1v2h2v1z"
android:fillColor="#BDBDBD"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_zoom_out_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14zM7,9h5v1L7,10z"
android:fillColor="#212121"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_zoom_out_gray_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14zM7,9h5v1L7,10z"
android:fillColor="#BDBDBD"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/zoomin_icon_selector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/ic_zoom_in_gray_24dp" />
<item android:drawable="@drawable/ic_zoom_in_black_24dp" />
</selector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/zoomout_icon_selector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/ic_zoom_out_gray_24dp" />
<item android:drawable="@drawable/ic_zoom_out_black_24dp" />
</selector>
22 changes: 22 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,28 @@
android:layout_marginEnd="3dp"
android:contentDescription="@string/refresh_page" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ibZoomIn"
android:src="@drawable/zoomin_icon_selector"
android:background="@drawable/button_bg_selector"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:contentDescription="@string/zoom_in" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ibZoomOut"
android:src="@drawable/zoomout_icon_selector"
android:background="@drawable/button_bg_selector"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:contentDescription="@string/zoom_out" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<string name="navigate_home">Navigate home</string>
<string name="refresh_page">Refresh page</string>
<string name="voice_search">Voice search</string>
<string name="zoom_in">Zoom in</string>
<string name="zoom_out">Zoom out</string>
<string name="not_set">Not set</string>
<string name="action">Action:</string>
<string name="current_key">Current key:</string>
Expand Down

0 comments on commit 489e982

Please sign in to comment.