Added Article WebView

This commit is contained in:
Toldi Balázs Ádám 2021-09-29 22:40:34 +02:00
parent e01c73b08a
commit 3d57fe4526
6 changed files with 140 additions and 16 deletions

View file

@ -57,6 +57,7 @@ dependencies {
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.navigation:navigation-compose:2.4.0-alpha09"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'

View file

@ -1,15 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eu.toldi.balazs.anotherfeedreader">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.AnotherFeedReader">
<activity
android:name=".WebView"
android:exported="true"
android:label="@string/title_activity_web_view"
android:theme="@style/Theme.AnotherFeedReader.NoActionBar" />
<activity
android:name=".MainActivity"
android:exported="true"

View file

@ -1,13 +1,20 @@
package eu.toldi.balazs.anotherfeedreader
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ScrollView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
@ -40,12 +47,33 @@ class MainActivity : ComponentActivity() {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
var articleCount by remember { mutableStateOf(feed.articleCount) }
var articleCount = remember { mutableStateOf(feed.articleCount) }
Log.w(null,articleCount.toString())
Column {
MenuBar()
LazyColumn {
items(articleCount) { index ->
showArticle(article = feed.get(index), feed.name!!)
val scrollState = rememberScrollState()
LazyColumn(
modifier = Modifier.scrollable(
state = scrollState,
orientation = Orientation.Vertical
)
) {
items(articleCount.value) { index ->
Column(
Modifier
.clickable {
Log.i(null, feed[index].title.toString())
WebView.webURL = feed[index].url.toString()
WebView.barTitle = feed.name.toString()
startActivity(Intent(baseContext, WebView::class.java))
}
.padding(
horizontal = 3.dp
)
) {
showArticle(article = feed[index], feed.name!!)
}
}
}
@ -91,7 +119,7 @@ class MainActivity : ComponentActivity() {
@Composable
fun showArticle(article: Article, feedName: String) {
Column(modifier = Modifier.padding(top = 5.dp, bottom = 5.dp)) {
Column(modifier = Modifier.padding(all = 5.dp)) {
Row {
if (article.author != null) {
Text(
@ -116,7 +144,9 @@ class MainActivity : ComponentActivity() {
}
if (article.title != null) {
Text(
text = article.title
text = article.title.trim(),
maxLines = 2,
overflow = TextOverflow.Ellipsis
)
} else Text("")
if (article.description != null) {
@ -142,15 +172,20 @@ class MainActivity : ComponentActivity() {
author = "Toldi Balázs",
pubDate = LocalDateTime.now()
)
showArticle(article, "Test feed")
showArticle(
Article(
title = "Teszt Article #2",
description = "Ez egy másik megjelenítést tesztelő cikk. Ebben egy nagyon hosszú szöveget akarok ábrázolni, aminek nem szabad egyben kiférnie. Legfeljebb 2 sort jeleníthet meg",
author = "Toldi Balázs",
pubDate = LocalDateTime.now().plusDays(-1)
), "Test2 feed"
)
Column(
modifier = Modifier.padding( horizontal = 1.dp)
) {
showArticle(article, "Test feed")
showArticle(
Article(
title = "Teszt Article #2",
description = "Ez egy másik megjelenítést tesztelő cikk. Ebben egy nagyon hosszú szöveget akarok ábrázolni, aminek nem szabad egyben kiférnie. Legfeljebb 2 sort jeleníthet meg",
author = "Toldi Balázs",
pubDate = LocalDateTime.now().plusDays(-1)
), "Test2 feed"
)
}
}
}
}

View file

@ -0,0 +1,68 @@
package eu.toldi.balazs.anotherfeedreader
import android.os.Bundle
import android.webkit.WebView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import eu.toldi.balazs.anotherfeedreader.WebView.Companion.barTitle
import eu.toldi.balazs.anotherfeedreader.WebView.Companion.webURL
import eu.toldi.balazs.anotherfeedreader.ui.theme.AnotherFeedReaderTheme
import eu.toldi.balazs.anotherfeedreader.views.CustomWebViewClient
class WebView : ComponentActivity() {
companion object {
var webURL = "http://example.rog"
var barTitle = ""
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AnotherFeedReaderTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
Column() {
TopBar(topTitle = barTitle)
ShowWebView("Android")
}
}
}
}
}
}
@Composable
fun TopBar(topTitle: String) {
TopAppBar (
title = { Text(text = topTitle) }
)
}
@Composable
fun ShowWebView(name: String) {
AndroidView(factory = { ctx ->
WebView(ctx).apply {
loadUrl(webURL)
webViewClient = CustomWebViewClient()
}
})
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
AnotherFeedReaderTheme {
Column {
TopBar("WebView")
ShowWebView("Android")
}
}
}

View file

@ -0,0 +1,12 @@
package eu.toldi.balazs.anotherfeedreader.views
import android.webkit.WebView
import android.webkit.WebViewClient
class CustomWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
view.loadUrl(url)
return true
}
}

View file

@ -1,3 +1,4 @@
<resources>
<string name="app_name">AnotherFeedReader</string>
<string name="title_activity_web_view">WebView</string>
</resources>