Skip to content

Commit

Permalink
Merge pull request #115 from CSE-110-Winter-2024/tomorrow-tests
Browse files Browse the repository at this point in the history
Fully implement US4, various recurring goal & testing improvements
  • Loading branch information
emguz authored Mar 14, 2024
2 parents 36353b6 + 04a054d commit 2779cf0
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 58 deletions.
18 changes: 16 additions & 2 deletions app/src/main/java/edu/ucsd/cse110/successorator/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.ucsd.cse110.successorator;


import android.app.AlertDialog;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
Expand Down Expand Up @@ -70,10 +71,23 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//Pending logic so that click does nothing
if (mainViewModel.getCurrentMode().getValue() == AppMode.PENDING) {
var appMode = mainViewModel.getCurrentMode().getValue();

if (appMode == AppMode.PENDING || appMode == appMode.RECURRING) {
// Do nothing
} else {
mainViewModel.pressGoal(Math.toIntExact(id));
boolean success = mainViewModel.pressGoal(Math.toIntExact(id));
if (!success) {
// Pasted from piazza
String flavorText = "This goal is still active for Today. If you've finished this goal for Today, mark it finished in that view.";
if (appMode == AppMode.TODAY) {
flavorText = "This goal is active for Tomorrow. If you're not done for this goal Tomorrow, unfinish it in that view.";
}
new AlertDialog.Builder(MainActivity.this)
.setTitle("Goal is still active!")
.setMessage(flavorText)
.show();
}
}
}
});
Expand Down
54 changes: 26 additions & 28 deletions app/src/main/java/edu/ucsd/cse110/successorator/MainViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public class MainViewModel extends ViewModel {
private final MutableSubject<Calendar> currentDateLocalized;
private final MutableSubject<String> currentTitleString;
private final MutableSubject<String> currentWeekday;
private final MutableSubject<String> currentNumberedWeekday;
private final MutableSubject<String> currentDateString;
private final MutableSubject<String> monthlyButtonString;
private final MutableSubject<String> yearlyButtonString;
private final MutableSubject<Context> currentContext;
private final MutableSubject<AppMode> currentMode;
private final Calendar dateConverter;
Expand Down Expand Up @@ -86,8 +86,8 @@ public MainViewModel(GoalRepository goalRepository, MutableSubject<Long> offset,
this.currentDateLocalized = new SimpleSubject<>();
this.currentTitleString = new SimpleSubject<>();
this.currentWeekday = new SimpleSubject<>();
this.currentNumberedWeekday = new SimpleSubject<>();
this.currentDateString = new SimpleSubject<>();
this.monthlyButtonString = new SimpleSubject<>();
this.yearlyButtonString = new SimpleSubject<>();
this.currentMode = new SimpleSubject<>();
this.currentMode.setValue(AppMode.TODAY);

Expand Down Expand Up @@ -290,11 +290,7 @@ public boolean pressGoal(int goalId) {
return false;
}
}
} else if (appMode.equals(AppMode.RECURRING)) {
this.deleteRecurringGoal(goalId);
return true;
}

