This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mock repository class for providing data | |
class RemoteRepository { | |
fun getUserName(): String { | |
return "Baljeet Singh" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun setupWebViewClient() { | |
val webViewClient = WebViewClientCompat() | |
webview.webViewClient = webViewClient | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val settings: WebSettings = webview.settings | |
settings.loadWithOverviewMode = true | |
settings.databaseEnabled = true | |
settings.domStorageEnabled = true | |
settings.javaScriptEnabled = true | |
settings.setAppCacheEnabled(false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val settings: WebSettings = webview.settings | |
settings.loadWithOverviewMode = true | |
settings.databaseEnabled = true | |
settings.domStorageEnabled = true | |
settings.javaScriptEnabled = true | |
settings.setAppCacheEnabled(false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |