Skip to content

Commit

Permalink
Merge pull request #296 from Glucosio/reminders
Browse files Browse the repository at this point in the history
Add Reminders
  • Loading branch information
emartynov authored Sep 20, 2016
2 parents 10d731d + 6c1895b commit acacf1d
Show file tree
Hide file tree
Showing 18 changed files with 828 additions and 17 deletions.
20 changes: 18 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@
<!-- Required for Google Cloud Messaging -->
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />

<!-- Required to set reminders -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- Enable Chromebook support (no touch) -->
<uses-feature android:name="android.hardware.touchscreen"
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />

<permission
Expand Down Expand Up @@ -154,6 +158,7 @@

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
Expand All @@ -169,7 +174,7 @@
android:value="org.glucosio.android.activity.MainActivity" />
</activity>

<!-- Google Cloud Messaging receiver -->
<!-- Google Cloud Messaging receiver -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
Expand All @@ -195,6 +200,15 @@
</intent-filter>
</service>

<receiver
android:name="org.glucosio.android.receivers.GlucosioBroadcastReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</receiver>

<!--
Register AnalyticsReceiver and AnalyticsService to support background
dispatching on non-Google Play devices
Expand Down Expand Up @@ -236,9 +250,11 @@
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.RemindersActivity"></activity>
</application>

</manifest>
13 changes: 11 additions & 2 deletions app/src/main/java/org/glucosio/android/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class MainActivity extends AppCompatActivity implements DatePickerDialog.
private static final int REQUEST_INVITE = 1;
private static final String INTENT_EXTRA_PAGER = "pager";
private final String INTENT_EXTRA_DROPDOWN = "history_dropdown";
BottomSheetBehavior bottomSheetBehavior;
private ExportPresenter exportPresenter;
private RadioButton exportRangeButton;
private HomePagerAdapter homePagerAdapter;
Expand All @@ -91,7 +92,6 @@ public class MainActivity extends AppCompatActivity implements DatePickerDialog.
private TextView exportDialogDateTo;
private View bottomSheetAddDialogView;
private FloatingActionButton fabAddReading;
BottomSheetBehavior bottomSheetBehavior;
private Toolbar toolbar;
private TabLayout tabLayout;

Expand Down Expand Up @@ -174,7 +174,7 @@ public void onClick(View view) {
final PrimaryDrawerItem itemInvite = new PrimaryDrawerItem().withName(R.string.action_invite).withIcon(R.drawable.ic_face_grey_24dp).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
final PrimaryDrawerItem itemDonate = new PrimaryDrawerItem().withName(R.string.about_donate).withIcon(R.drawable.ic_favorite_grey_24dp).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);
final PrimaryDrawerItem itemA1C = new PrimaryDrawerItem().withName(R.string.activity_converter_title).withIcon(R.drawable.ic_calculator_a1c_grey_24dp).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);

final PrimaryDrawerItem itemReminders = new PrimaryDrawerItem().withName(R.string.activity_reminders_title).withIcon(R.drawable.ic_alarm_grey_24dp).withSelectable(false).withTypeface(Typeface.DEFAULT_BOLD);

DrawerBuilder drawerBuilder = new DrawerBuilder()
.withActivity(this)
Expand Down Expand Up @@ -209,6 +209,8 @@ public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
openDonateIntent();
} else if (drawerItem.equals(itemA1C)) {
openA1CCalculator();
} else if (drawerItem.equals(itemReminders)) {
openRemindersActivity();
}
return false;
}
Expand All @@ -217,6 +219,7 @@ public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (isPlayServicesAvailable()) {
drawerBuilder.addDrawerItems(
itemA1C,
itemReminders,
itemExport,
itemSettings,
itemFeedback,
Expand All @@ -229,6 +232,7 @@ public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
} else {
drawerBuilder.addDrawerItems(
itemA1C,
itemReminders,
itemExport,
itemSettings,
itemFeedback,
Expand All @@ -255,6 +259,11 @@ public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
analytics.reportScreen("Main Activity");
}

private void openRemindersActivity() {
Intent intent = new Intent(this, RemindersActivity.class);
startActivity(intent);
}

private void initPresenters(GlucosioApplication application) {
final DatabaseHandler dbHandler = application.getDBHandler();
presenter = new MainPresenter(this, dbHandler);
Expand Down
128 changes: 128 additions & 0 deletions app/src/main/java/org/glucosio/android/activity/RemindersActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.glucosio.android.activity;

import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.wdullaer.materialdatetimepicker.time.RadialPickerLayout;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

import org.glucosio.android.R;
import org.glucosio.android.db.Reminder;
import org.glucosio.android.presenter.RemindersPresenter;
import org.glucosio.android.tools.AnimationTools;

import java.util.Calendar;

public class RemindersActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {

private FloatingActionButton addFab;
private RemindersPresenter presenter;
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
presenter = new RemindersPresenter(this);

Toolbar toolbar = (Toolbar) findViewById(R.id.activity_main_toolbar);

if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(2);
getSupportActionBar().setTitle(getResources().getString(R.string.activity_reminders_title));
}

addFab = (FloatingActionButton) findViewById(R.id.activity_reminders_fab_add);

addFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Open Time Picker on FAB click
boolean is24HourFormat = android.text.format.DateFormat.is24HourFormat(getApplicationContext());
Calendar cal = presenter.getCalendar();

TimePickerDialog tpd = TimePickerDialog.newInstance(
RemindersActivity.this,
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
is24HourFormat);
tpd.show(getFragmentManager(), "Timepickerdialog");
}
});