if (goal.completed()) {
var newGoal = goal.markIncomplete();
goalRepository.update(newGoal);
Expand All @@ -312,7 +308,6 @@ public boolean pressGoal(int goalId) {
}

// Open question whether this method should take a Context or a String
// TODO: test these methods in the PRs where they actually start being used
public void activateFocusMode(Context context) {
this.currentContext.setValue(context);
}
Expand Down Expand Up @@ -350,16 +345,16 @@ public Subject<List<Goal>> getGoalsToDisplay() {
}

// Exporting these strings for UI
public Subject<String> getCurrentWeekday() {
public Subject<String> getWeeklyButtonString() {
return currentWeekday;
}

public Subject<String> getCurrentNumberedWeekday() {
return currentNumberedWeekday;
public Subject<String> getMonthlyButtonString() {
return monthlyButtonString;
}

public Subject<String> getCurrentDateString() {
return currentDateString;
public Subject<String> getYearlyButtonString() {
return yearlyButtonString;
}

public Subject<AppMode> getCurrentMode() {
Expand Down Expand Up @@ -390,7 +385,7 @@ private void updateCalendarStrings(AppMode appMode, Calendar localized) {

formatter.applyPattern("EEE");
var weekday = formatter.format(displayDate.getTime());
this.currentWeekday.setValue(weekday);
this.currentWeekday.setValue("Weekly on " + weekday);

var weekday_num = displayDate.get(Calendar.DAY_OF_WEEK_IN_MONTH);
var weekday_string = Integer.toString(weekday_num);
Expand All @@ -403,11 +398,11 @@ private void updateCalendarStrings(AppMode appMode, Calendar localized) {
} else {
weekday_string += "th";
}
this.currentNumberedWeekday.setValue(weekday_string + " " + weekday);
this.monthlyButtonString.setValue("Monthly on " + weekday_string + " " + weekday);

formatter.applyPattern("M/d");
dateString = formatter.format(displayDate.getTime());
this.currentDateString.setValue(dateString);
this.yearlyButtonString.setValue("Yearly on " + dateString);
}


Expand All @@ -416,6 +411,11 @@ public void addGoal(String contents, Context context) {
// At the same time, I don't want to deal with nulls, so I'll just use the current time
var currentTime = this.currentDate.getValue();
if (currentTime == null) return;
var appMode = this.currentMode.getValue();
if (appMode == null) return;
if (appMode.equals(TOMORROW)) {
currentTime += 24 * 60 * 60 * 1000;
}
var newGoal = new Goal(null, contents, 0, false, currentTime, false, false, RecurrenceType.NONE, context, currentTime, null, null, null, false);
goalRepository.append(newGoal);
}
Expand All @@ -429,21 +429,23 @@ public void addPendingGoal(String contents, Context context) {

// Returns true if the goal was added, or false if it wasn't (due to the selected date being in the past)
public boolean addRecurringGoal(String contents, int year, int month, int day, RecurrenceType recurrenceType, Context context) {
if (context == null) return false;
if (recurrenceType == null) return false;
var currentTime = this.currentDateLocalized.getValue();
if (currentTime == null) return false;
var selectedDate = (Calendar) currentTime.clone();
// Set at 12:00 PM to avoid 2am edge cases
selectedDate.set(year, month, day, 12, 0, 0);
// Is this a bug? It seems like we are checking if selected date is equal to itself
// if (selectedDate.get(Calendar.DAY_OF_MONTH) == day && selectedDate.get(Calendar.YEAR) == year && selectedDate.get(Calendar.MONTH) == month) {
// return false;
// }
if (selectedDate.get(Calendar.DAY_OF_MONTH) != day || selectedDate.get(Calendar.YEAR) != year && selectedDate.get(Calendar.MONTH) != month) {
return false;
}
currentTime = TimeUtils.twoAMNormalized(currentTime);
if (selectedDate.before(currentTime)) return false;
var selectedMoment = selectedDate.getTimeInMillis();
var recurringGoal = new Goal(null, contents, 0, false, selectedMoment, false, true, recurrenceType, context, selectedDate.getTimeInMillis(), null, null, null, false);
var recurringGoal = new Goal(null, contents, 0, false, selectedMoment, false, true, recurrenceType, context, selectedMoment, null, null, null, false);
var goalId = goalRepository.append(recurringGoal);
recurringGoal = goalRepository.findGoal(goalId);
recurringGoal = goalRepository.findGoal(goalId); // Populate object with db id
handleRecurringGoalGeneration(recurringGoal, currentTime);
return true;
}
Expand Down Expand Up @@ -559,10 +561,6 @@ public void deleteRecurringGoal(int goalId) {
}
}

}
}

// public AppMode getCurrentMode() {
// return currentMode.getValue();
// }
//}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.ViewModelProvider;

import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.TextView;

import edu.ucsd.cse110.successorator.MainViewModel;
Expand Down Expand Up @@ -54,6 +52,11 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState){
this.view = FragmentDialogCreateGoalBinding.inflate(getLayoutInflater());

// Hook up button labels to the view model
mainViewModel.getWeeklyButtonString().observe(text -> view.WeeklyRecurringGoalButton.setText(text));
mainViewModel.getMonthlyButtonString().observe(text -> view.MonthlyRecurringGoalButton.setText(text));
mainViewModel.getYearlyButtonString().observe(text -> view.YearlyRecurringGoalButton.setText(text));


// Create listener for context buttons
this.view.contextRadio.setOnCheckedChangeListener((group, checkedId) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import android.widget.Toast;

import edu.ucsd.cse110.successorator.MainViewModel;
import edu.ucsd.cse110.successorator.databinding.FragmentDialogCreateRecurringGoalBinding;
Expand Down Expand Up @@ -53,6 +54,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState){
this.view = FragmentDialogCreateRecurringGoalBinding.inflate(getLayoutInflater());

// Hook up prompt text to the view model


// Create listener for context buttons
Expand Down Expand Up @@ -93,8 +95,18 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState){
if(actionId == EditorInfo.IME_ACTION_DONE){
String content = view.goalInput.getText().toString();
//Context is defaulted to HOME, Needs US3 to be implemented.
mainViewModel.addRecurringGoal(content,Integer.parseInt(parts[2]), Integer.parseInt(parts[0]), Integer.parseInt(parts[1]),

boolean success = mainViewModel.addRecurringGoal(content,Integer.parseInt(parts[2]), Integer.parseInt(parts[0]) - 1, Integer.parseInt(parts[1]),
recurrenceType, context);
if (!success) {
// Display a popup telling the user to correct their date or select a context
// https://stackoverflow.com/questions/2115758/how-do-i-display-an-alert-dialog-on-android
new AlertDialog.Builder(getActivity())
.setTitle("Invalid input!")
.setMessage("Please make sure you've selected a context, selected a recurring interval, and made sure you've selected a valid date (today or later, mm/dd/yyyy, and not the 32nd day of a month)")
.show();
return false;
}

//Lambda functions allow for usage of this. in interface declaration.
//Interestingly, without it dismiss() appears to call the correct function regardless.
Expand Down
Loading

0 comments on commit 2779cf0

Please sign in to comment.