BolusActivity -> kt

This commit is contained in:
Milos Kozak 2022-04-23 00:05:10 +02:00
parent 5ca9390f07
commit ffdcf9900b
5 changed files with 160 additions and 198 deletions

View file

@ -1,86 +0,0 @@
package info.nightscout.androidaps.interaction.actions;
import android.os.Bundle;
import android.support.wearable.view.GridPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.text.DecimalFormat;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventWearToMobile;
import info.nightscout.androidaps.interaction.utils.PlusMinusEditText;
import info.nightscout.shared.SafeParse;
import info.nightscout.shared.weardata.EventData;
public class BolusActivity extends ViewSelectorActivity {
PlusMinusEditText editInsulin;
double maxBolus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setAdapter(new MyGridViewPagerAdapter());
maxBolus = sp.getDouble(getString(R.string.key_treatmentssafety_maxbolus), 3.0);
}
@Override
protected void onPause() {
super.onPause();
finish();
}
@SuppressWarnings("deprecation")
private class MyGridViewPagerAdapter extends GridPagerAdapter {
@Override
public int getColumnCount(int arg0) {
return 2;
}
@Override
public int getRowCount() {
return 1;
}
@Override
public Object instantiateItem(ViewGroup container, int row, int col) {
final View view;
if (col == 0) {
view = getInflatedPlusMinusView(container);
double def = 0;
if (editInsulin != null) {
def = SafeParse.stringToDouble(editInsulin.editText.getText().toString());
}
editInsulin = new PlusMinusEditText(view, R.id.amountfield, R.id.plusbutton, R.id.minusbutton, def, 0d, maxBolus, 0.1d, new DecimalFormat("#0.0"), false);
setLabelToPlusMinusView(view, getString(R.string.action_insulin));
container.addView(view);
view.requestFocus();
} else {
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_send_item, container, false);
final ImageView confirmButton = view.findViewById(R.id.confirmbutton);
confirmButton.setOnClickListener((View v) -> {
rxBus.send(new EventWearToMobile(new EventData.ActionBolusPreCheck(SafeParse.stringToDouble(editInsulin.editText.getText().toString()), 0)));
showToast(BolusActivity.this, R.string.action_bolus_confirmation);
finishAffinity();
});
container.addView(view);
}
return view;
}
@Override
public void destroyItem(ViewGroup container, int row, int col, Object view) {
container.removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}

View file

@ -0,0 +1,65 @@
@file:Suppress("DEPRECATION")
package info.nightscout.androidaps.interaction.actions
import android.os.Bundle
import android.support.wearable.view.GridPagerAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import info.nightscout.androidaps.R
import info.nightscout.androidaps.events.EventWearToMobile
import info.nightscout.androidaps.interaction.utils.PlusMinusEditText
import info.nightscout.shared.SafeParse
import info.nightscout.shared.weardata.EventData.ActionBolusPreCheck
import java.text.DecimalFormat
class BolusActivity : ViewSelectorActivity() {
var editInsulin: PlusMinusEditText? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setAdapter(MyGridViewPagerAdapter())
}
override fun onPause() {
super.onPause()
finish()
}
private inner class MyGridViewPagerAdapter : GridPagerAdapter() {
override fun getColumnCount(arg0: Int): Int = 2
override fun getRowCount(): Int = 1
override fun instantiateItem(container: ViewGroup, row: Int, col: Int): Any {
val view: View
if (col == 0) {
view = getInflatedPlusMinusView(container)
val initValue = if (editInsulin != null) SafeParse.stringToDouble(editInsulin?.editText?.text.toString()) else 0.0
val maxBolus = sp.getDouble(getString(R.string.key_treatmentssafety_maxbolus), 3.0)
editInsulin = PlusMinusEditText(view, R.id.amountfield, R.id.plusbutton, R.id.minusbutton, initValue, 0.0, maxBolus, 0.1, DecimalFormat("#0.0"), false)
setLabelToPlusMinusView(view, getString(R.string.action_insulin))
container.addView(view)
view.requestFocus()
} else {
view = LayoutInflater.from(applicationContext).inflate(R.layout.action_send_item, container, false)
val confirmButton = view.findViewById<ImageView>(R.id.confirmbutton)
confirmButton.setOnClickListener {
rxBus.send(EventWearToMobile(ActionBolusPreCheck(SafeParse.stringToDouble(editInsulin?.editText?.text.toString()), 0)))
showToast(this@BolusActivity, R.string.action_bolus_confirmation)
finishAffinity()
}
container.addView(view)
}
return view
}
override fun destroyItem(container: ViewGroup, row: Int, col: Int, view: Any) {
container.removeView(view as View)
}
override fun isViewFromObject(view: View, `object`: Any): Boolean = view === `object`
}
}

View file

