|  | @@ -4,6 +4,7 @@ import android.content.DialogInterface;
 | 
		
	
		
			
			| 4 | 4 |  import android.os.Bundle;
 | 
		
	
		
			
			| 5 | 5 |  import android.support.annotation.Nullable;
 | 
		
	
		
			
			| 6 | 6 |  import android.support.v4.app.Fragment;
 | 
		
	
		
			
			|  | 7 | +import android.support.v4.widget.SwipeRefreshLayout;
 | 
		
	
		
			
			| 7 | 8 |  import android.support.v7.app.AlertDialog;
 | 
		
	
		
			
			| 8 | 9 |  import android.support.v7.widget.LinearLayoutManager;
 | 
		
	
		
			
			| 9 | 10 |  import android.support.v7.widget.RecyclerView;
 | 
		
	
	
		
			
			|  | @@ -13,11 +14,25 @@ import android.view.ViewGroup;
 | 
		
	
		
			
			| 13 | 14 |  import android.widget.TextView;
 | 
		
	
		
			
			| 14 | 15 |  import android.widget.Toast;
 | 
		
	
		
			
			| 15 | 16 |  
 | 
		
	
		
			
			|  | 17 | +import com.luticate.utils.business.LuPromise;
 | 
		
	
		
			
			| 16 | 18 |  import com.rthoni.stssaguenay.R;
 | 
		
	
		
			
			| 17 | 19 |  import com.rthoni.stssaguenay.business.STSBusiness;
 | 
		
	
		
			
			|  | 20 | +import com.rthoni.stssaguenay.business.SchedulesBusiness;
 | 
		
	
		
			
			| 18 | 21 |  import com.rthoni.stssaguenay.dbo.FavouriteStopDbo;
 | 
		
	
		
			
			| 19 |  | -
 | 
		
	
		
			
			|  | 22 | +import com.rthoni.stssaguenay.dbo.SchedulesDbo;
 | 
		
	
		
			
			|  | 23 | +
 | 
		
	
		
			
			|  | 24 | +import org.joda.time.DateTime;
 | 
		
	
		
			
			|  | 25 | +import org.joda.time.DateTimeZone;
 | 
		
	
		
			
			|  | 26 | +import org.joda.time.LocalDateTime;
 | 
		
	
		
			
			|  | 27 | +import org.joda.time.format.DateTimeFormat;
 | 
		
	
		
			
			|  | 28 | +import org.joda.time.format.DateTimeFormatter;
 | 
		
	
		
			
			|  | 29 | +
 | 
		
	
		
			
			|  | 30 | +import java.util.Collection;
 | 
		
	
		
			
			|  | 31 | +import java.util.Collections;
 | 
		
	
		
			
			|  | 32 | +import java.util.Comparator;
 | 
		
	
		
			
			|  | 33 | +import java.util.Date;
 | 
		
	
		
			
			| 20 | 34 |  import java.util.List;
 | 
		
	
		
			
			|  | 35 | +import java.util.TimeZone;
 | 
		
	
		
			
			| 21 | 36 |  import java.util.Vector;
 | 
		
	
		
			
			| 22 | 37 |  
 | 
		
	
		
			
			| 23 | 38 |  import butterknife.BindView;
 | 
		
	
	
		
			
			|  | @@ -31,11 +46,15 @@ public class HomeFragment extends Fragment {
 | 
		
	
		
			
			| 31 | 46 |      @BindView(R.id.listFavouritesStops)
 | 
		
	
		
			
			| 32 | 47 |      RecyclerView _favouritesStopsList;
 | 
		
	
		
			
			| 33 | 48 |  
 | 
		
	
		
			
			|  | 49 | +    @BindView(R.id.swipeContainer)
 | 
		
	
		
			
			|  | 50 | +    SwipeRefreshLayout _swipeRefreshLayout;
 | 
		
	
		
			
			|  | 51 | +
 | 
		
	
		
			
			| 34 | 52 |      FavouriteStopsAdapter _adapter;
 | 
		
	
		
			
			| 35 | 53 |  
 | 
		
	
		
			
			| 36 | 54 |      public static class ViewHolder extends RecyclerView.ViewHolder {
 | 
		
	
		
			
			| 37 | 55 |          public View _parentView;
 | 
		
	
		
			
			| 38 | 56 |          public TextView _textView;
 | 
		
	
		
			
			|  | 57 | +        public TextView _textView2;
 | 
		
	
		
			
			| 39 | 58 |          public ViewHolder(View v) {
 | 
		
	
		
			
			| 40 | 59 |              super(v);
 | 
		
	
		
			
			| 41 | 60 |              _parentView = v;
 | 
		
	
	
		
			
			|  | @@ -46,12 +65,15 @@ public class HomeFragment extends Fragment {
 | 
		
	
		
			
			| 46 | 65 |  
 | 
		
	
		
			
			| 47 | 66 |          private List<FavouriteStopDbo> _favourites;
 | 
		
	
		
			
			| 48 | 67 |  
 | 
		
	
		
			
			|  | 68 | +        private List<SchedulesDbo> _schedules;
 | 
		
	
		
			
			|  | 69 | +
 | 
		
	
		
			
			| 49 | 70 |          @Override
 | 
		
	
		
			
			| 50 | 71 |          public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 | 
		
	
		
			
			| 51 | 72 |              View v = LayoutInflater.from(parent.getContext())
 | 
		
	
		
			
			| 52 | 73 |                      .inflate(R.layout.favourite_stop_recycler_view_item, parent, false);
 | 
		
	
		
			
			| 53 | 74 |              ViewHolder vh = new ViewHolder(v);
 | 
		
	
		
			
			| 54 | 75 |              vh._textView = (TextView) v.findViewById(R.id.textView);
 | 
		
	
		
			
			|  | 76 | +            vh._textView2 = (TextView) v.findViewById(R.id.textView2);
 | 
		
	
		
			
			| 55 | 77 |              return vh;
 | 
		
	
		
			
			| 56 | 78 |          }
 | 
		
	
		
			
			| 57 | 79 |  
 | 
		
	
	
		
			
			|  | @@ -72,6 +94,32 @@ public class HomeFragment extends Fragment {
 | 
		
	
		
			
			| 72 | 94 |                      return true;
 | 
		
	
		
			
			| 73 | 95 |                  }
 | 
		
	
		
			
			| 74 | 96 |              });
 | 
		
	
		
			
			|  | 97 | +
 | 
		
	
		
			
			|  | 98 | +            if (_schedules != null) {
 | 
		
	
		
			
			|  | 99 | +                List<LocalDateTime> schedules = new Vector<>();
 | 
		
	
		
			
			|  | 100 | +                for (SchedulesDbo schedule : _schedules) {
 | 
		
	
		
			
			|  | 101 | +                    if (schedule.getStopId().equals(favouriteStopDbo.getStop().getId())) {
 | 
		
	
		
			
			|  | 102 | +                        schedules.addAll(schedule.getSchedules());
 | 
		
	
		
			
			|  | 103 | +                    }
 | 
		
	
		
			
			|  | 104 | +                }
 | 
		
	
		
			
			|  | 105 | +
 | 
		
	
		
			
			|  | 106 | +                Collections.sort(schedules);
 | 
		
	
		
			
			|  | 107 | +                schedules.size();
 | 
		
	
		
			
			|  | 108 | +
 | 
		
	
		
			
			|  | 109 | +                StringBuilder text = new StringBuilder();
 | 
		
	
		
			
			|  | 110 | +
 | 
		
	
		
			
			|  | 111 | +                for (int i = 0; i < Math.min(5, schedules.size()); ++i) {
 | 
		
	
		
			
			|  | 112 | +                    if (i > 0) {
 | 
		
	
		
			
			|  | 113 | +                        text.append(", ");
 | 
		
	
		
			
			|  | 114 | +                    }
 | 
		
	
		
			
			|  | 115 | +                    text.append(schedules.get(i).toString("HH:mm"));
 | 
		
	
		
			
			|  | 116 | +                }
 | 
		
	
		
			
			|  | 117 | +
 | 
		
	
		
			
			|  | 118 | +                holder._textView2.setText(text.toString());
 | 
		
	
		
			
			|  | 119 | +            }
 | 
		
	
		
			
			|  | 120 | +            else {
 | 
		
	
		
			
			|  | 121 | +                holder._textView2.setText(R.string.loading_schedules);
 | 
		
	
		
			
			|  | 122 | +            }
 | 
		
	
		
			
			| 75 | 123 |          }
 | 
		
	
		
			
			| 76 | 124 |  
 | 
		
	
		
			
			| 77 | 125 |          @Override
 | 
		
	
	
		
			
			|  | @@ -81,6 +129,20 @@ public class HomeFragment extends Fragment {
 | 
		
	
		
			
			| 81 | 129 |  
 | 
		
	
		
			
			| 82 | 130 |          public void setFavourites(List<FavouriteStopDbo> favourites) {
 | 
		
	
		
			
			| 83 | 131 |              _favourites = favourites;
 | 
		
	
		
			
			|  | 132 | +            _schedules = null;
 | 
		
	
		
			
			|  | 133 | +            notifyDataSetChanged();
 | 
		
	
		
			
			|  | 134 | +        }
 | 
		
	
		
			
			|  | 135 | +
 | 
		
	
		
			
			|  | 136 | +        public List<FavouriteStopDbo> getFavourites() {
 | 
		
	
		
			
			|  | 137 | +            return _favourites;
 | 
		
	
		
			
			|  | 138 | +        }
 | 
		
	
		
			
			|  | 139 | +
 | 
		
	
		
			
			|  | 140 | +        public List<SchedulesDbo> getSchedules() {
 | 
		
	
		
			
			|  | 141 | +            return _schedules;
 | 
		
	
		
			
			|  | 142 | +        }
 | 
		
	
		
			
			|  | 143 | +
 | 
		
	
		
			
			|  | 144 | +        public void setSchedules(List<SchedulesDbo> schedules) {
 | 
		
	
		
			
			|  | 145 | +            _schedules = schedules;
 | 
		
	
		
			
			| 84 | 146 |              notifyDataSetChanged();
 | 
		
	
		
			
			| 85 | 147 |          }
 | 
		
	
		
			
			| 86 | 148 |      }
 | 
		
	
	
		
			
			|  | @@ -95,6 +157,13 @@ public class HomeFragment extends Fragment {
 | 
		
	
		
			
			| 95 | 157 |          _favouritesStopsList.setAdapter(_adapter);
 | 
		
	
		
			
			| 96 | 158 |          _favouritesStopsList.setLayoutManager(new LinearLayoutManager(getContext()));
 | 
		
	
		
			
			| 97 | 159 |  
 | 
		
	
		
			
			|  | 160 | +        _swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
 | 
		
	
		
			
			|  | 161 | +            @Override
 | 
		
	
		
			
			|  | 162 | +            public void onRefresh() {
 | 
		
	
		
			
			|  | 163 | +                loadSchedules();
 | 
		
	
		
			
			|  | 164 | +            }
 | 
		
	
		
			
			|  | 165 | +        });
 | 
		
	
		
			
			|  | 166 | +
 | 
		
	
		
			
			| 98 | 167 |          loadFavourites();
 | 
		
	
		
			
			| 99 | 168 |  
 | 
		
	
		
			
			| 100 | 169 |          return v;
 | 
		
	
	
		
			
			|  | @@ -104,6 +173,7 @@ public class HomeFragment extends Fragment {
 | 
		
	
		
			
			| 104 | 173 |      {
 | 
		
	
		
			
			| 105 | 174 |          List<FavouriteStopDbo> favourites = STSBusiness.getFavouriteStops(getContext());
 | 
		
	
		
			
			| 106 | 175 |          _adapter.setFavourites(favourites);
 | 
		
	
		
			
			|  | 176 | +        loadSchedules();
 | 
		
	
		
			
			| 107 | 177 |      }
 | 
		
	
		
			
			| 108 | 178 |  
 | 
		
	
		
			
			| 109 | 179 |      public void onFavouriteClicked(FavouriteStopDbo favouriteStopDbo)
 | 
		
	
	
		
			
			|  | @@ -121,12 +191,8 @@ public class HomeFragment extends Fragment {
 | 
		
	
		
			
			| 121 | 191 |                      @Override
 | 
		
	
		
			
			| 122 | 192 |                      public void onClick(DialogInterface dialog, int which) {
 | 
		
	
		
			
			| 123 | 193 |  
 | 
		
	
		
			
			| 124 |  | -                        List<FavouriteStopDbo> favouriteStopDbos = STSBusiness.getFavouriteStops(getContext());
 | 
		
	
		
			
			| 125 |  | -                        for (int i = 0; i < favouriteStopDbos.size(); ++i) {
 | 
		
	
		
			
			| 126 |  | -                            if (favouriteStopDbos.get(i).getStop().getId().equals(favouriteStopDbo.getStop().getId())) {
 | 
		
	
		
			
			| 127 |  | -                                favouriteStopDbos.remove(i--);
 | 
		
	
		
			
			| 128 |  | -                            }
 | 
		
	
		
			
			| 129 |  | -                        }
 | 
		
	
		
			
			|  | 194 | +                        List<FavouriteStopDbo> favouriteStopDbos = _adapter.getFavourites();
 | 
		
	
		
			
			|  | 195 | +                        favouriteStopDbos.remove(favouriteStopDbo);
 | 
		
	
		
			
			| 130 | 196 |                          STSBusiness.setFavouriteStops(getContext(), favouriteStopDbos);
 | 
		
	
		
			
			| 131 | 197 |  
 | 
		
	
		
			
			| 132 | 198 |                          Toast.makeText(getContext(), getString(R.string.favourite_removed, favouriteStopDbo.getStop().getFullName()), Toast.LENGTH_LONG).show();
 | 
		
	
	
		
			
			|  | @@ -137,4 +203,27 @@ public class HomeFragment extends Fragment {
 | 
		
	
		
			
			| 137 | 203 |                  .setNegativeButton(android.R.string.no, null)
 | 
		
	
		
			
			| 138 | 204 |                  .show();
 | 
		
	
		
			
			| 139 | 205 |      }
 | 
		
	
		
			
			|  | 206 | +
 | 
		
	
		
			
			|  | 207 | +    public void loadSchedules()
 | 
		
	
		
			
			|  | 208 | +    {
 | 
		
	
		
			
			|  | 209 | +        _swipeRefreshLayout.setRefreshing(true);
 | 
		
	
		
			
			|  | 210 | +        _adapter.setSchedules(null);
 | 
		
	
		
			
			|  | 211 | +        DateTimeZone dtz = DateTimeZone.forOffsetMillis(TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings());
 | 
		
	
		
			
			|  | 212 | +        LocalDateTime date = LocalDateTime.now(dtz).plusMinutes(-30);
 | 
		
	
		
			
			|  | 213 | +
 | 
		
	
		
			
			|  | 214 | +        SchedulesBusiness.getMultiple(STSBusiness.getConfig(), _adapter.getFavourites(), date, 10)
 | 
		
	
		
			
			|  | 215 | +                .then(new LuPromise.LuConsumer<List<SchedulesDbo>>() {
 | 
		
	
		
			
			|  | 216 | +                    @Override
 | 
		
	
		
			
			|  | 217 | +                    public void execute(List<SchedulesDbo> data) {
 | 
		
	
		
			
			|  | 218 | +                        _adapter.setSchedules(data);
 | 
		
	
		
			
			|  | 219 | +                        _swipeRefreshLayout.setRefreshing(false);
 | 
		
	
		
			
			|  | 220 | +                    }
 | 
		
	
		
			
			|  | 221 | +                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
 | 
		
	
		
			
			|  | 222 | +                    @Override
 | 
		
	
		
			
			|  | 223 | +                    public void execute(LuPromise.LuPromiseError data) {
 | 
		
	
		
			
			|  | 224 | +                        _swipeRefreshLayout.setRefreshing(false);
 | 
		
	
		
			
			|  | 225 | +                        //TODO
 | 
		
	
		
			
			|  | 226 | +                    }
 | 
		
	
		
			
			|  | 227 | +                });
 | 
		
	
		
			
			|  | 228 | +    }
 | 
		
	
		
			
			| 140 | 229 |  }
 |