NSProfileFragment -> kotlin

This commit is contained in:
Milos Kozak 2019-08-06 22:53:09 +02:00
parent 73e8d21ca0
commit 949cde143a
5 changed files with 128 additions and 174 deletions

View file

@ -1,164 +0,0 @@
package info.nightscout.androidaps.plugins.profile.ns;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import com.squareup.otto.Subscribe;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemSelected;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.plugins.common.SubscriberFragment;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.profile.ns.events.EventNSProfileUpdateGUI;
import info.nightscout.androidaps.plugins.treatments.fragments.ProfileGraph;
import info.nightscout.androidaps.utils.DecimalFormatter;
import info.nightscout.androidaps.utils.OKDialog;
import static butterknife.OnItemSelected.Callback.NOTHING_SELECTED;
public class NSProfileFragment extends SubscriberFragment {
@BindView(R.id.nsprofile_spinner)
Spinner profileSpinner;
@BindView(R.id.profileview_noprofile)
TextView noProfile;
@BindView(R.id.profileview_invalidprofile)
TextView invalidProfile;
@BindView(R.id.profileview_units)
TextView units;
@BindView(R.id.profileview_dia)
TextView dia;
@BindView(R.id.profileview_activeprofile)
TextView activeProfile;
@BindView(R.id.profileview_ic)
TextView ic;
@BindView(R.id.profileview_isf)
TextView isf;
@BindView(R.id.profileview_basal)
TextView basal;
@BindView(R.id.profileview_basaltotal)
TextView basaltotal;
@BindView(R.id.profileview_target)
TextView target;
@BindView(R.id.basal_graph)
ProfileGraph basalGraph;
@BindView(R.id.nsprofile_profileswitch)
Button activateButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.nsprofile_fragment, container, false);
unbinder = ButterKnife.bind(this, view);
updateGUI();
return view;
}
@Subscribe
public void onStatusEvent(final EventNSProfileUpdateGUI ev) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(() -> {
synchronized (NSProfileFragment.this) {
updateGUI();
}
});
}
@Override
protected void updateGUI() {
if (noProfile == null || profileSpinner == null)
return;
ProfileStore profileStore = NSProfilePlugin.getPlugin().getProfile();
if (profileStore != null) {
ArrayList<CharSequence> profileList = profileStore.getProfileList();
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(getContext(),
R.layout.spinner_centered, profileList);
profileSpinner.setAdapter(adapter);
// set selected to actual profile
for (int p = 0; p < profileList.size(); p++) {
if (profileList.get(p).equals(ProfileFunctions.getInstance().getProfileName()))
profileSpinner.setSelection(p);
}
noProfile.setVisibility(View.GONE);
} else {
noProfile.setVisibility(View.VISIBLE);
}
}
@OnItemSelected(R.id.nsprofile_spinner)
public void onItemSelected(Spinner spinner, int position) {
String name = spinner.getItemAtPosition(position).toString();
ProfileStore store = NSProfilePlugin.getPlugin().getProfile();
if (store != null) {
Profile profile = store.getSpecificProfile(name);
if (profile != null) {
units.setText(profile.getUnits());
dia.setText(DecimalFormatter.to2Decimal(profile.getDia()) + " h");
activeProfile.setText(name);
ic.setText(profile.getIcList());
isf.setText(profile.getIsfList());
basal.setText(profile.getBasalList());
basaltotal.setText(String.format(MainApp.gs(R.string.profile_total), DecimalFormatter.to2Decimal(profile.baseBasalSum())));
target.setText(profile.getTargetList());
basalGraph.show(profile);
}
if (profile.isValid("NSProfileFragment")) {
invalidProfile.setVisibility(View.GONE);
activateButton.setVisibility(View.VISIBLE);
} else {
invalidProfile.setVisibility(View.VISIBLE);
activateButton.setVisibility(View.GONE);
}
} else {
activateButton.setVisibility(View.GONE);
}
}
@OnItemSelected(value = R.id.nsprofile_spinner, callback = NOTHING_SELECTED)
public void onNothingSelected() {
invalidProfile.setVisibility(View.VISIBLE);
noProfile.setVisibility(View.VISIBLE);
units.setText("");
dia.setText("");
activeProfile.setText("");
ic.setText("");
isf.setText("");
basal.setText("");
basaltotal.setText("");
target.setText("");
activateButton.setVisibility(View.GONE);
}
@OnClick(R.id.nsprofile_profileswitch)
public void onClickProfileSwitch() {
String name = profileSpinner.getSelectedItem() != null ? profileSpinner.getSelectedItem().toString() : "";
ProfileStore store = NSProfilePlugin.getPlugin().getProfile();
if (store != null) {
Profile profile = store.getSpecificProfile(name);
if (profile != null) {
OKDialog.showConfirmation(getActivity(), MainApp.gs(R.string.activate_profile) + ": " + name + " ?", () ->
ProfileFunctions.doProfileSwitch(store, name, 0, 100, 0)
);
}
}
}
}

