2016-12-31 18:13:21 +01:00
|
|
|
package org.mariotaku.twidere.fragment.filter
|
|
|
|
|
2017-01-18 18:48:29 +01:00
|
|
|
import android.app.Activity
|
2017-01-12 17:26:44 +01:00
|
|
|
import android.app.Dialog
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.DialogInterface
|
2017-01-18 18:48:29 +01:00
|
|
|
import android.content.Intent
|
2017-01-12 17:26:44 +01:00
|
|
|
import android.database.Cursor
|
2016-12-31 18:13:21 +01:00
|
|
|
import android.os.Bundle
|
2017-01-18 18:48:29 +01:00
|
|
|
import android.support.v4.app.DialogFragment
|
2017-01-12 17:26:44 +01:00
|
|
|
import android.support.v4.app.LoaderManager
|
|
|
|
import android.support.v4.content.CursorLoader
|
|
|
|
import android.support.v4.content.Loader
|
|
|
|
import android.support.v4.widget.SimpleCursorAdapter
|
|
|
|
import android.support.v7.app.AlertDialog
|
|
|
|
import android.view.*
|
2017-01-12 18:08:10 +01:00
|
|
|
import android.widget.TextView
|
2017-01-12 17:26:44 +01:00
|
|
|
import com.rengwuxian.materialedittext.MaterialEditText
|
|
|
|
import kotlinx.android.synthetic.main.layout_list_with_empty_view.*
|
2017-01-12 17:34:45 +01:00
|
|
|
import okhttp3.HttpUrl
|
2017-01-12 17:26:44 +01:00
|
|
|
import org.mariotaku.abstask.library.TaskStarter
|
2017-01-18 18:48:29 +01:00
|
|
|
import org.mariotaku.ktextension.Bundle
|
2017-01-12 17:26:44 +01:00
|
|
|
import org.mariotaku.ktextension.empty
|
|
|
|
import org.mariotaku.ktextension.isEmpty
|
2017-01-18 18:48:29 +01:00
|
|
|
import org.mariotaku.ktextension.set
|
2017-01-12 17:26:44 +01:00
|
|
|
import org.mariotaku.twidere.R
|
2017-01-18 18:48:29 +01:00
|
|
|
import org.mariotaku.twidere.TwidereConstants.REQUEST_PURCHASE_EXTRA_FEATURES
|
|
|
|
import org.mariotaku.twidere.constant.IntentConstants.EXTRA_ACTION
|
|
|
|
import org.mariotaku.twidere.constant.IntentConstants.INTENT_PACKAGE_PREFIX
|
2017-01-12 18:08:10 +01:00
|
|
|
import org.mariotaku.twidere.extension.model.getComponentLabel
|
2017-01-12 17:26:44 +01:00
|
|
|
import org.mariotaku.twidere.extension.model.setupUrl
|
|
|
|
import org.mariotaku.twidere.fragment.BaseDialogFragment
|
|
|
|
import org.mariotaku.twidere.fragment.BaseFragment
|
2017-01-18 18:48:29 +01:00
|
|
|
import org.mariotaku.twidere.fragment.ExtraFeaturesIntroductionDialogFragment
|
2017-01-12 17:26:44 +01:00
|
|
|
import org.mariotaku.twidere.fragment.ProgressDialogFragment
|
|
|
|
import org.mariotaku.twidere.model.FiltersSubscription
|
2017-01-12 18:08:10 +01:00
|
|
|
import org.mariotaku.twidere.model.FiltersSubscriptionCursorIndices
|
2017-01-12 17:26:44 +01:00
|
|
|
import org.mariotaku.twidere.model.FiltersSubscriptionValuesCreator
|
2017-01-18 18:48:29 +01:00
|
|
|
import org.mariotaku.twidere.model.analyzer.PurchaseFinished
|
2017-01-12 17:26:44 +01:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Filters
|
|
|
|
import org.mariotaku.twidere.task.filter.RefreshFiltersSubscriptionsTask
|
2017-01-18 18:48:29 +01:00
|
|
|
import org.mariotaku.twidere.util.Analyzer
|
|
|
|
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
|
2017-01-12 17:26:44 +01:00
|
|
|
import org.mariotaku.twidere.util.view.SimpleTextWatcher
|
|
|
|
import java.lang.ref.WeakReference
|
2016-12-31 18:13:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 2016/12/31.
|
|
|
|
*/
|
2017-01-12 17:26:44 +01:00
|
|
|
class FiltersSubscriptionsFragment : BaseFragment(), LoaderManager.LoaderCallbacks<Cursor> {
|
|
|
|
|
|
|
|
private lateinit var adapter: FilterSubscriptionsAdapter
|
|
|
|
|
2016-12-31 18:13:21 +01:00
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
|
super.onActivityCreated(savedInstanceState)
|
2017-01-12 17:26:44 +01:00
|
|
|
setHasOptionsMenu(true)
|
|
|
|
|
|
|
|
adapter = FilterSubscriptionsAdapter(context)
|
|
|
|
listView.adapter = adapter
|
|
|
|
|
|
|
|
listContainer.visibility = View.GONE
|
|
|
|
progressContainer.visibility = View.VISIBLE
|
|
|
|
loaderManager.initLoader(0, null, this)
|
2017-01-18 18:48:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
if (!extraFeaturesService.isSupported()) {
|
|
|
|
activity?.finish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (savedInstanceState == null) {
|
|
|
|
when (arguments?.getString(EXTRA_ACTION)) {
|
|
|
|
ACTION_ADD_URL_SUBSCRIPTION -> {
|
2017-01-20 15:31:29 +01:00
|
|
|
if (!extraFeaturesService.isEnabled(ExtraFeaturesService.FEATURE_FILTERS_SUBSCRIPTION)) {
|
2017-01-18 18:48:29 +01:00
|
|
|
val df = ExtraFeaturesIntroductionDialogFragment.show(childFragmentManager,
|
|
|
|
ExtraFeaturesService.FEATURE_FILTERS_SUBSCRIPTION)
|
|
|
|
df.setTargetFragment(this, REQUEST_ADD_URL_SUBSCRIPTION_PURCHASE)
|
|
|
|
} else {
|
|
|
|
showAddUrlSubscription()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else -> {
|
2017-01-20 15:31:29 +01:00
|
|
|
if (!extraFeaturesService.isEnabled(ExtraFeaturesService.FEATURE_FILTERS_SUBSCRIPTION)) {
|
|
|
|
val df = ExtraFeaturesIntroductionDialogFragment.show(childFragmentManager,
|
|
|
|
ExtraFeaturesService.FEATURE_FILTERS_SUBSCRIPTION)
|
|
|
|
df.setTargetFragment(this, REQUEST_PURCHASE_EXTRA_FEATURES)
|
|
|
|
}
|
2017-01-18 18:48:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
when (requestCode) {
|
|
|
|
REQUEST_ADD_URL_SUBSCRIPTION_PURCHASE -> {
|
|
|
|
if (resultCode == Activity.RESULT_OK) {
|
|
|
|
Analyzer.log(PurchaseFinished.create(data!!))
|
|
|
|
executeAfterFragmentResumed { fragment ->
|
|
|
|
(fragment as FiltersSubscriptionsFragment).showAddUrlSubscription()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
activity?.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
REQUEST_PURCHASE_EXTRA_FEATURES -> {
|
|
|
|
if (resultCode == Activity.RESULT_OK) {
|
|
|
|
Analyzer.log(PurchaseFinished.create(data!!))
|
|
|
|
} else {
|
|
|
|
activity?.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 17:26:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
|
|
|
inflater.inflate(R.menu.menu_filters_subscriptions, menu)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
when (item.itemId) {
|
|
|
|
R.id.add -> {
|
|
|
|
val df = AddUrlSubscriptionDialogFragment()
|
|
|
|
df.show(fragmentManager, "add_url_subscription")
|
|
|
|
return true
|
|
|
|
}
|
2017-01-12 17:34:45 +01:00
|
|
|
R.id.refresh -> {
|
2017-01-18 18:48:29 +01:00
|
|
|
executeAfterFragmentResumed { fragment ->
|
|
|
|
val dfRef = WeakReference(ProgressDialogFragment.show(fragment.childFragmentManager, "refresh_filters"))
|
|
|
|
val task = RefreshFiltersSubscriptionsTask(fragment.context)
|
|
|
|
val fragmentRef = WeakReference(fragment)
|
|
|
|
task.callback = {
|
|
|
|
fragmentRef.get()?.executeAfterFragmentResumed { fragment ->
|
|
|
|
val df = dfRef.get() ?: fragment.childFragmentManager.findFragmentByTag("refresh_filters") as? DialogFragment
|
|
|
|
df?.dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TaskStarter.execute(task)
|
2017-01-12 17:26:44 +01:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
else -> return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
return inflater.inflate(R.layout.layout_list_with_empty_view, container, false)
|
|
|
|
}
|
2016-12-31 18:13:21 +01:00
|
|
|
|
2017-01-12 17:26:44 +01:00
|
|
|
override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {
|
|
|
|
val loader = CursorLoader(context)
|
|
|
|
loader.uri = Filters.Subscriptions.CONTENT_URI
|
|
|
|
loader.projection = Filters.Subscriptions.COLUMNS
|
|
|
|
return loader
|
2016-12-31 18:13:21 +01:00
|
|
|
}
|
2017-01-12 17:26:44 +01:00
|
|
|
|
|
|
|
override fun onLoaderReset(loader: Loader<Cursor>) {
|
|
|
|
adapter.changeCursor(null)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor) {
|
|
|
|
adapter.changeCursor(cursor)
|
|
|
|
if (cursor.isEmpty) {
|
|
|
|
listView.visibility = View.GONE
|
|
|
|
emptyView.visibility = View.VISIBLE
|
|
|
|
emptyIcon.setImageResource(R.drawable.ic_info_info_generic)
|
|
|
|
emptyText.setText(R.string.hint_empty_filters_subscriptions)
|
|
|
|
} else {
|
|
|
|
listView.visibility = View.VISIBLE
|
|
|
|
emptyView.visibility = View.GONE
|
|
|
|
}
|
|
|
|
listContainer.visibility = View.VISIBLE
|
|
|
|
progressContainer.visibility = View.GONE
|
|
|
|
}
|
|
|
|
|
2017-01-18 18:48:29 +01:00
|
|
|
private fun showAddUrlSubscription() {
|
|
|
|
val df = AddUrlSubscriptionDialogFragment()
|
|
|
|
df.arguments = Bundle {
|
|
|
|
this[EXTRA_ADD_SUBSCRIPTION_URL] = arguments?.getString(EXTRA_ADD_SUBSCRIPTION_URL)
|
|
|
|
this[EXTRA_ADD_SUBSCRIPTION_NAME] = arguments?.getString(EXTRA_ADD_SUBSCRIPTION_NAME)
|
|
|
|
}
|
|
|
|
df.show(fragmentManager, "add_url_subscription")
|
|
|
|
}
|
|
|
|
|
2017-01-12 17:26:44 +01:00
|
|
|
class FilterSubscriptionsAdapter(context: Context) : SimpleCursorAdapter(context,
|
2017-01-12 18:08:10 +01:00
|
|
|
R.layout.list_item_two_line, null, arrayOf(Filters.Subscriptions.NAME),
|
2017-01-12 17:26:44 +01:00
|
|
|
intArrayOf(android.R.id.text1), 0) {
|
2017-01-12 18:08:10 +01:00
|
|
|
private var indices: FiltersSubscriptionCursorIndices? = null
|
|
|
|
private var tempObject: FiltersSubscription = FiltersSubscription()
|
|
|
|
|
|
|
|
override fun swapCursor(c: Cursor?): Cursor? {
|
|
|
|
indices = if (c != null) FiltersSubscriptionCursorIndices(c) else null
|
|
|
|
return super.swapCursor(c)
|
|
|
|
}
|
|
|
|
|
2017-01-12 17:26:44 +01:00
|
|
|
override fun bindView(view: View, context: Context, cursor: Cursor) {
|
|
|
|
super.bindView(view, context, cursor)
|
2017-01-12 18:08:10 +01:00
|
|
|
val indices = this.indices!!
|
2017-01-12 17:26:44 +01:00
|
|
|
val iconView = view.findViewById(android.R.id.icon)
|
2017-01-12 18:08:10 +01:00
|
|
|
val summaryView = view.findViewById(android.R.id.text2) as TextView
|
|
|
|
|
|
|
|
indices.parseFields(tempObject, cursor)
|
|
|
|
|
2017-01-12 17:26:44 +01:00
|
|
|
iconView.visibility = View.GONE
|
2017-01-12 18:08:10 +01:00
|
|
|
summaryView.text = tempObject.getComponentLabel(context)
|
2017-01-12 17:26:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AddUrlSubscriptionDialogFragment : BaseDialogFragment() {
|
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
val builder = AlertDialog.Builder(context)
|
|
|
|
builder.setView(R.layout.dialog_add_filters_subscription)
|
|
|
|
builder.setPositiveButton(R.string.action_add_filters_subscription) { dialog, which ->
|
|
|
|
dialog as AlertDialog
|
|
|
|
val editName = dialog.findViewById(R.id.name) as MaterialEditText
|
|
|
|
val editUrl = dialog.findViewById(R.id.url) as MaterialEditText
|
|
|
|
val subscription = FiltersSubscription()
|
|
|
|
subscription.name = editName.text.toString()
|
|
|
|
subscription.setupUrl(editUrl.text.toString())
|
|
|
|
context.contentResolver.insert(Filters.Subscriptions.CONTENT_URI, FiltersSubscriptionValuesCreator.create(subscription))
|
|
|
|
}
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
val dialog = builder.create()
|
|
|
|
dialog.setOnShowListener { dialog ->
|
|
|
|
dialog as AlertDialog
|
|
|
|
val editName = dialog.findViewById(R.id.name) as MaterialEditText
|
|
|
|
val editUrl = dialog.findViewById(R.id.url) as MaterialEditText
|
|
|
|
val positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE)
|
|
|
|
|
|
|
|
fun updateEnableState() {
|
2017-01-12 17:34:45 +01:00
|
|
|
val nameValid = !editName.empty
|
|
|
|
val urlValid = HttpUrl.parse(editUrl.text.toString()) != null
|
|
|
|
positiveButton.isEnabled = nameValid && urlValid
|
2017-01-12 17:26:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
val watcher = object : SimpleTextWatcher() {
|
|
|
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
|
|
updateEnableState()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
editName.addTextChangedListener(watcher)
|
|
|
|
editUrl.addTextChangedListener(watcher)
|
2017-01-18 18:48:29 +01:00
|
|
|
|
|
|
|
val args = arguments
|
|
|
|
if (savedInstanceState == null && args != null) {
|
|
|
|
editName.setText(args.getString(EXTRA_ADD_SUBSCRIPTION_NAME))
|
|
|
|
editUrl.setText(args.getString(EXTRA_ADD_SUBSCRIPTION_URL))
|
|
|
|
}
|
|
|
|
|
2017-01-12 17:26:44 +01:00
|
|
|
updateEnableState()
|
|
|
|
}
|
|
|
|
return dialog
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 18:48:29 +01:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val ACTION_ADD_URL_SUBSCRIPTION = "${INTENT_PACKAGE_PREFIX}ADD_URL_FILTERS_SUBSCRIPTION"
|
|
|
|
const val REQUEST_ADD_URL_SUBSCRIPTION_PURCHASE = 101
|
|
|
|
const val EXTRA_ADD_SUBSCRIPTION_URL = "add_subscription.url"
|
|
|
|
const val EXTRA_ADD_SUBSCRIPTION_NAME = "add_subscription.name"
|
|
|
|
}
|
2017-01-12 17:26:44 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 18:08:10 +01:00
|
|
|
|