Skip to content

Commit

Permalink
Merge pull request #3 from AdamG95/Restore-notifications-after-crash
Browse files Browse the repository at this point in the history
Restore notifications after crash
  • Loading branch information
AdamGuidarini authored Aug 7, 2022
2 parents b31344c + 35fa199 commit f3dbf4c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static void setCardParams(CardView cardView)
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
cardView.setLayoutParams(layoutParams);
ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) cardView.getLayoutParams();
marginLayoutParams.setMargins(25, 40, 25, 20);
marginLayoutParams.setMargins(25, 40, 10, 10);
cardView.requestLayout();
cardView.setRadius(30);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,7 @@ private void clearPreviousNotifications()
}
else
{
long[] ids = db.getMedicationTimeIds(medication);

for (long id : ids)
for (long id : medIds)
{
NotificationHelper.deletePendingNotification(id * -1, this);
}
Expand Down
44 changes: 37 additions & 7 deletions app/src/main/java/projects/medicationtracker/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;

public class MainActivity extends AppCompatActivity
Expand All @@ -53,7 +54,7 @@ protected void onCreate(Bundle savedInstanceState)
Objects.requireNonNull(getSupportActionBar()).setTitle("Medication Schedule");

NotificationHelper.createNotificationChannel(this);
// prepareNotifications();
prepareNotifications();

createMainActivityViews();
}
Expand Down Expand Up @@ -467,18 +468,47 @@ private void prepareNotifications()

for (Medication medication : medications)
{
LocalTime[] times = db.getMedicationTimes(medication.getMedId());
long[] timeIds = db.getMedicationTimeIds(medication);
long[] medicationTimeIds = db.getMedicationTimeIds(medication);

for (int i = 0; i < times.length; i++)
if (medication.getMedFrequency() == 1440)
{
NotificationHelper.deletePendingNotification(medication.getMedId(), this);

}
else
{
for (int i = 0; i < medicationTimeIds.length; i++)
{
NotificationHelper.deletePendingNotification(medicationTimeIds[i], this);
}
}
}

for (Medication medication : medications)
{
long[] medicationTimeIds = db.getMedicationTimeIds(medication);

if (medication.getMedFrequency() == 1440)
{
NotificationHelper.scheduleNotification(
this,
getApplicationContext(),
medication,
LocalDateTime.of(medication.getStartDate().toLocalDate(), times[i]),
times.length > 1 ? timeIds[i] * -1 : medication.getMedId()
LocalDateTime.of(LocalDate.now(), medication.getTimes()[0].toLocalTime()),
medication.getMedId()
);
}
else
{
for (int i = 0; i < medicationTimeIds.length; i++)
{
NotificationHelper.scheduleNotification(
getApplicationContext(),
medication,
LocalDateTime.of(LocalDate.now(), medication.getTimes()[i].toLocalTime()),
medicationTimeIds[i] * -1
);
}
}
}
}

Expand Down
3 changes: 0 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
android:layout_gravity="bottom"
android:layout_weight=".01">


<Button
android:id="@+id/navButtonLeft"
android:layout_width="45dp"
Expand All @@ -77,7 +76,5 @@
android:gravity="end"
android:layout_alignParentEnd="true" />
</RelativeLayout>

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 0 additions & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="colorButtonNormal">@color/black</item>
</style>
</resources>

0 comments on commit f3dbf4c

Please sign in to comment.