@ -1,112 +0,0 @@
package info.nightscout.androidaps.interaction.actions;
import android.content.Context;
import android.os.Bundle;
import android.support.wearable.view.DotsPageIndicator;
import android.support.wearable.view.GridPagerAdapter;
import android.support.wearable.view.GridViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.wear.widget.CurvedTextView;
import javax.inject.Inject;
import dagger.android.DaggerActivity;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.shared.sharedPreferences.SP;
/**
* Created by adrian on 13/02/17.
*/
public class ViewSelectorActivity extends DaggerActivity {
@Inject SP sp;
@Inject RxBus rxBus;
private GridViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
setTitleBasedOnScreenShape();
pager = findViewById(R.id.pager);
DotsPageIndicator dotsPageIndicator = findViewById(R.id.page_indicator);
dotsPageIndicator.setPager(pager);
pager.setOnPageChangeListener(new GridViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int row, int column, float rowOffset, float columnOffset, int rowOffsetPixels, int columnOffsetPixels) {
dotsPageIndicator.onPageScrolled(row, column, rowOffset, columnOffset, rowOffsetPixels,
columnOffsetPixels);
}
@Override
public void onPageSelected(int row, int column) {
dotsPageIndicator.onPageSelected(row, column);
View view = pager.getChildAt(column);
view.requestFocus();
}
@Override
public void onPageScrollStateChanged(int state) {
dotsPageIndicator.onPageScrollStateChanged(state);
}
});
}
public void setAdapter(GridPagerAdapter adapter) {
pager.setAdapter(adapter);
}
private void setTitleBasedOnScreenShape() {
// intents can inject dynamic titles, otherwise we'll use the default
String title = String.valueOf(getTitle());
if (getIntent().getExtras() != null) {
title = getIntent().getExtras().getString("title", title);
}
CurvedTextView titleViewCurved = findViewById(R.id.title_curved);
TextView titleView = findViewById(R.id.title);
if (this.getResources().getConfiguration().isScreenRound()) {
titleViewCurved.setText(title);
titleViewCurved.setVisibility(View.VISIBLE);
titleView.setVisibility((View.GONE));
} else {
titleView.setText(title);
titleView.setVisibility(View.VISIBLE);
titleViewCurved.setVisibility((View.GONE));
}
}
View getInflatedPlusMinusView(ViewGroup container) {
int design = sp.getInt("input_design", 1);
if (design == 2) {
return LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_editplusminus_item_quickrighty, container, false);
} else if (design == 3) {
return LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_editplusminus_item_quicklefty, container, false);
} else if (design == 4) {
return LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_editplusminus_item_viktoria, container, false);
}
//default
return LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_editplusminus_item, container, false);
}
void setLabelToPlusMinusView(View view, String labelText) {
final TextView textView = view.findViewById(R.id.label);
textView.setText(labelText);
}
void showToast(Context context, int text) {
Toast.makeText(context, getString(text), Toast.LENGTH_LONG).show();
}
}

View file

@ -0,0 +1,92 @@
@file:Suppress("DEPRECATION")
package info.nightscout.androidaps.interaction.actions
import android.content.Context
import android.os.Bundle
import android.support.wearable.view.DotsPageIndicator
import android.support.wearable.view.GridPagerAdapter
import android.support.wearable.view.GridViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.wear.widget.CurvedTextView
import dagger.android.DaggerActivity
import info.nightscout.androidaps.R
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.shared.sharedPreferences.SP
import javax.inject.Inject
/**
* Created by adrian on 13/02/17.
*/
open class ViewSelectorActivity : DaggerActivity() {
@Inject lateinit var sp: SP
@Inject lateinit var rxBus: RxBus
private var pager: GridViewPager? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.grid_layout)
setTitleBasedOnScreenShape()
pager = findViewById(R.id.pager)
val dotsPageIndicator: DotsPageIndicator = findViewById(R.id.page_indicator)
dotsPageIndicator.setPager(pager)
pager?.setOnPageChangeListener(object : GridViewPager.OnPageChangeListener {
override fun onPageScrolled(row: Int, column: Int, rowOffset: Float, columnOffset: Float, rowOffsetPixels: Int, columnOffsetPixels: Int) {
dotsPageIndicator.onPageScrolled(row, column, rowOffset, columnOffset, rowOffsetPixels, columnOffsetPixels)
}
override fun onPageSelected(row: Int, column: Int) {
dotsPageIndicator.onPageSelected(row, column)
pager?.getChildAt(column)?.requestFocus()
}
override fun onPageScrollStateChanged(state: Int) {
dotsPageIndicator.onPageScrollStateChanged(state)
}
})
}
fun setAdapter(adapter: GridPagerAdapter?) {
pager?.adapter = adapter
}
private fun setTitleBasedOnScreenShape() {
// intents can inject dynamic titles, otherwise we'll use the default
var title: String? = this.title.toString()
title = intent?.extras?.getString("title", title)
val titleViewCurved: CurvedTextView = findViewById(R.id.title_curved)
val titleView = findViewById<TextView>(R.id.title)
if (this.resources.configuration.isScreenRound) {
titleViewCurved.text = title
titleViewCurved.visibility = View.VISIBLE
titleView.visibility = View.GONE
} else {
titleView.text = title
titleView.visibility = View.VISIBLE
titleViewCurved.visibility = View.GONE
}
}
fun getInflatedPlusMinusView(container: ViewGroup?): View =
when (sp.getInt("input_design", 1)) {
2 -> LayoutInflater.from(applicationContext).inflate(R.layout.action_editplusminus_item_quickrighty, container, false)
3 -> LayoutInflater.from(applicationContext).inflate(R.layout.action_editplusminus_item_quicklefty, container, false)
4 -> LayoutInflater.from(applicationContext).inflate(R.layout.action_editplusminus_item_viktoria, container, false)
else -> LayoutInflater.from(applicationContext).inflate(R.layout.action_editplusminus_item, container, false)
}
fun setLabelToPlusMinusView(view: View, labelText: String?) {
val textView = view.findViewById<TextView>(R.id.label)
textView.text = labelText
}
fun showToast(context: Context?, text: Int) {
Toast.makeText(context, getString(text), Toast.LENGTH_LONG).show()
}
}

View file

@ -5,6 +5,7 @@ import android.os.Bundle
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.events.EventWearToMobile import info.nightscout.androidaps.events.EventWearToMobile
import info.nightscout.androidaps.interaction.AAPSPreferences import info.nightscout.androidaps.interaction.AAPSPreferences
import info.nightscout.androidaps.interaction.actions.BolusActivity
import info.nightscout.androidaps.interaction.actions.ECarbActivity import info.nightscout.androidaps.interaction.actions.ECarbActivity
import info.nightscout.androidaps.interaction.actions.TempTargetActivity import info.nightscout.androidaps.interaction.actions.TempTargetActivity
import info.nightscout.androidaps.interaction.actions.TreatmentActivity import info.nightscout.androidaps.interaction.actions.TreatmentActivity
@ -31,6 +32,7 @@ class MainMenuActivity : MenuListActivity() {
add(MenuItem(R.drawable.ic_calculator, getString(R.string.menu_wizard))) add(MenuItem(R.drawable.ic_calculator, getString(R.string.menu_wizard)))
add(MenuItem(R.drawable.ic_e_carbs, getString(R.string.menu_ecarb))) add(MenuItem(R.drawable.ic_e_carbs, getString(R.string.menu_ecarb)))
add(MenuItem(R.drawable.ic_treatment, getString(R.string.menu_treatment))) add(MenuItem(R.drawable.ic_treatment, getString(R.string.menu_treatment)))
add(MenuItem(R.drawable.ic_bolus, getString(R.string.menu_bolus)))
add(MenuItem(R.drawable.ic_temptarget, getString(R.string.menu_tempt))) add(MenuItem(R.drawable.ic_temptarget, getString(R.string.menu_tempt)))
add(MenuItem(R.drawable.ic_status, getString(R.string.status_cpp))) add(MenuItem(R.drawable.ic_status, getString(R.string.status_cpp)))
add(MenuItem(R.drawable.ic_settings, getString(R.string.menu_settings))) add(MenuItem(R.drawable.ic_settings, getString(R.string.menu_settings)))
@ -47,6 +49,7 @@ class MainMenuActivity : MenuListActivity() {
getString(R.string.status_cpp) -> rxBus.send(EventWearToMobile(EventData.ActionProfileSwitchSendInitialData(System.currentTimeMillis()))) getString(R.string.status_cpp) -> rxBus.send(EventWearToMobile(EventData.ActionProfileSwitchSendInitialData(System.currentTimeMillis())))
getString(R.string.menu_tempt) -> startActivity(Intent(this, TempTargetActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }) getString(R.string.menu_tempt) -> startActivity(Intent(this, TempTargetActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) })
getString(R.string.menu_treatment) -> startActivity(Intent(this, TreatmentActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }) getString(R.string.menu_treatment) -> startActivity(Intent(this, TreatmentActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) })
getString(R.string.menu_bolus) -> startActivity(Intent(this, BolusActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) })
getString(R.string.menu_wizard) -> startActivity(Intent(this, WizardActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }) getString(R.string.menu_wizard) -> startActivity(Intent(this, WizardActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) })
getString(R.string.menu_status) -> startActivity(Intent(this, StatusMenuActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }) getString(R.string.menu_status) -> startActivity(Intent(this, StatusMenuActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) })
getString(R.string.menu_prime_fill) -> startActivity(Intent(this, FillMenuActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }) getString(R.string.menu_prime_fill) -> startActivity(Intent(this, FillMenuActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) })