QuickWizardListActivity -> kotlin

This commit is contained in:
Milos Kozak 2019-10-01 15:48:48 +02:00
parent c1109a5b41
commit ee26795d97
5 changed files with 109 additions and 182 deletions

View file

@ -1,169 +0,0 @@
package info.nightscout.androidaps.plugins.general.overview.activities;
import android.app.Activity;
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.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.otto.Subscribe;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
import info.nightscout.androidaps.data.QuickWizard;
import info.nightscout.androidaps.plugins.general.overview.dialogs.EditQuickWizardDialog;
import info.nightscout.androidaps.plugins.general.overview.events.EventQuickWizardChange;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.DecimalFormatter;
public class QuickWizardListActivity extends NoSplashAppCompatActivity implements View.OnClickListener {
RecyclerView recyclerView;
LinearLayoutManager llm;
Button adButton;
public static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.QuickWizardEntryViewHolder> {
QuickWizard qvData;
FragmentManager fragmentManager;
RecyclerViewAdapter(QuickWizard data, FragmentManager fragmentManager) {
this.qvData = data;
this.fragmentManager = fragmentManager;
}
@NonNull
@Override
public QuickWizardEntryViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.overview_quickwizardlist_item, viewGroup, false);
QuickWizardEntryViewHolder quickWizardEntryViewHolder = new QuickWizardEntryViewHolder(v, fragmentManager, qvData);
return quickWizardEntryViewHolder;
}
@Override
public void onBindViewHolder(QuickWizardEntryViewHolder holder, int position) {
holder.from.setText(DateUtil.timeString(qvData.get(position).validFromDate()));
holder.to.setText(DateUtil.timeString(qvData.get(position).validToDate()));
holder.buttonText.setText(qvData.get(position).buttonText());
holder.carbs.setText(DecimalFormatter.to0Decimal(qvData.get(position).carbs()) + " g");
}
@Override
public int getItemCount() {
return qvData.size();
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class QuickWizardEntryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cv;
TextView buttonText;
TextView carbs;
TextView from;
TextView to;
Button editButton;
Button removeButton;
FragmentManager fragmentManager;
QuickWizard qvData;
QuickWizardEntryViewHolder(View itemView, FragmentManager fragmentManager, QuickWizard qvData) {
super(itemView);
cv = itemView.findViewById(R.id.overview_quickwizard_cardview);
buttonText = itemView.findViewById(R.id.overview_quickwizard_item_buttonText);
carbs = itemView.findViewById(R.id.overview_quickwizard_item_carbs);
from = itemView.findViewById(R.id.overview_quickwizard_item_from);
to = itemView.findViewById(R.id.overview_quickwizard_item_to);
editButton = itemView.findViewById(R.id.overview_quickwizard_item_edit_button);
removeButton = itemView.findViewById(R.id.overview_quickwizard_item_remove_button);
editButton.setOnClickListener(this);
removeButton.setOnClickListener(this);
this.fragmentManager = fragmentManager;
this.qvData = qvData;
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
switch (v.getId()) {
case R.id.overview_quickwizard_item_edit_button:
FragmentManager manager = fragmentManager;
EditQuickWizardDialog editQuickWizardDialog = new EditQuickWizardDialog();
editQuickWizardDialog.setData(qvData.get(position));
editQuickWizardDialog.show(manager, "EditQuickWizardDialog");
break;
case R.id.overview_quickwizard_item_remove_button:
qvData.remove(position);
MainApp.bus().post(new EventQuickWizardChange());
break;
}
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.overview_quickwizardlist_activity);
recyclerView = findViewById(R.id.overview_quickwizardactivity_recyclerview);
recyclerView.setHasFixedSize(true);
llm = new LinearLayoutManager(this);
recyclerView.setLayoutManager(llm);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(QuickWizard.INSTANCE, getSupportFragmentManager());
recyclerView.setAdapter(adapter);
adButton = findViewById(R.id.overview_quickwizardactivity_add_button);
adButton.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
MainApp.bus().register(this);
}
@Override
protected void onPause() {
super.onPause();
MainApp.bus().unregister(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.overview_quickwizardactivity_add_button) {
FragmentManager manager = getSupportFragmentManager();
EditQuickWizardDialog editQuickWizardDialog = new EditQuickWizardDialog();
editQuickWizardDialog.show(manager, "EditQuickWizardDialog");
}
}
@Subscribe
public void onStatusEvent(final EventQuickWizardChange ev) {
updateGUI();
}
public void updateGUI() {
Activity activity = this;
if (activity != null && recyclerView != null) {
activity.runOnUiThread(() -> {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(QuickWizard.INSTANCE, getSupportFragmentManager());
recyclerView.swapAdapter(adapter, false);
});
}
}
}

