SetupWizardActivity -> kt

This commit is contained in:
Milos Kozak 2020-03-21 13:45:08 +01:00
parent f6cf0e72cd
commit 69b177ed06
2 changed files with 215 additions and 236 deletions

View file

@ -1,236 +0,0 @@
package info.nightscout.androidaps.setupwizard;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import java.util.List;
import javax.inject.Inject;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
import info.nightscout.androidaps.events.EventProfileNeedsUpdate;
import info.nightscout.androidaps.events.EventProfileStoreChanged;
import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.plugins.bus.RxBusWrapper;
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus;
import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin;
import info.nightscout.androidaps.setupwizard.elements.SWItem;
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate;
import info.nightscout.androidaps.utils.AndroidPermission;
import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.LocaleHelper;
import info.nightscout.androidaps.utils.OKDialog;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
import info.nightscout.androidaps.utils.sharedPreferences.SP;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
public class SetupWizardActivity extends NoSplashAppCompatActivity {
@Inject HasAndroidInjector injector;
@Inject LocalProfilePlugin localProfilePlugin;
@Inject SWDefinition swDefinition;
@Inject RxBusWrapper rxBus;
@Inject ResourceHelper resourceHelper;
@Inject SP sp;
private CompositeDisposable disposable = new CompositeDisposable();
ScrollView scrollView;
private List<SWScreen> screens;
private int currentWizardPage = 0;
public static final String INTENT_MESSAGE = "WIZZARDPAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocaleHelper.INSTANCE.update(getApplicationContext());
setContentView(R.layout.activity_setupwizard);
scrollView = findViewById(R.id.sw_scrollview);
screens = swDefinition.getScreens();
Intent intent = getIntent();
currentWizardPage = intent.getIntExtra(SetupWizardActivity.INTENT_MESSAGE, 0);
if (screens.size() > 0 && currentWizardPage < screens.size()) {
SWScreen currentScreen = screens.get(currentWizardPage);
//Set screen name
TextView screenName = findViewById(R.id.sw_content);
screenName.setText(currentScreen.getHeader());
swDefinition.setActivity(this);
//Generate layout first
generateLayout();
updateButtons();
}
}
@Override
public void onPause() {
super.onPause();
disposable.clear();
}
@Override
protected void onResume() {
super.onResume();
swDefinition.setActivity(this);
disposable.add(rxBus
.toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateButtons(), exception -> FabricPrivacy.getInstance().logException(exception))
);
disposable.add(rxBus
.toObservable(EventNSClientStatus.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateButtons(), exception -> FabricPrivacy.getInstance().logException(exception))
);
disposable.add(rxBus
.toObservable(EventProfileNeedsUpdate.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateButtons(), exception -> FabricPrivacy.getInstance().logException(exception))
);
disposable.add(rxBus
.toObservable(EventProfileStoreChanged.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateButtons(), exception -> FabricPrivacy.getInstance().logException(exception))
);
disposable.add(rxBus
.toObservable(EventSWUpdate.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
if (event.getRedraw()) generateLayout();
updateButtons();
}, exception -> FabricPrivacy.getInstance().logException(exception))
);
}
private void generateLayout() {
SWScreen currentScreen = screens.get(currentWizardPage);
LinearLayout layout = new SWItem(injector, SWItem.Type.NONE).generateLayout(this.findViewById(R.id.sw_content_fields));
for (int i = 0; i < currentScreen.getItems().size(); i++) {
SWItem currentItem = currentScreen.getItems().get(i);
currentItem.generateDialog(layout);
}
scrollView.smoothScrollTo(0, 0);
}
private void updateButtons() {
runOnUiThread(() -> {
SWScreen currentScreen = screens.get(currentWizardPage);
if (currentScreen.getValidator() == null || currentScreen.getValidator().isValid() || currentScreen.getSkippable()) {
if (currentWizardPage == nextPage()) {
findViewById(R.id.finish_button).setVisibility(View.VISIBLE);
findViewById(R.id.next_button).setVisibility(View.GONE);
} else {
findViewById(R.id.finish_button).setVisibility(View.GONE);
findViewById(R.id.next_button).setVisibility(View.VISIBLE);
}
} else {
findViewById(R.id.finish_button).setVisibility(View.GONE);
findViewById(R.id.next_button).setVisibility(View.GONE);
}
if (currentWizardPage == 0)
findViewById(R.id.previous_button).setVisibility(View.GONE);
else
findViewById(R.id.previous_button).setVisibility(View.VISIBLE);
currentScreen.processVisibility();
});
}
@Override
public void onBackPressed() {
if (currentWizardPage == 0)
OKDialog.showConfirmation(this, resourceHelper.gs(R.string.exitwizard), this::finish);
else showPreviousPage(null);
}
public void exitPressed(View view) {
sp.putBoolean(R.string.key_setupwizard_processed, true);
OKDialog.showConfirmation(this, resourceHelper.gs(R.string.exitwizard), this::finish);
}
public void showNextPage(View view) {
this.finish();
Intent intent = new Intent(this, SetupWizardActivity.class);
intent.putExtra(INTENT_MESSAGE, nextPage());
startActivity(intent);
}
public void showPreviousPage(View view) {
this.finish();
Intent intent = new Intent(this, SetupWizardActivity.class);
intent.putExtra(INTENT_MESSAGE, previousPage());
startActivity(intent);
}
// Go back to overview
public void finishSetupWizard(View view) {
sp.putBoolean(R.string.key_setupwizard_processed, true);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
private int nextPage() {
int page = currentWizardPage + 1;
while (page < screens.size()) {
if (screens.get(page).getVisibility() == null || screens.get(page).getVisibility().isValid())
return page;
page++;
}
return Math.min(currentWizardPage, screens.size() - 1);
}
private int previousPage() {
int page = currentWizardPage - 1;
while (page >= 0) {
if (screens.get(page).getVisibility() == null || screens.get(page).getVisibility().isValid())
return page;
page--;
}
return Math.max(currentWizardPage, 0);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (permissions.length != 0) {
if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) {
switch (requestCode) {
case AndroidPermission.CASE_STORAGE:
//show dialog after permission is granted
OKDialog.show(this, resourceHelper.gs(R.string.permission), resourceHelper.gs(R.string.alert_dialog_storage_permission_text));
break;
case AndroidPermission.CASE_LOCATION:
case AndroidPermission.CASE_SMS:
case AndroidPermission.CASE_BATTERY:
break;
}
}
}
updateButtons();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AndroidPermission.CASE_BATTERY)
updateButtons();
}
}

