|
| 1 | +package com.monstarlab.features.main |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.net.Uri |
| 5 | +import androidx.appcompat.app.AlertDialog |
| 6 | +import androidx.lifecycle.lifecycleScope |
| 7 | +import dk.nodes.nstack.kotlin.NStack |
| 8 | +import dk.nodes.nstack.kotlin.models.AppUpdate |
| 9 | +import dk.nodes.nstack.kotlin.models.AppUpdateState |
| 10 | +import dk.nodes.nstack.kotlin.models.Message |
| 11 | +import dk.nodes.nstack.kotlin.models.RateReminder |
| 12 | +import dk.nodes.nstack.kotlin.models.Result |
| 13 | +import dk.nodes.nstack.kotlin.models.state |
| 14 | +import dk.nodes.nstack.kotlin.models.update |
| 15 | +import kotlinx.coroutines.Dispatchers |
| 16 | +import kotlinx.coroutines.launch |
| 17 | +import kotlinx.coroutines.withContext |
| 18 | + |
| 19 | +fun MainActivity.setupNStack() = lifecycleScope.launch(Dispatchers.IO) { |
| 20 | + when (val result = NStack.appOpen()) { |
| 21 | + is Result.Success -> { |
| 22 | + withContext(Dispatchers.Main) { |
| 23 | + when (result.value.data.update.state) { |
| 24 | + AppUpdateState.NONE -> { |
| 25 | + /* Nothing to do */ |
| 26 | + } |
| 27 | + AppUpdateState.UPDATE -> showUpdateDialog(result.value.data.update) |
| 28 | + AppUpdateState.FORCE -> showForceDialog(result.value.data.update) |
| 29 | + AppUpdateState.CHANGELOG -> showChangelogDialog(result.value.data.update) |
| 30 | + } |
| 31 | + |
| 32 | + result.value.data.message?.let { showMessageDialog(it) } |
| 33 | + result.value.data.rateReminder?.let { showRateReminderDialog(it) } |
| 34 | + } |
| 35 | + } |
| 36 | + is Result.Error -> { |
| 37 | + } |
| 38 | + } |
| 39 | + // if (withContext(Dispatchers.IO) { NStack.RateReminder.shouldShow() }) { |
| 40 | + // val answer = NStack.RateReminder.apply { |
| 41 | + // title = Translation.rate.title |
| 42 | + // message = Translation.rate.message |
| 43 | + // yesButton = Translation.rate.yesButton |
| 44 | + // noButton = Translation.rate.noButton |
| 45 | + // skipButton = Translation.rate.skipButton |
| 46 | + // }.show(ContextThemeWrapper(context, R.style.customDialog)) |
| 47 | + |
| 48 | + // when (answer) { |
| 49 | + // RateReminderAnswer.POSITIVE -> // take user to the playstore page |
| 50 | + // RateReminderAnswer.NEGATIVE -> // take user to the feedback screen |
| 51 | + // RateReminderAnswer.SKIP -> // do nothing? |
| 52 | + // } |
| 53 | + // } |
| 54 | +} |
| 55 | + |
| 56 | +fun MainActivity.showRateReminderDialog(rateReminder: RateReminder) { |
| 57 | + AlertDialog.Builder(this) |
| 58 | + .setMessage(rateReminder.body) |
| 59 | + .setTitle(rateReminder.title) |
| 60 | + .setCancelable(false) |
| 61 | + .setPositiveButton(rateReminder.yesButton) { dialog, _ -> |
| 62 | + startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(rateReminder.link))) |
| 63 | + dialog.dismiss() |
| 64 | + } |
| 65 | + .setNegativeButton(rateReminder.noButton) { dialog, _ -> |
| 66 | + dialog.dismiss() |
| 67 | + } |
| 68 | + .setNeutralButton(rateReminder.laterButton) { dialog, _ -> |
| 69 | + dialog.dismiss() |
| 70 | + } |
| 71 | + .show() |
| 72 | +} |
| 73 | + |
| 74 | +fun MainActivity.showMessageDialog(message: Message) { |
| 75 | + AlertDialog.Builder(this) |
| 76 | + .setMessage(message.message) |
| 77 | + .setCancelable(false) |
| 78 | + .setPositiveButton("Translation.defaultSection.ok") { dialog, _ -> |
| 79 | + NStack.messageSeen(message) |
| 80 | + dialog.dismiss() |
| 81 | + } |
| 82 | + .show() |
| 83 | +} |
| 84 | + |
| 85 | +fun MainActivity.showUpdateDialog(appUpdate: AppUpdate) { |
| 86 | + AlertDialog.Builder(this) |
| 87 | + .setTitle(appUpdate.update?.translate?.title ?: return) |
| 88 | + .setMessage(appUpdate.update?.translate?.message ?: return) |
| 89 | + .setPositiveButton(appUpdate.update?.translate?.positiveButton) { dialog, _ -> |
| 90 | + dialog.dismiss() |
| 91 | + } |
| 92 | + .show() |
| 93 | +} |
| 94 | + |
| 95 | +fun MainActivity.showChangelogDialog(appUpdate: AppUpdate) { |
| 96 | + AlertDialog.Builder(this) |
| 97 | + .setTitle(appUpdate.update?.translate?.title ?: return) |
| 98 | + .setMessage(appUpdate.update?.translate?.message ?: return) |
| 99 | + .setNegativeButton(appUpdate.update?.translate?.negativeButton ?: return) { dialog, _ -> |
| 100 | + dialog.dismiss() |
| 101 | + } |
| 102 | + .show() |
| 103 | +} |
| 104 | + |
| 105 | +fun MainActivity.startPlayStore() { |
| 106 | + try { |
| 107 | + startActivity( |
| 108 | + Intent( |
| 109 | + Intent.ACTION_VIEW, |
| 110 | + Uri.parse("market://details?id=$packageName") |
| 111 | + ) |
| 112 | + ) |
| 113 | + } catch (anfe: android.content.ActivityNotFoundException) { |
| 114 | + startActivity( |
| 115 | + Intent( |
| 116 | + Intent.ACTION_VIEW, |
| 117 | + Uri.parse("https://play.google.com/store/apps/details?id=$packageName") |
| 118 | + ) |
| 119 | + ) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +fun MainActivity.showForceDialog(appUpdate: AppUpdate) { |
| 124 | + val dialog = AlertDialog.Builder(this) |
| 125 | + .setTitle(appUpdate.update?.translate?.title ?: return) |
| 126 | + .setMessage(appUpdate.update?.translate?.message ?: return) |
| 127 | + .setCancelable(false) |
| 128 | + .setPositiveButton(appUpdate.update?.translate?.positiveButton, null) |
| 129 | + .create() |
| 130 | + |
| 131 | + dialog.setOnShowListener { |
| 132 | + val b = dialog.getButton(AlertDialog.BUTTON_POSITIVE) |
| 133 | + b.setOnClickListener { |
| 134 | + startPlayStore() |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + dialog.show() |
| 139 | +} |
0 commit comments