TreatmentsCareportalFragment -> kt

This commit is contained in:
Milos Kozak 2019-12-30 09:50:26 +01:00
parent 9ceeaa3136
commit 2f59cd4f09
3 changed files with 149 additions and 183 deletions

View file

@ -30,6 +30,7 @@ import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpFragment
import info.nightscout.androidaps.plugins.source.BGSourceFragment import info.nightscout.androidaps.plugins.source.BGSourceFragment
import info.nightscout.androidaps.plugins.treatments.TreatmentsFragment import info.nightscout.androidaps.plugins.treatments.TreatmentsFragment
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsBolusFragment import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsBolusFragment
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsCareportalFragment
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsProfileSwitchFragment import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsProfileSwitchFragment
@Module @Module
@ -60,8 +61,9 @@ abstract class FragmentsModule {
@ContributesAndroidInjector abstract fun contributesSmsCommunicatorFragment(): SmsCommunicatorFragment @ContributesAndroidInjector abstract fun contributesSmsCommunicatorFragment(): SmsCommunicatorFragment
@ContributesAndroidInjector abstract fun contributesTidepoolFragment(): TidepoolFragment @ContributesAndroidInjector abstract fun contributesTidepoolFragment(): TidepoolFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsFragment(): TreatmentsFragment @ContributesAndroidInjector abstract fun contributesTreatmentsFragment(): TreatmentsFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsProfileSwitchFragment(): TreatmentsProfileSwitchFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsBolusFragment(): TreatmentsBolusFragment @ContributesAndroidInjector abstract fun contributesTreatmentsBolusFragment(): TreatmentsBolusFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsCareportalFragment(): TreatmentsCareportalFragment
@ContributesAndroidInjector abstract fun contributesTreatmentsProfileSwitchFragment(): TreatmentsProfileSwitchFragment
@ContributesAndroidInjector abstract fun contributesVirtualPumpFragment(): VirtualPumpFragment @ContributesAndroidInjector abstract fun contributesVirtualPumpFragment(): VirtualPumpFragment
@ContributesAndroidInjector abstract fun contributesBolusProgressDialog(): BolusProgressDialog @ContributesAndroidInjector abstract fun contributesBolusProgressDialog(): BolusProgressDialog

View file

@ -1,182 +0,0 @@
package info.nightscout.androidaps.plugins.treatments.fragments;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.events.EventCareportalEventChange;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue;
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.OKDialog;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.Translator;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
/**
* Created by mike on 13/01/17.
*/
public class TreatmentsCareportalFragment extends Fragment {
private CompositeDisposable disposable = new CompositeDisposable();
private RecyclerView recyclerView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.CareportalEventsViewHolder> {
List<CareportalEvent> careportalEventList;
RecyclerViewAdapter(List<CareportalEvent> careportalEventList) {
this.careportalEventList = careportalEventList;
}
@NonNull
@Override
public CareportalEventsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_careportal_item, viewGroup, false);
return new CareportalEventsViewHolder(v);
}
@Override
public void onBindViewHolder(CareportalEventsViewHolder holder, int position) {
CareportalEvent careportalEvent = careportalEventList.get(position);
holder.ns.setVisibility(NSUpload.isIdValid(careportalEvent._id) ? View.VISIBLE : View.GONE);
holder.date.setText(DateUtil.dateAndTimeString(careportalEvent.date));
holder.note.setText(careportalEvent.getNotes());
holder.type.setText(Translator.translate(careportalEvent.eventType));
holder.remove.setTag(careportalEvent);
}
@Override
public int getItemCount() {
return careportalEventList.size();
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
class CareportalEventsViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView date;
TextView type;
TextView note;
TextView remove;
TextView ns;
CareportalEventsViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.careportal_cardview);
date = itemView.findViewById(R.id.careportal_date);
type = itemView.findViewById(R.id.careportal_type);
note = itemView.findViewById(R.id.careportal_note);
ns = itemView.findViewById(R.id.ns_sign);
remove = itemView.findViewById(R.id.careportal_remove);
remove.setOnClickListener(v -> {
final CareportalEvent careportalEvent = (CareportalEvent) v.getTag();
OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord),
"\n" + MainApp.gs(R.string.careportal_newnstreatment_eventtype) + ": " + Translator.translate(careportalEvent.eventType) +
"\n" + MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + careportalEvent.getNotes() +
"\n" + MainApp.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(careportalEvent.date),
(dialog, id) -> {
final String _id = careportalEvent._id;
if (NSUpload.isIdValid(_id)) {
NSUpload.removeCareportalEntryFromNS(_id);
} else {
UploadQueue.removeID("dbAdd", _id);
}
MainApp.getDbHelper().delete(careportalEvent);
}, null);
});
remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.treatments_careportal_fragment, container, false);
recyclerView = view.findViewById(R.id.careportal_recyclerview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(llm);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false));
recyclerView.setAdapter(adapter);
Button refreshFromNS = view.findViewById(R.id.careportal_refreshfromnightscout);
refreshFromNS.setOnClickListener(v ->
OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.careportal), MainApp.gs(R.string.refresheventsfromnightscout) + " ?", () -> {
MainApp.getDbHelper().resetCareportalEvents();
RxBus.Companion.getINSTANCE().send(new EventNSClientRestart());
}));
view.findViewById(R.id.careportal_removeandroidapsstartedevents).setOnClickListener(v ->
OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.careportal), MainApp.gs(R.string.careportal_removestartedevents), this::removeAndroidAPSStatedEvents));
boolean nsUploadOnly = SP.getBoolean(R.string.key_ns_upload_only, false);
if (nsUploadOnly)
refreshFromNS.setVisibility(View.GONE);
return view;
}
@Override
public synchronized void onResume() {
super.onResume();
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventCareportalEventChange.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException)
);
updateGui();
}
@Override
public synchronized void onPause() {
super.onPause();
disposable.clear();
}
private void updateGui() {
recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false);
}
private void removeAndroidAPSStatedEvents() {
List<CareportalEvent> events = MainApp.getDbHelper().getCareportalEvents(false);
for (int i = 0; i < events.size(); i++) {
CareportalEvent careportalEvent = events.get(i);
if (careportalEvent.json.contains(MainApp.gs(R.string.androidaps_start))) {
final String _id = careportalEvent._id;
if (NSUpload.isIdValid(_id)) {
NSUpload.removeCareportalEntryFromNS(_id);
} else {
UploadQueue.removeID("dbAdd", _id);
}
MainApp.getDbHelper().delete(careportalEvent);
}
}
}
}