listView = (ListView) findViewById(R.id.activity_reminders_listview);
listView.setEmptyView(findViewById(R.id.activity_reminders_listview_empty));
listView.setAdapter(presenter.getAdapter());
addFab.postDelayed(new Runnable() {
@Override
public void run() {
AnimationTools.startCircularReveal(addFab);
}
}, 600);
}

public void updateReminder(Reminder reminder) {
presenter.updateReminder(reminder);
presenter.saveReminders();
}

public void updateRemindersList() {
listView.setAdapter(presenter.getAdapter());
listView.invalidate();
}

@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int seconds) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
// Id is HOURS+MINUTES to avoid duplicates
String concatenatedId = hourOfDay + "" + minute;
// Metric is always glucose until I write support for other metrics so...
// TODO: Add Reminders for other metrics
// Also oneTime is always set to false until I implement one time alarms
// TODO: Implement one time alarms
presenter.addReminder(Long.parseLong(concatenatedId), cal.getTime(), "glucose", false, true);
}

public void showDuplicateError() {
View parentLayout = findViewById(R.id.activity_reminders_root_view);
Snackbar.make(parentLayout, R.string.activitiy_reminders_error_duplicate, Snackbar.LENGTH_SHORT).show();
}

public void showBottomSheetDialog(final long id) {
final BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(this);
View sheetView = getLayoutInflater().inflate(R.layout.fragment_reminders_bottom_sheet, null);
LinearLayout delete = (LinearLayout) sheetView.findViewById(R.id.fragment_history_bottom_sheet_delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
presenter.deleteReminder(id);
updateRemindersList();
mBottomSheetDialog.dismiss();
}
});

mBottomSheetDialog.setContentView(sheetView);
mBottomSheetDialog.show();
}

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
presenter.saveReminders();
finish();
}
return super.onOptionsItemSelected(menuItem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.glucosio.android.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import org.glucosio.android.R;
import org.glucosio.android.activity.RemindersActivity;
import org.glucosio.android.db.Reminder;
import org.glucosio.android.tools.FormatDateTime;

import java.util.Calendar;
import java.util.List;

public class RemindersAdapter extends ArrayAdapter<Reminder> {
private Context context;
private List<Reminder> items;
private Calendar calendar;
private FormatDateTime formatDateTime;

public RemindersAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}

public RemindersAdapter(Context context, int resource, List<Reminder> items) {
super(context, resource, items);
this.context = context;
this.items = items;
calendar = Calendar.getInstance();
formatDateTime = new FormatDateTime(context);
}

@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_reminder_item, parent, false);
}

LinearLayout rootView = (LinearLayout) v.findViewById(R.id.activity_reminders_root_view);
TextView timeTextView = (TextView) v.findViewById(R.id.activity_reminders_item_time);
Switch activeSwitch = (Switch) v.findViewById(R.id.activity_reminders_item_enabled);
final Reminder reminder = items.get(position);
final long reminderId = reminder.getId();

calendar.setTime(reminder.getAlarmTime());
timeTextView.setText(formatDateTime.getTime(calendar));
activeSwitch.setChecked(reminder.isActive());

activeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Reminder updatedReminder = new Reminder(reminder.getId(), reminder.getAlarmTime(), reminder.getMetric(),
reminder.isOneTime(), reminder.isActive());
updatedReminder.setActive(b);
updateReminder(updatedReminder);
}
});

rootView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
showBottomSheetMenu(reminderId);
return true;
}
});

return v;
}

private void showBottomSheetMenu(long id) {
((RemindersActivity) context).showBottomSheetDialog(id);
}

private void updateReminder(Reminder reminder) {
// Should be always true.
// I HOPE...
if (context instanceof RemindersActivity) {
((RemindersActivity) context).updateReminder(reminder);
}
}
}
Loading

0 comments on commit acacf1d

Please sign in to comment.