Skip to content

Commit

Permalink
Handle scrolling to bottom on intro screen
Browse files Browse the repository at this point in the history
  • Loading branch information
cylonid committed Nov 12, 2022
1 parent 6a7079f commit 698aa50
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ android {
applicationId "com.cylonid.nativealpha"
minSdkVersion 26
targetSdkVersion 32
versionCode 1304
versionCode 1305
versionName "1.3.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/assets/news/latestUpdate_de.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<body>
<header>
<img width="230px" class="logo" src="../logo.svg">
<img class="logo" src="../logo.svg">
</header>
<div id="wrapper">
<aside></aside>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/assets/news/latestUpdate_en.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<body>
<header>
<img width="230px" class="logo" src="../logo.svg">
<img class="logo" src="../logo.svg">
</header>
<div id="wrapper">
<aside></aside>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/assets/news/news.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ header {
flex-direction: column;
align-items: center;
}

header img {
width: 100px;
}

#wrapper {
display: flex;
flex-direction: row;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,109 @@
package com.cylonid.nativealpha.activities;

import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewTreeObserver
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.cylonid.nativealpha.BuildConfig
import com.cylonid.nativealpha.R
import com.cylonid.nativealpha.model.DataManager
import com.cylonid.nativealpha.util.LocaleUtils
import com.cylonid.nativealpha.util.Utility
import kotlinx.android.synthetic.main.news_activity.*
import kotlin.properties.Delegates


class NewsActivity : AppCompatActivity() {
class NewsActivity : AppCompatActivity(), View.OnTouchListener, ViewTreeObserver.OnScrollChangedListener {

var btnDefaultBackgroundColor: Drawable? = null
var btnDefaultTextColor: Int = android.R.color.black

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.news_activity)

initializeUI()
setButtonState()
}


override fun onBackPressed() {}

private fun disableAcceptButton() {
btnNewsConfirm.isActivated = false
btnDefaultTextColor = btnNewsConfirm.currentTextColor
btnDefaultBackgroundColor = btnNewsConfirm.background

btnNewsConfirm.setBackgroundColor(
ContextCompat.getColor(
baseContext,
R.color.disabled_background_color
)
)
btnNewsConfirm.setTextColor(
ContextCompat.getColor(
baseContext,
R.color.disabled_text_color
)
)
btnNewsConfirm.setOnClickListener {
Utility.showToast(
this,
getString(R.string.scroll_to_bottom),
Toast.LENGTH_SHORT
)
}
}

private fun enableAcceptButton() {
btnNewsConfirm.isActivated = true
btnNewsConfirm.setTextColor(btnDefaultTextColor)
btnNewsConfirm.background = btnDefaultBackgroundColor

btnNewsConfirm.setOnClickListener { confirm() }
}

private fun initializeUI() {
setText()
btnNewsConfirm.setOnClickListener {
confirm()
}
}

private fun setButtonState() {
val vto: ViewTreeObserver = news_scrollchild.viewTreeObserver
vto.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
val height: Int = news_scrollchild.measuredHeight
if (height > 0) {
news_scrollchild.viewTreeObserver.removeOnGlobalLayoutListener(this)
if (news_scrollview.canScrollVertically(1) || news_scrollview.canScrollVertically(-1)) {
news_scrollview.setOnTouchListener(this@NewsActivity)
news_scrollview.viewTreeObserver.addOnScrollChangedListener(this@NewsActivity)
disableAcceptButton()
}
}
}
})
}

private fun setText() {
val fileId = intent.extras?.getString("text") ?: "latestUpdate"

news_content.loadUrl("file:///android_asset/news/" + fileId + "_" + LocaleUtils.fileEnding +".html")
if(DataManager.getInstance().eulaData) {
btnNewsConfirm.isEnabled = true
news_content.settings.javaScriptEnabled = true
news_content.webViewClient = NewsWebViewClient()
}
Expand All @@ -45,6 +114,19 @@ class NewsActivity : AppCompatActivity() {
DataManager.getInstance().lastShownUpdate = BuildConfig.VERSION_CODE
finish()
}

override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
return false
}

override fun onScrollChanged() {
val view = news_scrollview.getChildAt(news_scrollview.childCount - 1)
val bottomDetector: Int = view.bottom - (news_scrollview.height + news_scrollview.scrollY)

if (bottomDetector < 30) {
enableAcceptButton()
}
}
}

private class NewsWebViewClient : WebViewClient() {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/news_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

<ScrollView android:layout_height="wrap_content"
android:layout_weight="90"
android:id="@+id/news_scrollview"
android:layout_width="match_parent">
<LinearLayout
android:id="@+id/news_scrollchild"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,5 @@
<string name="no_biometric_keys_enrolled">Ihr Gerät unterstützt biometrische Authentifizierung, aber bisher wurde keine Aktivierungsmethode festgelegt.</string>
<string name="webview_activity_launch_failed">Web-App konnte nicht gestartet werden.</string>
<string name="no_url_entered">Bitte geben Sie eine URL an.</string>
<string name="scroll_to_bottom">Bitte scrollen Sie bis zum Ende.</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
<color name="transparent">#07000000</color>
<color name="snackbar_background">#CCCCCC</color>

<color name="disabled_background_color">#10181818</color>
<color name="disabled_text_color">#aaa</color>


</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<string name="no_biometric_keys_enrolled">Your device does support biometric authentication, but you did not enroll any method yet.</string>
<string name="webview_activity_launch_failed">Web App could not be started.</string>
<string name="no_url_entered">Please enter an URL.</string>
<string name="scroll_to_bottom">Please scroll to the bottom.</string>


</resources>

0 comments on commit 698aa50

Please sign in to comment.