View file

@ -0,0 +1,146 @@
package info.nightscout.androidaps.plugins.treatments.fragments
import android.graphics.Paint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.db.CareportalEvent
import info.nightscout.androidaps.events.EventCareportalEventChange
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsCareportalFragment.RecyclerViewAdapter.CareportalEventsViewHolder
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.OKDialog
import info.nightscout.androidaps.utils.Translator
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.treatments_careportal_fragment.*
import javax.inject.Inject
class TreatmentsCareportalFragment : DaggerFragment() {
private val disposable = CompositeDisposable()
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var sp: SP
@Inject lateinit var resourceHelper: ResourceHelper
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.treatments_careportal_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
careportal_recyclerview.setHasFixedSize(true)
careportal_recyclerview.layoutManager = LinearLayoutManager(view.context)
careportal_recyclerview.adapter = RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false))
careportal_refreshfromnightscout.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal), resourceHelper.gs(R.string.refresheventsfromnightscout) + " ?", Runnable {
MainApp.getDbHelper().resetCareportalEvents()
rxBus.send(EventNSClientRestart())
})
}
}
careportal_removeandroidapsstartedevents.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal), resourceHelper.gs(R.string.careportal_removestartedevents), Runnable {
val events = MainApp.getDbHelper().getCareportalEvents(false)
for (i in events.indices) {
val careportalEvent = events[i]
if (careportalEvent.json.contains(resourceHelper.gs(R.string.androidaps_start))) {
if (NSUpload.isIdValid(careportalEvent._id))
NSUpload.removeCareportalEntryFromNS(careportalEvent._id)
else
UploadQueue.removeID("dbAdd", careportalEvent._id)
MainApp.getDbHelper().delete(careportalEvent)
}
}
}, null)
}
}
val nsUploadOnly = sp.getBoolean(R.string.key_ns_upload_only, false)
if (nsUploadOnly) careportal_refreshfromnightscout.visibility = View.GONE
}
@Synchronized override fun onResume() {
super.onResume()
disposable.add(rxBus
.toObservable(EventCareportalEventChange::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGui() }) { FabricPrivacy.logException(it) }
)
updateGui()
}
@Synchronized override fun onPause() {
super.onPause()
disposable.clear()
}
private fun updateGui() =
careportal_recyclerview?.swapAdapter(RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false)
inner class RecyclerViewAdapter internal constructor(private var careportalEventList: List<CareportalEvent>) : RecyclerView.Adapter<CareportalEventsViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): CareportalEventsViewHolder {
val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_careportal_item, viewGroup, false)
return CareportalEventsViewHolder(v)
}
override fun onBindViewHolder(holder: CareportalEventsViewHolder, position: Int) {
val careportalEvent = careportalEventList[position]
holder.ns.visibility = if (NSUpload.isIdValid(careportalEvent._id)) View.VISIBLE else View.GONE
holder.date.text = DateUtil.dateAndTimeString(careportalEvent.date)
holder.note.text = careportalEvent.notes
holder.type.text = Translator.translate(careportalEvent.eventType)
holder.remove.tag = careportalEvent
}
override fun getItemCount(): Int {
return careportalEventList.size
}
inner class CareportalEventsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var cv: CardView = itemView.findViewById(R.id.careportal_cardview)
var date: TextView = itemView.findViewById(R.id.careportal_date)
var type: TextView = itemView.findViewById(R.id.careportal_type)
var note: TextView = itemView.findViewById(R.id.careportal_note)
var remove: TextView = itemView.findViewById(R.id.careportal_remove)
var ns: TextView = itemView.findViewById(R.id.ns_sign)
init {
remove.setOnClickListener { v: View ->
val careportalEvent = v.tag as CareportalEvent
activity?.let { activity ->
val text = resourceHelper.gs(R.string.careportal_newnstreatment_eventtype) + ": " + Translator.translate(careportalEvent.eventType) + "\n" +
resourceHelper.gs(R.string.careportal_newnstreatment_notes_label) + ": " + careportalEvent.notes + "\n" +
resourceHelper.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(careportalEvent.date)
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.removerecord), text, Runnable {
if (NSUpload.isIdValid(careportalEvent._id))
NSUpload.removeCareportalEntryFromNS(careportalEvent._id)
else
UploadQueue.removeID("dbAdd", careportalEvent._id)
MainApp.getDbHelper().delete(careportalEvent)
}, null)
}
}
remove.paintFlags = remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
}
}
}