Merge pull request #84 from MilosKozak/notifications

basic notification functionality
This commit is contained in:
Milos Kozak 2016-12-06 23:05:23 +01:00 committed by GitHub
commit d897b778a0
12 changed files with 303 additions and 4 deletions

View file

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View file

@ -295,7 +295,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
bgDiff = lastBgValue - targetBGHigh;
}
bg.setText(lastBg.valueToUnitsToString(units) + " ISF: " + DecimalFormatter.to0Decimal(sens));
bg.setText(lastBg.valueToUnitsToString(units) + " ISF: " + DecimalFormatter.to1Decimal(sens));
bgInsulin.setText(DecimalFormatter.to2Decimal(bgDiff / sens) + "U");
bgInput.removeTextChangedListener(textWatcher);
//bgInput.setText(lastBg.valueToUnitsToString(units));
@ -368,10 +368,10 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
BolusWizard wizard = new BolusWizard();
wizard.doCalc(specificProfile, carbsAfterConstraint, c_bg, corrAfterConstraint, bolusIobCheckbox.isChecked(), basalIobCheckbox.isChecked());
bg.setText(c_bg + " ISF: " + DecimalFormatter.to0Decimal(wizard.sens));
bg.setText(c_bg + " ISF: " + DecimalFormatter.to1Decimal(wizard.sens));
bgInsulin.setText(DecimalFormatter.to2Decimal(wizard.insulinFromBG) + "U");
carbs.setText(DecimalFormatter.to0Decimal(c_carbs) + "g IC: " + DecimalFormatter.to0Decimal(wizard.ic));
carbs.setText(DecimalFormatter.to0Decimal(c_carbs) + "g IC: " + DecimalFormatter.to1Decimal(wizard.ic));
carbsInsulin.setText(DecimalFormatter.to2Decimal(wizard.insulinFromCarbs) + "U");
bolusIobInsulin.setText(DecimalFormatter.to2Decimal(wizard.insulingFromBolusIOB) + "U");

View file

@ -0,0 +1,20 @@
package info.nightscout.androidaps.plugins.Overview;
import java.util.Date;
/**
* Created by mike on 03.12.2016.
*/
public class Notification {
public static final int URGENT = 0;
public static final int NORMAL = 1;
public static final int LOW = 2;
public static final int INFO = 3;
public int id;
Date date;
String text;
Date validTo = new Date(0);
int level;
}

View file

@ -0,0 +1,60 @@
package info.nightscout.androidaps.plugins.Overview;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
/**
* Created by mike on 03.12.2016.
*/
public class NotificationStore {
public List<Notification> store = new ArrayList<Notification>();
public NotificationStore() {
}
public class NotificationComparator implements Comparator<Notification> {
@Override
public int compare(Notification o1, Notification o2) {
return o1.level - o2.level;
}
}
public Notification get(int index) {
return store.get(index);
}
public void add(Notification n) {
for (int i = 0; i < store.size(); i++) {
if (get(i).id == n.id) {
get(i).date = n.date;
get(i).validTo = n.validTo;
return;
}
}
store.add(n);
Collections.sort(store, new NotificationComparator());
}
public void remove(int id) {
for (int i = 0; i < store.size(); i++) {
if (get(i).id == id) {
store.remove(i);
return;
}
}
}
public void removeExpired() {
for (int i = 0; i < store.size(); i++) {
Notification n = get(i);
if (n.validTo.getTime() != 0 && n.validTo.getTime() < new Date().getTime()) {
store.remove(i);
}
}
}
}

View file