View file

@ -0,0 +1,121 @@
package info.nightscout.androidaps.plugins.profile.ns
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.fragment.app.Fragment
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions
import info.nightscout.androidaps.plugins.profile.ns.events.EventNSProfileUpdateGUI
import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.OKDialog
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.close.*
import kotlinx.android.synthetic.main.nsprofile_fragment.*
import kotlinx.android.synthetic.main.profileviewer_fragment.*
class NSProfileFragment : Fragment() {
private var disposable: CompositeDisposable = CompositeDisposable()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.nsprofile_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
close.visibility = View.GONE // not needed for fragment
updateGUI()
nsprofile_profileswitch.setOnClickListener {
val name = nsprofile_spinner.selectedItem?.toString() ?: ""
NSProfilePlugin.getPlugin().profile?.let { store ->
store.getSpecificProfile(name)?.let {
OKDialog.showConfirmation(activity,
MainApp.gs(R.string.activate_profile) + ": " + name + " ?"
) {
ProfileFunctions.doProfileSwitch(store, name, 0, 100, 0)
}
}
}
}
nsprofile_spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
profileview_invalidprofile.visibility = View.VISIBLE
profileview_noprofile.visibility = View.VISIBLE
profileview_units.text = ""
profileview_dia.text = ""
profileview_activeprofile.text = ""
profileview_ic.text = ""
profileview_isf.text = ""
profileview_basal.text = ""
profileview_basaltotal.text = ""
profileview_target.text = ""
nsprofile_profileswitch.visibility = View.GONE
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val name = nsprofile_spinner.getItemAtPosition(position).toString()
nsprofile_profileswitch.visibility = View.GONE
NSProfilePlugin.getPlugin().profile?.let { store ->
store.getSpecificProfile(name)?.let { profile ->
profileview_units.text = profile.units
profileview_dia.text = MainApp.gs(R.string.format_hours, profile.dia)
profileview_activeprofile.text = name
profileview_ic.text = profile.icList
profileview_isf.text = profile.isfList
profileview_basal.text = profile.basalList
profileview_basaltotal.text = String.format(MainApp.gs(R.string.profile_total), DecimalFormatter.to2Decimal(profile.baseBasalSum()))
profileview_target.text = profile.targetList
basal_graph.show(profile)
if (profile.isValid("NSProfileFragment")) {
profileview_invalidprofile.visibility = View.GONE
nsprofile_profileswitch.visibility = View.VISIBLE
} else {
profileview_invalidprofile.visibility = View.VISIBLE
nsprofile_profileswitch.visibility = View.GONE
}
}
}
}
}
disposable.add(RxBus
.toObservable(EventNSProfileUpdateGUI::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
updateGUI()
}, {
FabricPrivacy.logException(it)
})
)
}
fun updateGUI() {
profileview_noprofile.visibility = View.VISIBLE
NSProfilePlugin.getPlugin().profile?.let { profileStore ->
val profileList = profileStore.profileList
val adapter = ArrayAdapter(context!!, R.layout.spinner_centered, profileList)
nsprofile_spinner.adapter = adapter
// set selected to actual profile
for (p in profileList.indices) {
if (profileList[p] == ProfileFunctions.getInstance().profileName)
nsprofile_spinner.setSelection(p)
}
profileview_noprofile.visibility = View.GONE
}
}
}

View file

@ -14,6 +14,7 @@ import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.services.Intents;
import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.events.EventProfileStoreChanged;
@ -76,7 +77,7 @@ public class NSProfilePlugin extends PluginBase implements ProfileInterface {
storeNSProfile();
if (isEnabled(PluginType.PROFILE)) {
MainApp.bus().post(new EventProfileStoreChanged());
MainApp.bus().post(new EventNSProfileUpdateGUI());
RxBus.INSTANCE.send(new EventNSProfileUpdateGUI());
}
if (L.isEnabled(L.PROFILE))
log.debug("Received profileStore: " + activeProfile + " " + profile);

View file

@ -1,9 +0,0 @@
package info.nightscout.androidaps.plugins.profile.ns.events;
import info.nightscout.androidaps.events.EventUpdateGui;
/**
* Created by mike on 05.08.2016.
*/
public class EventNSProfileUpdateGUI extends EventUpdateGui {
}

View file

@ -0,0 +1,5 @@
package info.nightscout.androidaps.plugins.profile.ns.events
import info.nightscout.androidaps.events.EventUpdateGui
class EventNSProfileUpdateGUI : EventUpdateGui()