View file

@ -0,0 +1,100 @@
package info.nightscout.androidaps.plugins.general.overview.activities
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.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity
import info.nightscout.androidaps.data.QuickWizard
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.general.overview.dialogs.EditQuickWizardDialog
import info.nightscout.androidaps.plugins.general.overview.events.EventQuickWizardChange
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.plusAssign
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.overview_quickwizardlist_activity.*
class QuickWizardListActivity : NoSplashAppCompatActivity() {
private var disposable: CompositeDisposable = CompositeDisposable()
private inner class RecyclerViewAdapter internal constructor(internal var fragmentManager: FragmentManager) : RecyclerView.Adapter<RecyclerViewAdapter.QuickWizardEntryViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuickWizardEntryViewHolder {
return QuickWizardEntryViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.overview_quickwizardlist_item, parent, false), fragmentManager)
}
override fun onBindViewHolder(holder: QuickWizardEntryViewHolder, position: Int) {
holder.from.text = DateUtil.timeString(QuickWizard[position].validFromDate())
holder.to.text = DateUtil.timeString(QuickWizard[position].validToDate())
holder.buttonText.text = QuickWizard[position].buttonText()
holder.carbs.text = DecimalFormatter.to0Decimal(QuickWizard[position].carbs().toDouble()) + " g"
}
override fun getItemCount(): Int = QuickWizard.size()
private inner class QuickWizardEntryViewHolder internal constructor(itemView: View, internal var fragmentManager: FragmentManager) : RecyclerView.ViewHolder(itemView) {
val buttonText: TextView = itemView.findViewById(R.id.overview_quickwizard_item_buttonText)
val carbs: TextView = itemView.findViewById(R.id.overview_quickwizard_item_carbs)
val from: TextView = itemView.findViewById(R.id.overview_quickwizard_item_from)
val to: TextView = itemView.findViewById(R.id.overview_quickwizard_item_to)
private val editButton: Button = itemView.findViewById(R.id.overview_quickwizard_item_edit_button)
private val removeButton: Button = itemView.findViewById(R.id.overview_quickwizard_item_remove_button)
init {
editButton.setOnClickListener {
val manager = fragmentManager
val editQuickWizardDialog = EditQuickWizardDialog()
editQuickWizardDialog.setData(QuickWizard[adapterPosition])
editQuickWizardDialog.show(manager, "EditQuickWizardDialog")
}
removeButton.setOnClickListener {
QuickWizard.remove(adapterPosition)
RxBus.send(EventQuickWizardChange())
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.overview_quickwizardlist_activity)
overview_quickwizardactivity_recyclerview?.setHasFixedSize(true)
overview_quickwizardactivity_recyclerview?.layoutManager = LinearLayoutManager(this)
overview_quickwizardactivity_recyclerview?.adapter = RecyclerViewAdapter(supportFragmentManager)
overview_quickwizardactivity_add_button.setOnClickListener {
val manager = supportFragmentManager
val editQuickWizardDialog = EditQuickWizardDialog()
editQuickWizardDialog.show(manager, "EditQuickWizardDialog")
}
}
override fun onResume() {
super.onResume()
disposable += RxBus
.toObservable(EventQuickWizardChange::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
val adapter = RecyclerViewAdapter(supportFragmentManager)
overview_quickwizardactivity_recyclerview?.swapAdapter(adapter, false)
}, {
FabricPrivacy.logException(it)
})
}
override fun onPause() {
disposable.clear()
super.onPause()
}
}

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.general.overview.dialogs;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -13,6 +12,8 @@ import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import androidx.fragment.app.DialogFragment;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -23,7 +24,7 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.QuickWizard;
import info.nightscout.androidaps.data.QuickWizardEntry;
import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.general.overview.events.EventQuickWizardChange;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SafeParse;
@ -152,7 +153,7 @@ public class EditQuickWizardDialog extends DialogFragment implements View.OnClic
}
QuickWizard.INSTANCE.addOrUpdate(entry);
dismiss();
MainApp.bus().post(new EventQuickWizardChange());
RxBus.INSTANCE.send(new EventQuickWizardChange());
break;
case R.id.cancel:
dismiss();

View file

@ -1,10 +0,0 @@
package info.nightscout.androidaps.plugins.general.overview.events;
import info.nightscout.androidaps.events.Event;
/**
* Created by mike on 20.10.2016.
*/
public class EventQuickWizardChange extends Event {
}

View file

@ -0,0 +1,5 @@
package info.nightscout.androidaps.plugins.general.overview.events
import info.nightscout.androidaps.events.Event
class EventQuickWizardChange : Event()