2016-12-25 17:20:08 +01:00
|
|
|
package org.mariotaku.twidere.fragment
|
|
|
|
|
2017-01-18 18:48:29 +01:00
|
|
|
import android.app.Activity
|
2016-12-25 17:20:08 +01:00
|
|
|
import android.app.Dialog
|
2017-01-18 18:48:29 +01:00
|
|
|
import android.content.DialogInterface
|
|
|
|
import android.content.Intent
|
2016-12-25 17:20:08 +01:00
|
|
|
import android.os.Bundle
|
2017-01-09 06:16:23 +01:00
|
|
|
import android.support.v4.app.FragmentManager
|
2016-12-25 17:20:08 +01:00
|
|
|
import android.support.v7.app.AlertDialog
|
2016-12-26 04:25:55 +01:00
|
|
|
import android.view.View
|
2017-01-09 06:16:23 +01:00
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.TextView
|
|
|
|
import org.mariotaku.ktextension.Bundle
|
|
|
|
import org.mariotaku.ktextension.set
|
2016-12-25 17:20:08 +01:00
|
|
|
import org.mariotaku.twidere.R
|
2017-01-07 11:02:32 +01:00
|
|
|
import org.mariotaku.twidere.constant.IntentConstants.EXTRA_REQUEST_CODE
|
2017-02-05 14:42:20 +01:00
|
|
|
import org.mariotaku.twidere.extension.applyTheme
|
2017-01-07 12:16:17 +01:00
|
|
|
import org.mariotaku.twidere.model.analyzer.PurchaseConfirm
|
|
|
|
import org.mariotaku.twidere.model.analyzer.PurchaseFinished
|
|
|
|
import org.mariotaku.twidere.model.analyzer.PurchaseIntroduction
|
|
|
|
import org.mariotaku.twidere.util.Analyzer
|
2017-01-01 16:28:00 +01:00
|
|
|
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
|
2016-12-25 17:20:08 +01:00
|
|
|
|
|
|
|
/**
|
2017-03-21 14:36:06 +01:00
|
|
|
* Show extra features introduction
|
2016-12-25 17:20:08 +01:00
|
|
|
* Created by mariotaku on 2016/12/25.
|
|
|
|
*/
|
|
|
|
class ExtraFeaturesIntroductionDialogFragment : BaseDialogFragment() {
|
2016-12-26 04:25:55 +01:00
|
|
|
|
2017-01-09 06:16:23 +01:00
|
|
|
val feature: String get() = arguments.getString(EXTRA_FEATURE)
|
2017-02-02 17:27:21 +01:00
|
|
|
val source: String? get() = arguments.getString(EXTRA_SOURCE)
|
2017-01-09 06:16:23 +01:00
|
|
|
val requestCode: Int get() = arguments.getInt(EXTRA_REQUEST_CODE, 0)
|
|
|
|
|
2016-12-25 17:20:08 +01:00
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
val builder = AlertDialog.Builder(context)
|
|
|
|
builder.setTitle(R.string.title_extra_features)
|
|
|
|
builder.setView(R.layout.dialog_extra_features_introduction)
|
2017-03-21 14:36:06 +01:00
|
|
|
builder.setPositiveButton(R.string.action_purchase) { _, _ ->
|
2017-01-09 06:16:23 +01:00
|
|
|
startPurchase(feature)
|
2017-01-07 12:16:17 +01:00
|
|
|
Analyzer.log(PurchaseConfirm(PurchaseFinished.NAME_EXTRA_FEATURES))
|
2016-12-25 17:20:08 +01:00
|
|
|
}
|
2017-03-21 14:36:06 +01:00
|
|
|
builder.setNegativeButton(R.string.action_later) { _, _ ->
|
2017-01-18 18:48:29 +01:00
|
|
|
onDialogCancelled()
|
2016-12-25 17:20:08 +01:00
|
|
|
}
|
2017-01-09 06:16:23 +01:00
|
|
|
val restorePurchaseIntent = extraFeaturesService.createRestorePurchaseIntent(context, feature)
|
2016-12-26 04:25:55 +01:00
|
|
|
if (restorePurchaseIntent != null) {
|
2017-03-21 14:36:06 +01:00
|
|
|
builder.setNeutralButton(R.string.action_restore_purchase) { _, _ ->
|
2017-01-18 18:48:29 +01:00
|
|
|
startActivityForResultOnTarget(restorePurchaseIntent)
|
2016-12-26 04:25:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
val dialog = builder.create()
|
|
|
|
dialog.setOnShowListener {
|
2017-02-05 14:42:20 +01:00
|
|
|
it as AlertDialog
|
|
|
|
it.applyTheme()
|
|
|
|
it.findViewById(R.id.restorePurchaseHint)?.visibility = if (restorePurchaseIntent != null) {
|
2016-12-26 04:25:55 +01:00
|
|
|
View.VISIBLE
|
|
|
|
} else {
|
|
|
|
View.GONE
|
|
|
|
}
|
2017-01-09 06:16:23 +01:00
|
|
|
val description = ExtraFeaturesService.getIntroduction(context, feature)
|
|
|
|
val featureIcon = it.findViewById(R.id.featureIcon) as ImageView
|
|
|
|
val featureDescription = it.findViewById(R.id.featureDescription) as TextView
|
|
|
|
featureIcon.setImageResource(description.icon)
|
|
|
|
featureDescription.text = description.description
|
2017-02-05 14:42:20 +01:00
|
|
|
it.findViewById(R.id.buyFeaturesPack)?.setOnClickListener {
|
2017-01-09 06:16:23 +01:00
|
|
|
startPurchase(ExtraFeaturesService.FEATURE_FEATURES_PACK)
|
|
|
|
dismiss()
|
|
|
|
}
|
2016-12-25 17:20:08 +01:00
|
|
|
}
|
2017-01-07 12:16:17 +01:00
|
|
|
if (savedInstanceState == null) {
|
2017-02-02 05:26:20 +01:00
|
|
|
Analyzer.log(PurchaseIntroduction(feature, source))
|
2017-01-07 12:16:17 +01:00
|
|
|
}
|
2016-12-26 04:25:55 +01:00
|
|
|
return dialog
|
2016-12-25 17:20:08 +01:00
|
|
|
}
|
2017-01-09 06:16:23 +01:00
|
|
|
|
2017-01-18 18:48:29 +01:00
|
|
|
override fun onCancel(dialog: DialogInterface?) {
|
|
|
|
onDialogCancelled()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onDialogCancelled() {
|
|
|
|
if (targetRequestCode != 0) {
|
|
|
|
targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_CANCELED, null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 06:16:23 +01:00
|
|
|
private fun startPurchase(feature: String) {
|
2017-01-18 18:48:29 +01:00
|
|
|
val purchaseIntent = extraFeaturesService.createPurchaseIntent(context, feature) ?: return
|
|
|
|
startActivityForResultOnTarget(purchaseIntent)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun startActivityForResultOnTarget(intent: Intent) {
|
|
|
|
if (targetFragment != null) {
|
|
|
|
targetFragment.startActivityForResult(intent, targetRequestCode)
|
|
|
|
} else if (requestCode == 0) {
|
|
|
|
startActivity(intent)
|
2017-01-09 06:16:23 +01:00
|
|
|
} else if (parentFragment != null) {
|
2017-01-18 18:48:29 +01:00
|
|
|
parentFragment.startActivityForResult(intent, requestCode)
|
2017-01-09 06:16:23 +01:00
|
|
|
} else {
|
2017-01-18 18:48:29 +01:00
|
|
|
activity.startActivityForResult(intent, requestCode)
|
2017-01-09 06:16:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val EXTRA_FEATURE = "feature"
|
2017-02-02 05:26:20 +01:00
|
|
|
const val EXTRA_SOURCE = "source"
|
|
|
|
fun show(fm: FragmentManager, feature: String, source: String? = null, requestCode: Int = 0): ExtraFeaturesIntroductionDialogFragment {
|
2017-01-09 06:16:23 +01:00
|
|
|
val df = ExtraFeaturesIntroductionDialogFragment()
|
|
|
|
df.arguments = Bundle {
|
|
|
|
this[EXTRA_FEATURE] = feature
|
2017-02-02 05:26:20 +01:00
|
|
|
this[EXTRA_SOURCE] = source
|
2017-01-09 06:16:23 +01:00
|
|
|
this[EXTRA_REQUEST_CODE] = requestCode
|
|
|
|
}
|
|
|
|
df.show(fm, "extra_features_introduction")
|
|
|
|
return df
|
|
|
|
}
|
|
|
|
}
|
2016-12-25 17:20:08 +01:00
|
|
|
}
|