saved searches livedata migration

This commit is contained in:
Mariotaku Lee 2018-04-11 23:49:35 +08:00
parent 002b41934a
commit 9e368b3aef
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
2 changed files with 59 additions and 26 deletions

View File

@ -0,0 +1,39 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.data
import android.accounts.AccountManager
import android.content.Context
import org.mariotaku.microblog.library.Twitter
import org.mariotaku.microblog.library.model.microblog.ResponseList
import org.mariotaku.microblog.library.model.microblog.SavedSearch
import org.mariotaku.twidere.extension.getDetailsOrThrow
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.model.UserKey
class SavedSearchesLiveData(val context: Context, private val accountKey: UserKey) :
ComputableExceptionLiveData<ResponseList<SavedSearch>>(false) {
override fun compute(): ResponseList<SavedSearch> {
val twitter = AccountManager.get(context).getDetailsOrThrow(accountKey, true)
.newMicroBlogInstance(context, Twitter::class.java)
return twitter.savedSearches
}
}

View File

@ -21,22 +21,20 @@ package org.mariotaku.twidere.fragment
import android.content.Context
import android.os.Bundle
import android.support.v4.app.LoaderManager.LoaderCallbacks
import android.support.v4.app.hasRunningLoadersSafe
import android.support.v4.content.Loader
import android.view.View
import android.widget.AdapterView
import com.bumptech.glide.RequestManager
import com.squareup.otto.Subscribe
import kotlinx.android.synthetic.main.fragment_content_listview.*
import org.mariotaku.microblog.library.model.microblog.ResponseList
import org.mariotaku.microblog.library.model.microblog.SavedSearch
import org.mariotaku.twidere.R
import org.mariotaku.twidere.adapter.SavedSearchesAdapter
import org.mariotaku.twidere.data.SavedSearchesLiveData
import org.mariotaku.twidere.extension.accountKey
import org.mariotaku.twidere.extension.get
import org.mariotaku.twidere.extension.data.observe
import org.mariotaku.twidere.extension.getErrorMessage
import org.mariotaku.twidere.extension.linkHandlerTitle
import org.mariotaku.twidere.loader.SavedSearchesLoader
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.event.SavedSearchDestroyedEvent
import org.mariotaku.twidere.singleton.BusSingleton
@ -44,8 +42,7 @@ import org.mariotaku.twidere.util.IntentUtils.openTweetSearch
import java.util.*
class SavedSearchesListFragment : AbsContentListViewFragment<SavedSearchesAdapter>(),
LoaderCallbacks<ResponseList<SavedSearch>?>, AdapterView.OnItemClickListener,
AdapterView.OnItemLongClickListener {
AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
override var refreshing: Boolean
get() = loaderManager.hasRunningLoadersSafe()
@ -56,15 +53,29 @@ class SavedSearchesListFragment : AbsContentListViewFragment<SavedSearchesAdapte
val accountKey: UserKey
get() = arguments!!.accountKey!!
private lateinit var savedSearchesLiveData: SavedSearchesLiveData
private val positionComparator = Comparator<SavedSearch> { object1, object2 -> object1.position - object2.position }
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
linkHandlerTitle = getString(R.string.saved_searches)
savedSearchesLiveData = SavedSearchesLiveData(activity!!, accountKey)
listView.onItemClickListener = this
listView.onItemLongClickListener = this
loaderManager.initLoader(0, null, this)
savedSearchesLiveData.observe(this, success = { data ->
adapter.setData(data)
showContent()
refreshing = false
}, fail = { ex ->
showError(R.drawable.ic_info_error_generic, ex.getErrorMessage(context!!))
refreshing = false
})
savedSearchesLiveData.load()
showProgress()
}
@ -82,10 +93,6 @@ class SavedSearchesListFragment : AbsContentListViewFragment<SavedSearchesAdapte
return SavedSearchesAdapter(activity)
}
override fun onCreateLoader(id: Int, args: Bundle?): Loader<ResponseList<SavedSearch>?> {
return SavedSearchesLoader(activity!!, accountKey)
}
override fun onItemLongClick(view: AdapterView<*>, child: View, position: Int, id: Long): Boolean {
val item = adapter.findItem(id) ?: return false
DestroySavedSearchDialogFragment.show(fragmentManager!!, accountKey, item.id, item.name)
@ -97,22 +104,9 @@ class SavedSearchesListFragment : AbsContentListViewFragment<SavedSearchesAdapte
openTweetSearch(activity!!, accountKey, item.query)
}
override fun onLoaderReset(loader: Loader<ResponseList<SavedSearch>?>) {
adapter.setData(null)
}
override fun onLoadFinished(loader: Loader<ResponseList<SavedSearch>?>, data: ResponseList<SavedSearch>?) {
if (data != null) {
Collections.sort(data, positionComparator)
}
adapter.setData(data)
showContent()
refreshing = false
}
override fun onRefresh() {
if (refreshing) return
loaderManager.restartLoader(0, null, this)
savedSearchesLiveData.load()
}
@Subscribe