Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Sep 26, 2024
1 parent a762ba4 commit ae1c2bb
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 43 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/io/legado/app/data/dao/RssStarDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ interface RssStarDao {
val all: List<RssStar>

@Query("select `group` from rssStars group by `group` order by `group`")
fun groupList(): Flow<List<String>>
fun flowGroups(): Flow<List<String>>

@Query("select * from rssStars where `group` = :group order by starTime desc")
fun getByGroup(group: String): Flow<List<RssStar>>
fun flowByGroup(group: String): Flow<List<RssStar>>

@Query("select * from rssStars where origin = :origin and link = :link")
fun get(origin: String, link: String): RssStar?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package io.legado.app.ui.rss.favorites

import android.os.Bundle
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.lifecycle.lifecycleScope
Expand All @@ -16,34 +15,36 @@ import io.legado.app.utils.gone
import io.legado.app.utils.viewbindingdelegate.viewBinding
import io.legado.app.utils.visible
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

/**
* 收藏夹
*/
class RssFavoritesActivity : BaseActivity<ActivityRssFavoritesBinding>(){
class RssFavoritesActivity : BaseActivity<ActivityRssFavoritesBinding>() {

override val binding by viewBinding(ActivityRssFavoritesBinding::inflate)
private val adapter by lazy { TabFragmentPageAdapter() }
private var groupList = mutableListOf<String>()
private var rssStarFlowJob: Job? = null

override fun onActivityCreated(savedInstanceState: Bundle?) {
initView()
upFragments()
}

private fun initView() {
binding.viewPager.adapter = adapter
binding.tabLayout.setupWithViewPager(binding.viewPager)
binding.tabLayout.setSelectedTabIndicatorColor(accentColor)
upFragments()
}

private fun upFragments() {
rssStarFlowJob?.cancel()
rssStarFlowJob = lifecycleScope.launch {
appDb.rssStarDao.groupList().catch {
lifecycleScope.launch {
appDb.rssStarDao.flowGroups().catch {
AppLog.put("订阅分组数据获取失败\n${it.localizedMessage}", it)
}.flowOn(IO).collect {
}.distinctUntilChanged().flowOn(IO).collect {
groupList.clear()
groupList.addAll(it)
if (groupList.size == 1) {
Expand Down Expand Up @@ -76,9 +77,5 @@ class RssFavoritesActivity : BaseActivity<ActivityRssFavoritesBinding>(){
return groupList.size
}

override fun instantiateItem(container: ViewGroup, position: Int): Any {
val fragment = super.instantiateItem(container, position) as Fragment
return fragment
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package io.legado.app.ui.rss.favorites

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
import io.legado.app.data.entities.RssArticle
import io.legado.app.databinding.DialogRssfavoritesBinding
import io.legado.app.databinding.DialogRssFavoriteConfigBinding
import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.setLayout
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.launch

class RssFavoritesDialog() : BaseDialogFragment(R.layout.dialog_rssfavorites, true) {
class RssFavoritesDialog() : BaseDialogFragment(R.layout.dialog_rss_favorite_config, true) {

constructor(rssArticle: RssArticle) : this() {
arguments = Bundle().apply {
Expand All @@ -23,7 +20,7 @@ class RssFavoritesDialog() : BaseDialogFragment(R.layout.dialog_rssfavorites, tr
}
}

private val binding by viewBinding(DialogRssfavoritesBinding::bind)
private val binding by viewBinding(DialogRssFavoriteConfigBinding::bind)

override fun onStart() {
super.onStart()
Expand All @@ -47,23 +44,19 @@ class RssFavoritesDialog() : BaseDialogFragment(R.layout.dialog_rssfavorites, tr
}
tvOk.setOnClickListener {
val editTitle = editTitle.text.toString()
if(!TextUtils.isEmpty(editTitle)){
if (editTitle.isNotBlank()) {
title = editTitle
}
val editGroup = editGroup.text.toString()
if(!TextUtils.isEmpty(editGroup)){
if (editGroup.isNotBlank()) {
group = editGroup
}
lifecycleScope.launch {
callback?.updateFavorite(title, group)
dismiss()
}
callback?.updateFavorite(title, group)
dismiss()
}
tvFooterLeft.setOnClickListener {
lifecycleScope.launch {
callback?.deleteFavorite()
dismiss()
}
callback?.deleteFavorite()
dismiss()
}
}
}
Expand All @@ -78,4 +71,4 @@ class RssFavoritesDialog() : BaseDialogFragment(R.layout.dialog_rssfavorites, tr

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import io.legado.app.utils.setEdgeEffectColor
import io.legado.app.utils.startActivity
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
Expand All @@ -38,28 +37,26 @@ class RssFavoritesFragment() : VMBaseFragment<RssFavoritesViewModel>(R.layout.fr
private val adapter: RssFavoritesAdapter by lazy {
RssFavoritesAdapter(requireContext(), this@RssFavoritesFragment)
}
private var articlesFlowJob: Job? = null

override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
initView()
loadArticles()
}

private fun initView() = binding.run {
refreshLayout.setEnabled(false)
refreshLayout.isEnabled = false
recyclerView.setEdgeEffectColor(primaryColor)
recyclerView.layoutManager = run {
recyclerView.addItemDecoration(VerticalDivider(requireContext()))
LinearLayoutManager(requireContext())
}
recyclerView.adapter = adapter
loadArticles()
}

private fun loadArticles() {
articlesFlowJob?.cancel()
articlesFlowJob = lifecycleScope.launch {
lifecycleScope.launch {
val group = arguments?.getString("group") ?: "默认分组"
appDb.rssStarDao.getByGroup(group).catch {
appDb.rssStarDao.flowByGroup(group).catch {
AppLog.put("订阅文章界面获取数据失败\n${it.localizedMessage}", it)
}.flowOn(IO).collect {
adapter.setItems(it)
Expand All @@ -74,4 +71,4 @@ class RssFavoritesFragment() : VMBaseFragment<RssFavoritesViewModel>(R.layout.fr
putExtra("link", rssStar.link)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import io.legado.app.base.BaseViewModel

class RssFavoritesViewModel(application: Application) : BaseViewModel(application) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ import java.util.regex.PatternSyntaxException
/**
* rss阅读界面
*/
class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>(), RssFavoritesDialog.Callback {
class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>(),
RssFavoritesDialog.Callback {

override val binding by viewBinding(ActivityRssReadBinding::inflate)
override val viewModel by viewModels<ReadRssViewModel>()
Expand Down Expand Up @@ -153,12 +154,14 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
R.id.menu_rss_refresh -> viewModel.refresh {
binding.webView.reload()
}

R.id.menu_rss_star -> {
viewModel.addFavorite()
viewModel.rssArticle?.let {
showDialogFragment(RssFavoritesDialog(it))
}
}

R.id.menu_share_it -> {
binding.webView.url?.let {
share(it)
Expand Down

0 comments on commit ae1c2bb

Please sign in to comment.