Skip to content

Instantly share code, notes, and snippets.

View iambaljeet's full-sized avatar

Baljeet Singh iambaljeet

View GitHub Profile
// Mock repository class for providing data
class RemoteRepository {
fun getUserName(): String {
return "Baljeet Singh"
}
}
// Koin for Android
implementation "org.koin:koin-android:2.1.5"
// Koin AndroidX ViewModel features
implementation "org.koin:koin-androidx-viewmodel:2.1.5"
// Koin AndroidX Experimental features
implementation "org.koin:koin-androidx-ext:2.1.5"
private fun saveUrlDataToFile() {
if (websiteToLoad?.isNotBlank() == true) {
webUrlCoroutineJob = CoroutineScope(Dispatchers.IO).launch {
loadAndSaveDataFromUrlToFile(websiteToLoad)
withContext(Dispatchers.Main) {
Toast.makeText(this@MainActivity, "Loading finished", Toast.LENGTH_SHORT).show()
}
}
} else {
Toast.makeText(this, "Enter website", Toast.LENGTH_SHORT).show()
fun loadUrlIntoWebView() {
webUrlCoroutineJob = CoroutineScope(Dispatchers.IO).launch {
val path: File = filesDir
val file = File(path, "offlineFile.txt")
val length = file.length().toInt()
val bytes = ByteArray(length)
val fileInputStream = FileInputStream(file)
try {
fileInputStream.read(bytes)
} finally {
fun setupWebViewClient() {
val webViewClient = WebViewClientCompat()
webview.webViewClient = webViewClient
}
val settings: WebSettings = webview.settings
settings.loadWithOverviewMode = true
settings.databaseEnabled = true
settings.domStorageEnabled = true
settings.javaScriptEnabled = true
settings.setAppCacheEnabled(false)
val settings: WebSettings = webview.settings
settings.loadWithOverviewMode = true
settings.databaseEnabled = true
settings.domStorageEnabled = true
settings.javaScriptEnabled = true
settings.setAppCacheEnabled(false)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebViewActivity">
<WebView
fun loadUrlIntoWebView() {
webUrlCoroutineJob = CoroutineScope(Dispatchers.IO).launch {
val path: File = filesDir
val file = File(path, "offlineFile.txt")
val length = file.length().toInt()
val bytes = ByteArray(length)
val fileInputStream = FileInputStream(file)
try {
fileInputStream.read(bytes)
} finally {
private fun loadAndSaveDataFromUrlToFile(websiteToLoad: String?) {
val google = URL(websiteToLoad)
val bufferedReader = BufferedReader(InputStreamReader(google.openStream()))
var input: String?
val stringBuffer = StringBuffer()
while (bufferedReader.readLine().also { input = it } != null) {
stringBuffer.append(input)
}
bufferedReader.close()
val htmlData = stringBuffer.toString()