feat: 优化

pull/209/head
kunfei 5 years ago
parent 9e28739bf3
commit 995dde9096
  1. 12
      app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt
  2. 21
      app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt
  3. 3
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt
  4. 17
      app/src/main/java/io/legado/app/utils/ViewExtensions.kt

@ -45,12 +45,9 @@ class RssAdapter(context: Context, val callBack: CallBack) :
popupMenu.inflate(R.menu.rss_main_item) popupMenu.inflate(R.menu.rss_main_item)
popupMenu.setOnMenuItemClickListener { popupMenu.setOnMenuItemClickListener {
when (it.itemId) { when (it.itemId) {
R.id.menu_top -> { R.id.menu_top -> callBack.toTop(rssSource)
} R.id.menu_edit -> callBack.edit(rssSource)
R.id.menu_edit -> { R.id.menu_del -> callBack.del(rssSource)
}
R.id.menu_del -> {
}
} }
true true
} }
@ -59,5 +56,8 @@ class RssAdapter(context: Context, val callBack: CallBack) :
interface CallBack { interface CallBack {
fun openRss(rssSource: RssSource) fun openRss(rssSource: RssSource)
fun toTop(rssSource: RssSource)
fun edit(rssSource: RssSource)
fun del(rssSource: RssSource)
} }
} }

@ -8,22 +8,27 @@ import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseFragment import io.legado.app.base.VMBaseFragment
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.ui.main.MainViewModel import io.legado.app.ui.main.MainViewModel
import io.legado.app.ui.rss.article.RssSortActivity import io.legado.app.ui.rss.article.RssSortActivity
import io.legado.app.ui.rss.favorites.RssFavoritesActivity import io.legado.app.ui.rss.favorites.RssFavoritesActivity
import io.legado.app.ui.rss.source.edit.RssSourceEditActivity
import io.legado.app.ui.rss.source.manage.RssSourceActivity import io.legado.app.ui.rss.source.manage.RssSourceActivity
import io.legado.app.ui.rss.source.manage.RssSourceViewModel
import io.legado.app.utils.getViewModel
import io.legado.app.utils.getViewModelOfActivity import io.legado.app.utils.getViewModelOfActivity
import io.legado.app.utils.startActivity import io.legado.app.utils.startActivity
import kotlinx.android.synthetic.main.fragment_rss.* import kotlinx.android.synthetic.main.fragment_rss.*
import kotlinx.android.synthetic.main.view_title_bar.* import kotlinx.android.synthetic.main.view_title_bar.*
class RssFragment : BaseFragment(R.layout.fragment_rss), class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
RssAdapter.CallBack { RssAdapter.CallBack {
private lateinit var adapter: RssAdapter private lateinit var adapter: RssAdapter
override val viewModel: RssSourceViewModel
get() = getViewModel(RssSourceViewModel::class.java)
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(toolbar) setSupportToolbar(toolbar)
@ -62,4 +67,16 @@ class RssFragment : BaseFragment(R.layout.fragment_rss),
override fun openRss(rssSource: RssSource) { override fun openRss(rssSource: RssSource) {
startActivity<RssSortActivity>(Pair("url", rssSource.sourceUrl)) startActivity<RssSortActivity>(Pair("url", rssSource.sourceUrl))
} }
override fun toTop(rssSource: RssSource) {
viewModel.topSource(rssSource)
}
override fun edit(rssSource: RssSource) {
startActivity<RssSourceEditActivity>(Pair("data", rssSource.sourceUrl))
}
override fun del(rssSource: RssSource) {
viewModel.del(rssSource)
}
} }

@ -147,7 +147,6 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
null null
} }
} }
if (null != content) { if (null != content) {
GSON.fromJsonArray<RssSource>(content)?.let { GSON.fromJsonArray<RssSource>(content)?.let {
App.db.rssSourceDao().insert(*it.toTypedArray()) App.db.rssSourceDao().insert(*it.toTypedArray())
@ -215,8 +214,8 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
} }
App.db.rssSourceDao().insert(*sources.toTypedArray()) App.db.rssSourceDao().insert(*sources.toTypedArray())
return sources.size return sources.size
} }
return 0 return 0
} }
} }

@ -1,5 +1,6 @@
package io.legado.app.utils package io.legado.app.utils
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Canvas import android.graphics.Canvas
@ -11,8 +12,11 @@ import android.view.inputmethod.InputMethodManager
import android.widget.RadioGroup import android.widget.RadioGroup
import android.widget.SeekBar import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.menu.MenuPopupHelper
import androidx.appcompat.widget.PopupMenu
import androidx.core.view.get import androidx.core.view.get
import io.legado.app.App import io.legado.app.App
import java.lang.reflect.Field
private tailrec fun getCompatActivity(context: Context?): AppCompatActivity? { private tailrec fun getCompatActivity(context: Context?): AppCompatActivity? {
@ -108,3 +112,16 @@ fun RadioGroup.getCheckedIndex(): Int {
fun RadioGroup.checkByIndex(index: Int) { fun RadioGroup.checkByIndex(index: Int) {
check(get(index).id) check(get(index).id)
} }
@SuppressLint("RestrictedApi")
fun PopupMenu.show(x: Int, y: Int) {
try {
val field: Field = this.javaClass.getDeclaredField("mPopup")
field.isAccessible = true
(field.get(this) as MenuPopupHelper).show(x, y)
} catch (e: NoSuchFieldException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
}
Loading…
Cancel
Save