Added better imageURL finder

Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2021-10-04 21:15:21 +02:00
parent c213fd0f23
commit b0b22e8158
Signed by: Bazsalanszky
GPG key ID: 933820884952BE27
7 changed files with 52 additions and 66 deletions

View file

@ -9,11 +9,15 @@
<entry key="../../../../../layout/compose-model-1631273263532.xml" value="0.6462962962962963" />
<entry key="../../../../../layout/compose-model-1631532588435.xml" value="0.5888888888888889" />
<entry key="../../../../../layout/compose-model-1631535061700.xml" value="0.5851851851851851" />
<entry key="../../../../../layout/compose-model-1631612496291.xml" value="0.39469026548672564" />
<entry key="../../../../../layout/compose-model-1631623919303.xml" value="0.5777777777777777" />
<entry key="../../../../../layout/compose-model-1633291176088.xml" value="0.30869932432432434" />
<entry key="../../../../../layout/compose-model-1633370743378.xml" value="0.1" />
<entry key="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml" value="0.21458333333333332" />
</map>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View file

@ -59,9 +59,10 @@ dependencies {
implementation("io.coil-kt:coil-compose:1.3.2")
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'
implementation "androidx.navigation:navigation-compose:2.4.0-alpha10"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
implementation "com.google.accompanist:accompanist-swiperefresh:0.19.0"
implementation "org.jsoup:jsoup:1.13.1"
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

View file

@ -15,7 +15,10 @@ import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
@ -26,6 +29,7 @@ import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import eu.toldi.balazs.anotherfeedreader.entities.Article
import eu.toldi.balazs.anotherfeedreader.entities.Feed
import eu.toldi.balazs.anotherfeedreader.entities.FeedData
import eu.toldi.balazs.anotherfeedreader.entities.FeedGroup
import eu.toldi.balazs.anotherfeedreader.ui.theme.AnotherFeedReaderTheme
import kotlinx.coroutines.launch
@ -33,11 +37,6 @@ import java.net.URL
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import eu.toldi.balazs.anotherfeedreader.entities.FeedData
class MainActivity : ComponentActivity() {
@ -81,7 +80,9 @@ class MainActivity : ComponentActivity() {
},
) {
LazyColumn(
modifier = Modifier.fillMaxHeight()
modifier = Modifier
.fillMaxHeight()
.fillMaxWidth()
) {
items(articleList.value.size) { index ->
@ -161,7 +162,7 @@ class MainActivity : ComponentActivity() {
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
modifier = Modifier
.height(150.dp)
.height(200.dp)
.fillMaxWidth()
.fillMaxHeight()
)
@ -169,13 +170,21 @@ class MainActivity : ComponentActivity() {
Row {
if (article.author != null) {
Text(
article.author,
text = (fun(): String {
if (article.author.length > 25)
return article.author.dropLast(article.author.length - 25)
else return article.author
}).invoke(),
fontSize = 11.sp
)
} else Text("")
article.feedName?.let {
Text(
it,
text = (fun(): String {
return if (it.length > 25)
return it.dropLast(it.length - 25)
else return it
}).invoke(),
fontSize = 11.sp,
textAlign = TextAlign.Center,
modifier = Modifier.padding(horizontal = 10.dp),
@ -241,7 +250,7 @@ class MainActivity : ComponentActivity() {
ModalDrawer(drawerState = drawerState, drawerContent = {
LazyColumn {
item {
Text(text=stringResource(id = R.string.all_feeds),
Text(text = stringResource(id = R.string.all_feeds),
fontSize = 26.sp,
maxLines = 1,
overflow = TextOverflow.Ellipsis,

View file

@ -1,23 +1,12 @@
package eu.toldi.balazs.anotherfeedreader.entities
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.w3c.dom.Document
import kotlinx.coroutines.runBlocking
import org.jsoup.Jsoup
import org.w3c.dom.Element
import org.w3c.dom.Node
import org.w3c.dom.NodeList
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import android.R.xml
import java.io.StringReader
import org.xml.sax.InputSource
import java.net.URL
class Article(
@ -67,7 +56,7 @@ class Article(
* Cikk beolvasása egy megadott Node objektumból. Egy hírcsatorna beolvasásakor könnyedén alkalmazható egy "item" Node-ra
* @param article Egy "item" Node egy RSS hírcsatornáról
*/
fun createFromNode(article: Node,feedName: String?) : Article{
fun createFromNode(article: Node, feedName: String?): Article {
val properties: NodeList = article.getChildNodes()
var title: String? = null
var url: String? = null
@ -100,40 +89,24 @@ class Article(
}
}
}
if(imageURL == null && url != null) {
/* GlobalScope.launch {
if (imageURL == null && url != null) {
runBlocking {
val con = Jsoup.connect(url)
val doc = con.userAgent("Mozilla").get()
val factory: DocumentBuilderFactory = DocumentBuilderFactory.newInstance()
val builder: DocumentBuilder = factory.newDocumentBuilder()
var doc: Document
withContext(Dispatchers.IO) {
val ips = InputSource()
ips.characterStream = StringReader(URL(url).readText().replace("<!doctype html>","<?xml version=\"1.0\" encoding=\"utf-8\"?>"))
doc = builder.parse(ips)
}
val ogTags = doc.select("meta[property^=og:]")
val metas: NodeList = doc.getElementsByTagName("meta")
for (i in 0 until metas.length) {
val meta: Node = metas.item(i)
val property = meta.attributes.getNamedItem("property")
if (property != null && property.textContent == "og:image"){
val imageProperty = meta.attributes.getNamedItem("content")
if(imageProperty != null)
imageURL = imageProperty.textContent
if (ogTags.size > 0) {
ogTags.forEachIndexed { index, element ->
val tag = ogTags[index]
if (tag.attr("property") == "og:image")
imageURL = tag.attr("content")
}
}
}*/
}
}
return Article(title, url, description, imageURL, author, pubDate,feedName)
return Article(title, url, description, imageURL, author, pubDate, feedName)
}
}
/**
* A cikk megnyitása böngészőben (ha ez lehetséges)
*/
fun open() {
if (url == null) return
//TODO: open link in webview
}
}

View file

@ -1,6 +1,5 @@
package eu.toldi.balazs.anotherfeedreader.entities
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
@ -35,7 +34,7 @@ open class Feed
/**
* A hírcsatornához tartozó cikkek listája
*/
open var articleList: List<Article> = ArrayList()
open var articleList: List<Article> = emptyList()
private val _isRefreshing = MutableStateFlow(false)
val isRefreshing: StateFlow<Boolean>
@ -82,7 +81,7 @@ open class Feed
val a = Article.createFromNode(article,name)
addArticle(a)
}
Log.e(null, articleCount.toString())
articleList.sortedByDescending { it.pubDate }
_isRefreshing.emit(false)
}
}

View file

@ -7,8 +7,10 @@ class FeedData : FeedGroup("MainFrame") {
private var _articleList = emptyList<Article>()
override var articleList: List<Article>
get() =limit?.articleList ?: _articleList
set(value) {_articleList = value.sortedByDescending { it.pubDate }}
get() = limit?.articleList?.sortedByDescending { it.pubDate } ?: _articleList
set(value) {
_articleList = value.sortedByDescending { it.pubDate }
}
}

View file

@ -1,9 +1,7 @@
package eu.toldi.balazs.anotherfeedreader.entities
import android.util.Log
import java.util.ArrayList
import java.io.IOException
import java.util.*
open class FeedGroup(name: String) : Feed(name) {
@ -56,9 +54,9 @@ open class FeedGroup(name: String) : Feed(name) {
if (articleList == null) articleList = ArrayList()
for (f in feedList) {
f.updateFeed()
for (i in 0 until f.articleCount) {
if (!hasAricle(f[i]))
articleList += f[i]
f.articleList.forEach {
if (!hasAricle(it))
articleList += it
}
}
articleList = articleList.sortedByDescending { it.pubDate }