Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
01c0a7da7f
3 changed files with 90 additions and 75 deletions
|
@ -49,6 +49,7 @@ import java.time.format.DateTimeFormatter
|
|||
import java.time.format.FormatStyle
|
||||
|
||||
const val FILE_SELECT_CODE = 0
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
|
||||
|
@ -88,13 +89,16 @@ class MainActivity : ComponentActivity() {
|
|||
}
|
||||
}
|
||||
val isRefreshing = viewModel.isRefereshing.value
|
||||
|
||||
val isFeedListChanged = viewModel.isFeedListChanged.value
|
||||
SwipeRefresh(
|
||||
state = rememberSwipeRefreshState(isRefreshing),
|
||||
onRefresh = {
|
||||
viewModel.updateFeeds()
|
||||
},
|
||||
) {
|
||||
if(isFeedListChanged) {
|
||||
Text(text= "Feed list changed!", modifier = Modifier.padding(all = 15.dp))
|
||||
}
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxHeight()
|
||||
|
@ -124,7 +128,6 @@ class MainActivity : ComponentActivity() {
|
|||
}
|
||||
LaunchedEffect(true) {
|
||||
viewModel.initArticles()
|
||||
viewModel.updateFeeds()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -136,7 +139,8 @@ class MainActivity : ComponentActivity() {
|
|||
var groupName by remember { mutableStateOf("New Group") }
|
||||
var selectedIndex by remember { mutableStateOf(0) }
|
||||
val feedGroups = viewModel.feedGroups.observeAsState(emptyList())
|
||||
AlertDialog(onDismissRequest = { toClose() },
|
||||
AlertDialog(
|
||||
onDismissRequest = { toClose() },
|
||||
title = {
|
||||
Text(text = "Add feed")
|
||||
},
|
||||
|
@ -246,7 +250,6 @@ class MainActivity : ComponentActivity() {
|
|||
viewModel.addFeed(feed)
|
||||
}
|
||||
}
|
||||
viewModel.changeFeedList()
|
||||
} catch (e: Exception) {
|
||||
Log.e(null, e.stackTraceToString())
|
||||
} finally {
|
||||
|
@ -465,9 +468,17 @@ class MainActivity : ComponentActivity() {
|
|||
modifier = Modifier.padding(all = 4.dp)
|
||||
)
|
||||
} else if (inputElement is Feed) {
|
||||
inputElement.name?.let {
|
||||
if (inputElement.name != null) {
|
||||
Text(
|
||||
text = it,
|
||||
text = inputElement.name!!,
|
||||
fontSize = 26.sp,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier = Modifier.padding(all = 4.dp)
|
||||
)
|
||||
} else {
|
||||
Text(
|
||||
text = inputElement.feedURL,
|
||||
fontSize = 26.sp,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
|
@ -492,9 +503,17 @@ class MainActivity : ComponentActivity() {
|
|||
elevation = 10.dp,
|
||||
backgroundColor = MaterialTheme.colors.surface,
|
||||
) {
|
||||
it.name?.let {
|
||||
if (it.name != null) {
|
||||
Text(
|
||||
text = it,
|
||||
text = it.name!!,
|
||||
fontSize = 26.sp,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier = Modifier.padding(all = 4.dp)
|
||||
)
|
||||
} else {
|
||||
Text(
|
||||
text = it.feedURL,
|
||||
fontSize = 26.sp,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
|
@ -574,10 +593,6 @@ class MainActivity : ComponentActivity() {
|
|||
}, content = content)
|
||||
}
|
||||
|
||||
override fun onActivityReenter(resultCode: Int, data: Intent?) {
|
||||
super.onActivityReenter(resultCode, data)
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun DefaultPreview() {
|
||||
|
@ -613,16 +628,17 @@ class MainActivity : ComponentActivity() {
|
|||
}
|
||||
}
|
||||
}
|
||||
fun openArticle(articleURL: String, feedName: String) {
|
||||
WebView.webURL = articleURL
|
||||
WebView.barTitle = feedName
|
||||
startActivity(
|
||||
Intent(
|
||||
baseContext,
|
||||
WebView::class.java
|
||||
)
|
||||
)
|
||||
|
||||
}
|
||||
fun openArticle(articleURL: String, feedName: String) {
|
||||
WebView.webURL = articleURL
|
||||
WebView.barTitle = feedName
|
||||
startActivity(
|
||||
Intent(
|
||||
baseContext,
|
||||
WebView::class.java
|
||||
)
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -11,6 +11,8 @@ import eu.toldi.balazs.anotherfeedreader.entities.FeedGroup
|
|||
import eu.toldi.balazs.anotherfeedreader.sqlite.FeedsAndArticlesDAO
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.mm2d.touchicon.TouchIconExtractor
|
||||
import org.w3c.dom.Document
|
||||
|
@ -24,9 +26,12 @@ class FeedRepository(private val feedsAndArticleDAO: FeedsAndArticlesDAO) {
|
|||
|
||||
|
||||
private val _isRefereshing: MutableState<Boolean> = mutableStateOf(false)
|
||||
private val _isFeedListChanged = mutableStateOf(false)
|
||||
|
||||
val isRefeshing: State<Boolean>
|
||||
get() = _isRefereshing
|
||||
val isFeedListChanged: State<Boolean>
|
||||
get() = _isFeedListChanged
|
||||
|
||||
private var limit: Any? = null
|
||||
|
||||
|
@ -43,26 +48,24 @@ class FeedRepository(private val feedsAndArticleDAO: FeedsAndArticlesDAO) {
|
|||
fun getFeedsByGroup(id: Long?) = feedsAndArticleDAO.getFeedsByGroup(id)
|
||||
|
||||
suspend fun getAllArticles(): List<Article> {
|
||||
|
||||
return when (limit) {
|
||||
_isFeedListChanged.value = true
|
||||
val list = when (limit) {
|
||||
is Feed -> {
|
||||
_isRefereshing.value = true
|
||||
|
||||
withContext(IO) {
|
||||
feedsAndArticleDAO.getArticlesByFeed((limit as Feed).feedId!!).also {
|
||||
_isRefereshing.value = false
|
||||
}
|
||||
feedsAndArticleDAO.getArticlesByFeed((limit as Feed).feedId!!)
|
||||
}
|
||||
}
|
||||
is FeedGroup -> {
|
||||
_isRefereshing.value = true
|
||||
|
||||
withContext(IO) {
|
||||
feedsAndArticleDAO.getAllFromGroup((limit as FeedGroup).id!!).also {
|
||||
_isRefereshing.value = false
|
||||
}
|
||||
feedsAndArticleDAO.getAllFromGroup((limit as FeedGroup).id!!)
|
||||
}
|
||||
}
|
||||
else -> withContext(IO) { feedsAndArticleDAO.getAllList() }
|
||||
}
|
||||
_isFeedListChanged.value =false
|
||||
return list
|
||||
}
|
||||
|
||||
suspend fun instert(feed: Feed, article: Article) {
|
||||
|
@ -76,6 +79,7 @@ class FeedRepository(private val feedsAndArticleDAO: FeedsAndArticlesDAO) {
|
|||
withContext(IO) {
|
||||
try {
|
||||
_isRefereshing.value = true
|
||||
_isFeedListChanged.value = true
|
||||
when (limit) {
|
||||
is Feed -> {
|
||||
try {
|
||||
|
@ -106,6 +110,7 @@ class FeedRepository(private val feedsAndArticleDAO: FeedsAndArticlesDAO) {
|
|||
}
|
||||
}
|
||||
_isRefereshing.value = false
|
||||
_isFeedListChanged.value = false
|
||||
} catch (e: Exception) {
|
||||
Log.e(null, e.stackTraceToString())
|
||||
}
|
||||
|
@ -114,37 +119,45 @@ class FeedRepository(private val feedsAndArticleDAO: FeedsAndArticlesDAO) {
|
|||
}
|
||||
|
||||
private suspend fun updateFeed(feed: Feed) {
|
||||
|
||||
val builder: DocumentBuilder = factory.newDocumentBuilder()
|
||||
var doc: Document = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
builder.parse(feed.link.toString())
|
||||
} catch (e: Exception) {
|
||||
Log.e(null, e.stackTraceToString())
|
||||
builder.newDocument()
|
||||
withContext(IO) {
|
||||
val builder: DocumentBuilder = factory.newDocumentBuilder()
|
||||
var doc: Document = withContext(IO) {
|
||||
try {
|
||||
builder.parse(feed.link.toString())
|
||||
} catch (e: Exception) {
|
||||
Log.e(null, e.stackTraceToString())
|
||||
builder.newDocument()
|
||||
}
|
||||
}
|
||||
}
|
||||
if (doc.getElementsByTagName("title").length > 0)
|
||||
feed.name = doc.getElementsByTagName("title").item(0).getTextContent()
|
||||
val items: NodeList = doc.getElementsByTagName("item")
|
||||
for (i in 0 until items.length) {
|
||||
val article: Node = items.item(i)
|
||||
val a = withContext(Dispatchers.IO) { Article.createFromNode(article, feed = feed) }
|
||||
val a1 = withContext(IO) { a.url?.let { feedsAndArticleDAO.getArticleByURL(it) } }
|
||||
if (a != a1) {
|
||||
feedsAndArticleDAO.insertArticle(feed, a)
|
||||
_articleState.value = getAllArticles()
|
||||
if (doc.getElementsByTagName("title").length > 0)
|
||||
feed.name = doc.getElementsByTagName("title").item(0).getTextContent()
|
||||
val items: NodeList = doc.getElementsByTagName("item")
|
||||
for (i in 0 until items.length) {
|
||||
GlobalScope.launch {
|
||||
try {
|
||||
val article: Node = items.item(i)
|
||||
val a = withContext(IO) { Article.createFromNode(article, feed = feed) }
|
||||
val a1 =
|
||||
withContext(IO) { a.url?.let { feedsAndArticleDAO.getArticleByURL(it) } }
|
||||
if (a != a1) {
|
||||
feedsAndArticleDAO.insertArticle(feed, a)
|
||||
_articleState.value = getAllArticles()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(null, e.stackTraceToString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
feed.faviconURL = withContext(Dispatchers.IO) {
|
||||
val extractor = TouchIconExtractor()
|
||||
val icons = extractor.fromPage("https://" + feed.link.host)
|
||||
if (icons.size > 0)
|
||||
icons.maxByOrNull { it.inferArea() }?.url
|
||||
else null
|
||||
feed.faviconURL = withContext(IO) {
|
||||
val extractor = TouchIconExtractor()
|
||||
val icons = extractor.fromPage("https://" + feed.link.host)
|
||||
if (icons.size > 0)
|
||||
icons.maxByOrNull { it.inferArea() }?.url
|
||||
else null
|
||||
}
|
||||
feedsAndArticleDAO.updateFeed(feed)
|
||||
}
|
||||
feedsAndArticleDAO.updateFeed(feed)
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -22,18 +22,16 @@ class FeedViewModel(application: Application) : AndroidViewModel(application) {
|
|||
val feeds: LiveData<List<Feed>>
|
||||
val feedGroups: LiveData<List<FeedGroup>>
|
||||
val isRefereshing: State<Boolean>
|
||||
val isFeedListChanged: State<Boolean>
|
||||
|
||||
init {
|
||||
val feedsAndArticlesDao = AppDatabase.getInstance(application).feedsAndArticlesDao()
|
||||
feedRepo = FeedRepository(feedsAndArticleDAO = feedsAndArticlesDao)
|
||||
articles = feedRepo.articleState
|
||||
viewModelScope.launch(IO) {
|
||||
feedRepo.updateFeeds()
|
||||
|
||||
}
|
||||
feeds = feedRepo.getFeedList()
|
||||
feedGroups = feedRepo.getFeedGroups()
|
||||
isRefereshing = feedRepo.isRefeshing
|
||||
isFeedListChanged = feedRepo.isFeedListChanged
|
||||
}
|
||||
|
||||
fun updateFeeds(): Job {
|
||||
|
@ -42,18 +40,6 @@ class FeedViewModel(application: Application) : AndroidViewModel(application) {
|
|||
}
|
||||
}
|
||||
|
||||
fun changeFeed() {
|
||||
viewModelScope.launch {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun changeFeedList() {
|
||||
viewModelScope.launch {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun setLimit(feed: Any?) {
|
||||
viewModelScope.launch {
|
||||
feedRepo.setLimit(feed)
|
||||
|
|
Loading…
Reference in a new issue