@ -14,6 +14,9 @@ import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.View;
@ -35,6 +38,7 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
@ -69,6 +73,8 @@ import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTreatmentDialog;
import info.nightscout.androidaps.plugins.Overview.Dialogs.WizardDialog;
import info.nightscout.androidaps.plugins.Overview.GraphSeriesExtension.PointsWithLabelGraphSeries;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.BolusWizard;
import info.nightscout.utils.DateUtil;
@ -98,6 +104,9 @@ public class OverviewFragment extends Fragment {
TextView apsModeView;
GraphView bgGraph;
RecyclerView notificationsView;
LinearLayoutManager llm;
LinearLayout cancelTempLayout;
LinearLayout acceptTempLayout;
LinearLayout quickWizardLayout;
@ -148,6 +157,11 @@ public class OverviewFragment extends Fragment {
quickWizardButton = (Button) view.findViewById(R.id.overview_quickwizard);
quickWizardLayout = (LinearLayout) view.findViewById(R.id.overview_quickwizardlayout);
notificationsView = (RecyclerView) view.findViewById(R.id.overview_notifications);
notificationsView.setHasFixedSize(true);
llm = new LinearLayoutManager(view.getContext());
notificationsView.setLayoutManager(llm);
treatmentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -383,6 +397,12 @@ public class OverviewFragment extends Fragment {
@Subscribe
public void onStatusEvent(final EventNewBasalProfile ev) { updateGUIIfVisible(); }
@Subscribe
public void onStatusEvent(final EventNewNotification n) { updateNotifications(); }
@Subscribe
public void onStatusEvent(final EventDismissNotification n) { updateNotifications(); }
private void hideTempRecommendation() {
Activity activity = getActivity();
if (activity != null)
@ -407,6 +427,7 @@ public class OverviewFragment extends Fragment {
@SuppressLint("SetTextI18n")
public void updateGUI() {
updateNotifications();
BgReading actualBG = MainApp.getDbHelper().actualBg();
BgReading lastBG = MainApp.getDbHelper().lastBg();
if (MainApp.getConfigBuilder() == null || MainApp.getConfigBuilder().getActiveProfile() == null) // app not initialized yet
@ -758,4 +779,95 @@ public class OverviewFragment extends Fragment {
}
//Notifications
public static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.NotificationsViewHolder> {
List<Notification> notificationsList;
RecyclerViewAdapter(List<Notification> notificationsList) {
this.notificationsList = notificationsList;
}
@Override
public NotificationsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.overview_notification_item, viewGroup, false);
NotificationsViewHolder notificationsViewHolder = new NotificationsViewHolder(v);
return notificationsViewHolder;
}
@Override
public void onBindViewHolder(NotificationsViewHolder holder, int position) {
DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
Notification notification = notificationsList.get(position);
holder.dismiss.setTag(notification);
holder.text.setText(notification.text);
holder.time.setText(df.format(notification.date));
if (notification.level == Notification.URGENT)
holder.cv.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.notificationUrgent));
else if (notification.level == Notification.NORMAL)
holder.cv.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.notificationNormal));
else if (notification.level == Notification.LOW)
holder.cv.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.notificationLow));
else if (notification.level == Notification.INFO)
holder.cv.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.notificationInfo));
}
@Override
public int getItemCount() {
return notificationsList.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class NotificationsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cv;
TextView time;
TextView text;
Button dismiss;
NotificationsViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.notification_cardview);
time = (TextView) itemView.findViewById(R.id.notification_time);
text = (TextView) itemView.findViewById(R.id.notification_text);
dismiss = (Button) itemView.findViewById(R.id.notification_dismiss);
dismiss.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Notification notification = (Notification) v.getTag();
switch (v.getId()) {
case R.id.notification_dismiss:
MainApp.bus().post(new EventDismissNotification(notification.id));
break;
}
}
}
}
void updateNotifications() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
NotificationStore nstore = getPlugin().notificationStore;
nstore.removeExpired();
if (nstore.store.size() > 0) {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(nstore.store);
notificationsView.setAdapter(adapter);
notificationsView.setVisibility(View.VISIBLE);
} else {
notificationsView.setVisibility(View.GONE);
}
}
});
}
}

View file

@ -3,12 +3,16 @@ package info.nightscout.androidaps.plugins.Overview;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.squareup.otto.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
/**
* Created by mike on 05.08.2016.
@ -20,6 +24,8 @@ public class OverviewPlugin implements PluginBase {
public QuickWizard quickWizard = new QuickWizard();
public NotificationStore notificationStore = new NotificationStore();
public OverviewPlugin() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
String storedData = preferences.getString("QuickWizard", "[]");
@ -28,6 +34,7 @@ public class OverviewPlugin implements PluginBase {
} catch (JSONException e) {
e.printStackTrace();
}
MainApp.bus().register(this);
}
@Override
@ -71,4 +78,14 @@ public class OverviewPlugin implements PluginBase {
}
@Subscribe
public void onStatusEvent(final EventNewNotification n) {
notificationStore.add(n.notification);
}
@Subscribe
public void onStatusEvent(final EventDismissNotification n) {
notificationStore.remove(n.id);
}
}

View file

@ -0,0 +1,14 @@
package info.nightscout.androidaps.plugins.Overview.events;
/**
* Created by mike on 03.12.2016.
*/
public class EventDismissNotification {
public int id;
public EventDismissNotification(int did) {
id = did;
}
}

View file

@ -0,0 +1,15 @@
package info.nightscout.androidaps.plugins.Overview.events;
import info.nightscout.androidaps.plugins.Overview.Notification;
/**
* Created by mike on 03.12.2016.
*/
public class EventNewNotification {
public Notification notification;
public EventNewNotification(Notification n) {
notification = n;
}
}

View file

@ -13,6 +13,13 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/overview_notifications"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

View file

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/notification_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardBackgroundColor="@color/cardColorBackground"
card_view:cardCornerRadius="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:text="Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/notification_time"
android:layout_weight="0.1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/notification_text"
android:layout_weight="1"
android:text="Notification text" />
<Button
android:text="Dismiss"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:id="@+id/notification_dismiss"
android:layout_weight="0.2" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>

View file

@ -24,4 +24,9 @@
<color name="colorPumpLabel">#779ECB</color>
<color name="cardItemLabel">#FF478EFF</color>
<color name="notificationUrgent">#ff0400</color>
<color name="notificationNormal">#ff5e55</color>
<color name="notificationLow">#ff827c</color>
<color name="notificationInfo">#009705</color>
</resources>

View file

@ -123,6 +123,8 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.gms/play-services-base/7.3.0/jars" />
@ -131,11 +133,13 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/me.denley.wearpreferenceactivity/wearpreferenceactivity/0.5.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/ustwo-clockwise-debug/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />