AndroidAPS/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.kt

202 lines
8 KiB
Kotlin
Raw Normal View History

2020-03-21 13:45:08 +01:00
package info.nightscout.androidaps.setupwizard
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.MainActivity
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity
2021-01-25 12:09:22 +01:00
import info.nightscout.androidaps.databinding.ActivitySetupwizardBinding
2021-04-29 20:42:45 +02:00
import info.nightscout.androidaps.events.EventProfileSwitchChanged
2020-03-21 13:45:08 +01:00
import info.nightscout.androidaps.events.EventProfileStoreChanged
import info.nightscout.androidaps.events.EventPumpStatusChanged
2021-02-09 17:57:28 +01:00
import info.nightscout.androidaps.logging.UserEntryLogger
2021-10-15 14:56:22 +02:00
import info.nightscout.androidaps.plugins.bus.RxBus
2020-03-21 13:45:08 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus
import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin
import info.nightscout.androidaps.plugins.pump.common.events.EventRileyLinkDeviceStatusChange
2020-03-21 13:45:08 +01:00
import info.nightscout.androidaps.setupwizard.elements.SWItem
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
import info.nightscout.androidaps.utils.FabricPrivacy
2021-02-09 17:57:28 +01:00
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.locale.LocaleHelper.update
2021-02-04 20:54:09 +01:00
import info.nightscout.androidaps.utils.rx.AapsSchedulers
2020-03-21 13:45:08 +01:00
import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.disposables.CompositeDisposable
import javax.inject.Inject
import kotlin.math.max
import kotlin.math.min
class SetupWizardActivity : NoSplashAppCompatActivity() {
@Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var localProfilePlugin: LocalProfilePlugin
@Inject lateinit var swDefinition: SWDefinition
2021-10-15 14:56:22 +02:00
@Inject lateinit var rxBus: RxBus
2020-03-21 13:45:08 +01:00
@Inject lateinit var sp: SP
@Inject lateinit var fabricPrivacy: FabricPrivacy
2021-02-04 20:54:09 +01:00
@Inject lateinit var aapsSchedulers: AapsSchedulers
2021-02-09 17:57:28 +01:00
@Inject lateinit var uel: UserEntryLogger
2020-03-21 13:45:08 +01:00
private val disposable = CompositeDisposable()
private lateinit var screens: List<SWScreen>
private var currentWizardPage = 0
private val intentMessage = "WIZZARDPAGE"
2021-01-25 12:09:22 +01:00
private lateinit var binding: ActivitySetupwizardBinding
2020-03-21 13:45:08 +01:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
update(applicationContext)
2021-01-25 12:09:22 +01:00
binding = ActivitySetupwizardBinding.inflate(layoutInflater)
setContentView(binding.root)
2020-03-21 13:45:08 +01:00
screens = swDefinition.getScreens()
val intent = intent
currentWizardPage = intent.getIntExtra(intentMessage, 0)
if (screens.isNotEmpty() && currentWizardPage < screens.size) {
val currentScreen = screens[currentWizardPage]
//Set screen name
val screenName = findViewById<TextView>(R.id.sw_content)
screenName.text = currentScreen.getHeader()
swDefinition.activity = this
//Generate layout first
generateLayout()
updateButtons()
}
}
2021-10-01 11:07:16 +02:00
override fun onPause() {
2020-03-21 13:45:08 +01:00
super.onPause()
disposable.clear()
}
override fun onResume() {
super.onResume()
swDefinition.activity = this
disposable.add(rxBus
.toObservable(EventPumpStatusChanged::class.java)
2021-02-04 20:54:09 +01:00
.observeOn(aapsSchedulers.main)
.subscribe({ updateButtons() }, fabricPrivacy::logException)
2020-03-21 13:45:08 +01:00
)
disposable.add(rxBus
.toObservable(EventRileyLinkDeviceStatusChange::class.java)
2021-02-04 20:54:09 +01:00
.observeOn(aapsSchedulers.main)
.subscribe({ updateButtons() }, fabricPrivacy::logException)
)
2020-03-21 13:45:08 +01:00
disposable.add(rxBus
.toObservable(EventNSClientStatus::class.java)
2021-02-04 20:54:09 +01:00
.observeOn(aapsSchedulers.main)
.subscribe({ updateButtons() }, fabricPrivacy::logException)
2020-03-21 13:45:08 +01:00
)
disposable.add(rxBus
2021-04-29 20:42:45 +02:00
.toObservable(EventProfileSwitchChanged::class.java)
2021-02-04 20:54:09 +01:00
.observeOn(aapsSchedulers.main)
.subscribe({ updateButtons() }, fabricPrivacy::logException)
2020-03-21 13:45:08 +01:00
)
disposable.add(rxBus
.toObservable(EventProfileStoreChanged::class.java)
2021-02-04 20:54:09 +01:00
.observeOn(aapsSchedulers.main)
.subscribe({ updateButtons() }, fabricPrivacy::logException)
2020-03-21 13:45:08 +01:00
)
disposable.add(rxBus
.toObservable(EventSWUpdate::class.java)
2021-02-04 20:54:09 +01:00
.observeOn(aapsSchedulers.main)
2020-03-21 13:45:08 +01:00
.subscribe({ event: EventSWUpdate ->
if (event.redraw) generateLayout()
updateButtons()
2021-02-04 20:54:09 +01:00
}, fabricPrivacy::logException)
2020-03-21 13:45:08 +01:00
)
updateButtons()
2020-03-21 13:45:08 +01:00
}
private fun generateLayout() {
val currentScreen = screens[currentWizardPage]
val layout = SWItem(injector, SWItem.Type.NONE).generateLayout(findViewById(R.id.sw_content_fields))
for (i in currentScreen.items.indices) {
val currentItem = currentScreen.items[i]
currentItem.generateDialog(layout)
}
2021-01-25 12:09:22 +01:00
binding.swScrollview.smoothScrollTo(0, 0)
2020-03-21 13:45:08 +01:00
}
2021-01-18 22:47:55 +01:00
override fun updateButtons() {
2020-03-21 13:45:08 +01:00
runOnUiThread {
val currentScreen = screens[currentWizardPage]
2021-09-20 13:04:13 +02:00
if (currentScreen.validator == null || currentScreen.validator?.isValid == true || currentScreen.skippable) {
2020-03-21 13:45:08 +01:00
if (currentWizardPage == nextPage(null)) {
findViewById<View>(R.id.finish_button).visibility = View.VISIBLE
findViewById<View>(R.id.next_button).visibility = View.GONE
} else {
findViewById<View>(R.id.finish_button).visibility = View.GONE
findViewById<View>(R.id.next_button).visibility = View.VISIBLE
}
} else {
findViewById<View>(R.id.finish_button).visibility = View.GONE
findViewById<View>(R.id.next_button).visibility = View.GONE
}
if (currentWizardPage == 0) findViewById<View>(R.id.previous_button).visibility = View.GONE else findViewById<View>(R.id.previous_button).visibility = View.VISIBLE
currentScreen.processVisibility()
}
}
override fun onBackPressed() {
2021-11-02 09:53:16 +01:00
if (currentWizardPage == 0) OKDialog.showConfirmation(this, rh.gs(R.string.exitwizard)) { finish() } else showPreviousPage(null)
2020-03-21 13:45:08 +01:00
}
@Suppress("UNUSED_PARAMETER")
fun exitPressed(view: View?) {
sp.putBoolean(R.string.key_setupwizard_processed, true)
2021-11-02 09:53:16 +01:00
OKDialog.showConfirmation(this, rh.gs(R.string.exitwizard)) { finish() }
2020-03-21 13:45:08 +01:00
}
@Suppress("UNUSED_PARAMETER")
fun showNextPage(view: View?) {
finish()
val intent = Intent(this, SetupWizardActivity::class.java)
intent.putExtra(intentMessage, nextPage(null))
startActivity(intent)
}
@Suppress("UNUSED_PARAMETER")
fun showPreviousPage(view: View?) {
finish()
val intent = Intent(this, SetupWizardActivity::class.java)
intent.putExtra(intentMessage, previousPage(null))
startActivity(intent)
}
// Go back to overview
@Suppress("UNUSED_PARAMETER")
fun finishSetupWizard(view: View?) {
sp.putBoolean(R.string.key_setupwizard_processed, true)
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
finish()
}
@Suppress("UNUSED_PARAMETER", "SameParameterValue")
2020-03-21 13:45:08 +01:00
private fun nextPage(view: View?): Int {
var page = currentWizardPage + 1
while (page < screens.size) {
2021-09-20 13:04:13 +02:00
if (screens[page].visibility == null || screens[page].visibility?.isValid == true) return page
2020-03-21 13:45:08 +01:00
page++
}
return min(currentWizardPage, screens.size - 1)
}
@Suppress("UNUSED_PARAMETER", "SameParameterValue")
2020-03-21 13:45:08 +01:00
private fun previousPage(view: View?): Int {
var page = currentWizardPage - 1
while (page >= 0) {
2021-09-20 13:04:13 +02:00
if (screens[page].visibility == null || screens[page].visibility?.isValid == true) return page
2020-03-21 13:45:08 +01:00
page--
}
return max(currentWizardPage, 0)
}
}