Skip to content

Commit

Permalink
Merge pull request #106 from ZeusWPI/menu
Browse files Browse the repository at this point in the history
Fix resto logic + add preference for closing time
  • Loading branch information
niknetniko authored Sep 30, 2016
2 parents 7d49c69 + 944d956 commit d5d16fa
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import be.ugent.zeus.hydra.activities.common.AppCompatPreferenceActivity;
import be.ugent.zeus.hydra.fragments.preferences.HomeFragment;
import be.ugent.zeus.hydra.fragments.preferences.MinervaFragment;
import be.ugent.zeus.hydra.fragments.preferences.NotificationFragment;
import be.ugent.zeus.hydra.fragments.preferences.RestoPreferenceFragment;
import be.ugent.zeus.hydra.fragments.preferences.SkoFragment;
import be.ugent.zeus.hydra.notifications.NotificationScheduler;

Expand Down Expand Up @@ -43,7 +43,7 @@ public void onBuildHeaders(List<Header> target) {
*/
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| NotificationFragment.class.getName().equals(fragmentName)
|| RestoPreferenceFragment.class.getName().equals(fragmentName)
|| HomeFragment.class.getName().equals(fragmentName)
|| MinervaFragment.class.getName().equals(fragmentName)
|| SkoFragment.class.getName().equals(fragmentName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
import be.ugent.zeus.hydra.HydraApplication;
import be.ugent.zeus.hydra.R;
import be.ugent.zeus.hydra.activities.resto.common.RestoWebsiteActivity;
import be.ugent.zeus.hydra.fragments.resto.RestoFragment;
import be.ugent.zeus.hydra.loaders.LoaderCallbackHandler;
import be.ugent.zeus.hydra.models.resto.RestoMenu;
import be.ugent.zeus.hydra.models.resto.RestoOverview;
import be.ugent.zeus.hydra.requests.resto.RestoMenuOverviewRequest;
import be.ugent.zeus.hydra.viewpager.MenuPagerAdapter;
import org.threeten.bp.LocalDate;
import org.threeten.bp.ZonedDateTime;

import java.util.Collections;

Expand Down Expand Up @@ -69,14 +67,10 @@ public void onPageSelected(int position) {
Intent intent = getIntent();

//Get the default start date
ZonedDateTime start = ZonedDateTime.now();
if(start.isAfter(start.withHour(RestoFragment.CLOSING_HOUR))) {
start = start.plusDays(1);
}
if(intent.hasExtra(ARG_DATE)) {
startDate = (LocalDate) intent.getSerializableExtra(ARG_DATE);
} else {
startDate = start.toLocalDate();
startDate = LocalDate.now();
}

loaderHandler.startLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import be.ugent.zeus.hydra.models.resto.RestoOverview;
import be.ugent.zeus.hydra.recyclerview.adapters.HomeCardAdapter;
import be.ugent.zeus.hydra.requests.resto.RestoMenuOverviewRequest;
import org.threeten.bp.LocalDate;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -29,11 +28,11 @@ public RestoMenuCallback(Context context, HomeCardAdapter adapter, FragmentCallb
@Override
protected List<HomeCard> convertData(@NonNull RestoOverview data) {
List<HomeCard> menuCardList = new ArrayList<>();
for (RestoMenu menu : data) {
if (menu.getDate().isAfter(LocalDate.now())) {
menuCardList.add(new RestoMenuCard(menu));
}

for (RestoMenu menu : RestoOverview.filter(data, context)) {
menuCardList.add(new RestoMenuCard(menu));
}

return menuCardList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*
* @author Rien Maertens
*/
public class NotificationFragment extends PreferenceFragment {
public class RestoPreferenceFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_notifications);
addPreferencesFromResource(R.xml.pref_resto);

// Set and remove notifications
final NotificationScheduler scheduler = new NotificationScheduler(getActivity());
final CheckBoxPreference notificationCheckbox = (CheckBoxPreference) findPreference("pref_key_daily_notifications_checkbox");
TimePreference notificationTime = (TimePreference) findPreference("pref_daily_notifications_time");
TimePreference notificationTime = (TimePreference) findPreference("pref_resto_notifications_time");

notificationCheckbox.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
Expand All @@ -44,7 +44,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if(notificationCheckbox.isChecked()){
scheduler.scheduleNotification(newValue);
scheduler.scheduleNotification((String) newValue);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

/**
* Displays the resto menu for one day.
*
* The subclasses should decided in what manner the data is represented.
*/
public class MenuFragment extends Fragment {

Expand All @@ -36,9 +34,6 @@ public static MenuFragment newInstance(RestoMenu menu) {
return fragment;
}

/**
* Some implementations may wish to persist the data.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -16,25 +17,26 @@
import be.ugent.zeus.hydra.activities.resto.MenuActivity;
import be.ugent.zeus.hydra.activities.resto.MetaActivity;
import be.ugent.zeus.hydra.activities.resto.SandwichActivity;
import be.ugent.zeus.hydra.fragments.common.CachedLoaderFragment;
import be.ugent.zeus.hydra.fragments.common.LoaderFragment;
import be.ugent.zeus.hydra.loaders.RequestAsyncTaskLoader;
import be.ugent.zeus.hydra.loaders.ThrowableEither;
import be.ugent.zeus.hydra.models.resto.RestoMenu;
import be.ugent.zeus.hydra.models.resto.RestoOverview;
import be.ugent.zeus.hydra.requests.common.ProcessableCacheRequest;
import be.ugent.zeus.hydra.requests.resto.RestoMenuOverviewRequest;
import be.ugent.zeus.hydra.utils.DateUtils;
import be.ugent.zeus.hydra.utils.ViewUtils;
import be.ugent.zeus.hydra.views.MenuTable;
import org.threeten.bp.LocalDateTime;

import java.util.ArrayList;

import static be.ugent.zeus.hydra.utils.ViewUtils.$;

/**
* @author Niko Strijbol
* @author mivdnber
*/
public class RestoFragment extends CachedLoaderFragment<RestoOverview> {

//The hour after which every resto is closed.
public static final int CLOSING_HOUR = 20;
public class RestoFragment extends LoaderFragment<ArrayList<RestoMenu>> {

private TextView title;
private MenuTable table;
Expand All @@ -58,13 +60,6 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

setIcons();

viewMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), MenuActivity.class));
}
});

viewSandwich.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -109,35 +104,43 @@ private void setIcons() {
viewResto.setCompoundDrawablesWithIntrinsicBounds(null, restoIcon, null, null);
}

/**
* This must be called when data is received that has no errors.
*
* @param data The data.
*/
@Override
public void receiveData(@NonNull RestoOverview data) {
public void receiveData(@NonNull ArrayList<RestoMenu> data) {

//We can't do anything without data.
if(data.size() < 2) {
//Check that we have at least one menu
//TODO: show error
if(data.size() < 1) {
return;
}

RestoMenu menu = data.get(0);
LocalDateTime now = LocalDateTime.now();
if(now.isAfter(now.withHour(CLOSING_HOUR)) || now.isAfter(menu.getDate().atStartOfDay())) {
menu = data.get(1);
}
final RestoMenu menu = data.get(0);

table.setMenu(menu);

title.setText(String.format(getString(R.string.resto_menu_title), DateUtils.getFriendlyDate(menu.getDate())));

viewMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), MenuActivity.class);
intent.putExtra(MenuActivity.ARG_DATE, menu.getDate());
startActivity(intent);
}
});
}

/**
* @return The request that will be executed.
* Do the filtering of the menu's in the background as well.
*/
@Override
protected RestoMenuOverviewRequest getRequest() {
return new RestoMenuOverviewRequest();
public Loader<ThrowableEither<ArrayList<RestoMenu>>> getLoader() {
return new RequestAsyncTaskLoader<>(
new ProcessableCacheRequest<RestoOverview, ArrayList<RestoMenu>>(getContext(), new RestoMenuOverviewRequest(), shouldRenew) {
@NonNull
@Override
protected ArrayList<RestoMenu> transform(@NonNull RestoOverview data) {
return RestoOverview.filter(data, getContext());
}
}, getContext()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package be.ugent.zeus.hydra.models.resto;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import org.threeten.bp.LocalDate;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.LocalTime;

import java.util.ArrayList;

/**
Expand All @@ -10,4 +18,37 @@
* @author Niko Strijbol
* @author mivdnber
*/
public class RestoOverview extends ArrayList<RestoMenu> {}
public class RestoOverview extends ArrayList<RestoMenu> {

public static final String DEFAULT_CLOSING_TIME = "21:00";
public static final String PREF_RESTO_CLOSING_HOUR = "pref_resto_closing_hour";

/**
* Filter the resto menu's. After 20:00, the menu of today is removed from the list. This will also remove resto's
* that are not today.
*
* @param data The original data, probably from the server.
* @return The filtered data.
*/
public static ArrayList<RestoMenu> filter(ArrayList<RestoMenu> data, Context context) {

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

LocalTime closingHour = LocalTime.parse(preferences.getString(PREF_RESTO_CLOSING_HOUR, DEFAULT_CLOSING_TIME));

ArrayList<RestoMenu> list = new ArrayList<>();

final LocalDate today = LocalDate.now();
final boolean isTooLate = LocalDateTime.now().isAfter(LocalDateTime.of(LocalDate.now(), closingHour));

//we still DONT have filters
for(RestoMenu menu: data) {
//Menu is skipped if (it is before today) or (it is too late and it is today)
if(!menu.getDate().isBefore(today) && (!isTooLate || !menu.getDate().isEqual(today))) {
list.add(menu);
}
}

return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import be.ugent.zeus.hydra.preference.Time;

import java.util.Calendar;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.LocalTime;
import org.threeten.bp.ZonedDateTime;

/**
* @author Rien Maertens
Expand Down Expand Up @@ -41,19 +42,30 @@ public void testNotification(){
}

public void scheduleNotification(){
scheduleNotification(preferences.getInt("pref_daily_notifications_time", 0));
scheduleNotification(preferences.getString("pref_resto_notifications_time", "12:00"));
}

public void scheduleNotification(Object time){
Time timeObj = new Time(time);
Calendar cal = timeObj.nextOccurrence();
public void scheduleNotification(String time) {

LocalTime notificationTime = LocalTime.parse(time);
LocalDateTime now = LocalDateTime.now();

ZonedDateTime nextOccurrence = ZonedDateTime.now();

//Get the next occurrence
if(notificationTime.isAfter(now.toLocalTime())) {
nextOccurrence = nextOccurrence.plusDays(1);
}

nextOccurrence = nextOccurrence.withHour(notificationTime.getHour()).withMinute(notificationTime.getMinute());

cancelNotifications();

// Daily alarm
alarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
nextOccurrence.toInstant().toEpochMilli(),
AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
}
Loading

0 comments on commit d5d16fa

Please sign in to comment.