View file

@ -0,0 +1,215 @@
package info.nightscout.androidaps.setupwizard
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.app.ActivityCompat
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.MainActivity
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity
import info.nightscout.androidaps.events.EventProfileNeedsUpdate
import info.nightscout.androidaps.events.EventProfileStoreChanged
import info.nightscout.androidaps.events.EventPumpStatusChanged
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus
import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin
import info.nightscout.androidaps.setupwizard.elements.SWItem
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
import info.nightscout.androidaps.utils.AndroidPermission
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.LocaleHelper.update
import info.nightscout.androidaps.utils.OKDialog.show
import info.nightscout.androidaps.utils.OKDialog.showConfirmation
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.activity_setupwizard.*
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
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var sp: SP
@Inject lateinit var fabricPrivacy: FabricPrivacy
private val disposable = CompositeDisposable()
private lateinit var screens: List<SWScreen>
private var currentWizardPage = 0
private val intentMessage = "WIZZARDPAGE"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
update(applicationContext)
setContentView(R.layout.activity_setupwizard)
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()
}
}
public override fun onPause() {
super.onPause()
disposable.clear()
}
override fun onResume() {
super.onResume()
swDefinition.activity = this
disposable.add(rxBus
.toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateButtons() }) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
)
disposable.add(rxBus
.toObservable(EventNSClientStatus::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateButtons() }) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
)
disposable.add(rxBus
.toObservable(EventProfileNeedsUpdate::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateButtons() }) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
)
disposable.add(rxBus
.toObservable(EventProfileStoreChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateButtons() }) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
)
disposable.add(rxBus
.toObservable(EventSWUpdate::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ event: EventSWUpdate ->
if (event.redraw) generateLayout()
updateButtons()
}) { exception: Throwable? -> fabricPrivacy.logException(exception!!) }
)
}
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)
}
sw_scrollview?.smoothScrollTo(0, 0)
}
private fun updateButtons() {
runOnUiThread {
val currentScreen = screens[currentWizardPage]
if (currentScreen.validator == null || currentScreen.validator!!.isValid || currentScreen.skippable) {
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() {
if (currentWizardPage == 0) showConfirmation(this, resourceHelper.gs(R.string.exitwizard), Runnable { finish() }) else showPreviousPage(null)
}
@Suppress("UNUSED_PARAMETER")
fun exitPressed(view: View?) {
sp.putBoolean(R.string.key_setupwizard_processed, true)
showConfirmation(this, resourceHelper.gs(R.string.exitwizard), Runnable { finish() })
}
@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")
private fun nextPage(view: View?): Int {
var page = currentWizardPage + 1
while (page < screens.size) {
if (screens[page].visibility == null || screens[page].visibility!!.isValid) return page
page++
}
return min(currentWizardPage, screens.size - 1)
}
@Suppress("UNUSED_PARAMETER","SameParameterValue")
private fun previousPage(view: View?): Int {
var page = currentWizardPage - 1
while (page >= 0) {
if (screens[page].visibility == null || screens[page].visibility!!.isValid) return page
page--
}
return max(currentWizardPage, 0)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (permissions.isNotEmpty()) {
if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) {
when (requestCode) {
AndroidPermission.CASE_STORAGE -> //show dialog after permission is granted
show(this, resourceHelper.gs(R.string.permission), resourceHelper.gs(R.string.alert_dialog_storage_permission_text))
AndroidPermission.CASE_LOCATION, AndroidPermission.CASE_SMS, AndroidPermission.CASE_BATTERY -> {
}
}
}
}
updateButtons()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == AndroidPermission.CASE_BATTERY) updateButtons()
}
}