diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/data/SavedSearchesLiveData.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/data/SavedSearchesLiveData.kt new file mode 100644 index 000000000..b037661a5 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/data/SavedSearchesLiveData.kt @@ -0,0 +1,39 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2014 Mariotaku Lee + * + * 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 . + */ + +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>(false) { + override fun compute(): ResponseList { + val twitter = AccountManager.get(context).getDetailsOrThrow(accountKey, true) + .newMicroBlogInstance(context, Twitter::class.java) + return twitter.savedSearches + } + +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/SavedSearchesListFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/SavedSearchesListFragment.kt index 519851ad1..52ddd984c 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/SavedSearchesListFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/SavedSearchesListFragment.kt @@ -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(), - LoaderCallbacks?>, AdapterView.OnItemClickListener, - AdapterView.OnItemLongClickListener { + AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener { override var refreshing: Boolean get() = loaderManager.hasRunningLoadersSafe() @@ -56,15 +53,29 @@ class SavedSearchesListFragment : AbsContentListViewFragment { 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?> { - 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?>) { - adapter.setData(null) - } - - override fun onLoadFinished(loader: Loader?>, data: ResponseList?) { - 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