Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
hadithmv committed Dec 29, 2024
1 parent 474c485 commit d0caf16
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 3 deletions.
33 changes: 33 additions & 0 deletions androidApp-kt/addedFolder/MainActivity files/MainActivity-1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.hadithmv.hmv

import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.ComponentActivity

class MainActivity : ComponentActivity() {
private val applicationUrl = "file:///android_asset/index.html"

@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)


val webView = WebView(this).apply {
webViewClient = WebViewClient()
settings.apply {
javaScriptEnabled = true
allowFileAccess = true
domStorageEnabled = true
// added
allowFileAccessFromFileURLs = true
// https://developer.android.com/reference/android/webkit/WebSettings
// https://chromium.googlesource.com/chromium/src/+/HEAD/android_webview/docs/cors-and-webview-api.md
// https://docs.jumio.com/production/Content/Integration/Integration%20Channels/Android%20WebView.htm
}
loadUrl(applicationUrl)
}
setContentView(webView)
}
}
165 changes: 165 additions & 0 deletions androidApp-kt/addedFolder/MainActivity files/MainActivity-2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package com.hadithmv.hmv

import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.ComponentActivity
import android.content.Intent
import android.net.Uri
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import androidx.activity.OnBackPressedCallback
import android.content.SharedPreferences

/**
* MainActivity serves as the primary activity for the WebView-based application.
* It handles URL loading, state persistence, and back button navigation.
*/
class MainActivity : ComponentActivity() {
// Base URL for the application's local HTML content in the assets folder
private val applicationUrl = "file:///android_asset/books/index.html"

// WebView instance that will be used throughout the activity's lifecycle
private lateinit var webView: WebView

// SharedPreferences instance for persisting the last visited URL
private lateinit var prefs: SharedPreferences

// Key used to store and retrieve the last visited URL in SharedPreferences
private val LAST_URL_KEY = "last_url"

/**
* Initialize the activity and set up the WebView with all necessary configurations.
* @param savedInstanceState Bundle containing the activity's previously saved state
*/
@SuppressLint("SetJavaScriptEnabled") // Suppress JavaScript warning as it's required for our app
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Initialize SharedPreferences for storing the last visited URL
prefs = getSharedPreferences("WebViewPrefs", MODE_PRIVATE)

// Create and configure the WebView
webView = WebView(this).apply {
// Configure the WebViewClient to handle URL loading
webViewClient = object : WebViewClient() {
/**
* Handle URL loading requests. External URLs are opened in the browser,
* while local files are handled within the WebView.
*/
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
val url = request?.url?.toString()
return when {
url == null -> false // No URL provided, let WebView handle it
url.startsWith("file://") -> false // Local file, let WebView handle it
else -> {
try {
// External URL: open in browser
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
true
} catch (e: Exception) {
// If no app can handle the URL, let WebView try to handle it
false
}
}
}
}

/**
* Called when a page finishes loading. Save the URL to preferences
* for persistence across app restarts.
*/
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
url?.let { saveLastUrl(it) }
}
}

// Configure WebView settings
settings.apply {
javaScriptEnabled = true // Enable JavaScript execution
allowFileAccess = true // Allow access to local files
domStorageEnabled = true // Enable DOM storage API
allowFileAccessFromFileURLs = true // Allow local files to access other local files
allowContentAccess = true // Allow access to content providers

// Prevent mixed content (HTTP content in HTTPS pages)
mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW

// Enable caching
cacheMode = WebSettings.LOAD_DEFAULT

// Enable database storage API
databaseEnabled = true
}

// Load either the last visited URL or the default URL
loadUrl(getLastUrl())
}

// Set the WebView as the content view
setContentView(webView)

// Configure back button handling
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
/**
* Handle back button press:
* - If WebView can go back, navigate to previous page
* - Otherwise, exit the app
*/
override fun handleOnBackPressed() {
when {
webView.canGoBack() -> webView.goBack()
else -> {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}
}
})
}

/**
* Save the last visited URL to SharedPreferences.
* @param url The URL to save
*/
private fun saveLastUrl(url: String) {
prefs.edit().apply {
putString(LAST_URL_KEY, url)
apply() // Asynchronously save changes
}
}

/**
* Retrieve the last visited URL from SharedPreferences.
* @return The last visited URL or the default application URL if none exists
*/
private fun getLastUrl(): String {
return prefs.getString(LAST_URL_KEY, applicationUrl) ?: applicationUrl
}

/**
* Save WebView state when the activity is paused.
*/
override fun onPause() {
super.onPause()
webView.saveState(Bundle())
}

/**
* Save WebView state before the activity might be destroyed.
*/
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
webView.saveState(outState)
}

/**
* Restore WebView state when the activity is recreated.
*/
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
webView.restoreState(savedInstanceState)
}
}
1 change: 1 addition & 0 deletions androidApp-kt/addedFolder/MainActivity files/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/hadithmv/hadithmv.github.io/tree/master/androidApp-kt/app/src/main/java/com/hadithmv/hmv
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion androidApp-kt/app/src/main/assets/css/DT-inline.min.css

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion books-uc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2147,7 +2147,10 @@
</div>
<!-- -->
<div class="bookContainer">
<a class="book" href="../mauhad/arabic.html">
<a
class="book"
href="https://hadithmv.github.io/mauhad/arabic.html"
>
<p>1 مناهج معهد تعليم اللغة العربية</p>
<p>📙 މަދީނާ އަރަބި ފޮތްތައް</p>
</a>
Expand Down
2 changes: 2 additions & 0 deletions css/DT-inline.css
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@
.dt-input::-webkit-search-cancel-button {
position: relative;
left: 10px; /* Adjust this value as needed */
/* increase size a little */
font-size: 50px;
}
/* firefox doesnt show it at all */
}
Expand Down
2 changes: 1 addition & 1 deletion css/DT-inline.min.css

Large diffs are not rendered by default.

0 comments on commit d0caf16

Please sign in to comment.