|  | @@ -1,15 +1,36 @@
 | 
		
	
		
			
			| 1 | 1 |  package com.rthoni.stssaguenay.ui.fragments;
 | 
		
	
		
			
			| 2 | 2 |  
 | 
		
	
		
			
			|  | 3 | +import android.app.ProgressDialog;
 | 
		
	
		
			
			|  | 4 | +import android.graphics.Color;
 | 
		
	
		
			
			| 3 | 5 |  import android.os.Bundle;
 | 
		
	
		
			
			| 4 | 6 |  import android.support.annotation.Nullable;
 | 
		
	
		
			
			| 5 | 7 |  import android.support.v4.app.Fragment;
 | 
		
	
		
			
			|  | 8 | +import android.support.v7.widget.LinearLayoutManager;
 | 
		
	
		
			
			|  | 9 | +import android.support.v7.widget.RecyclerView;
 | 
		
	
		
			
			| 6 | 10 |  import android.view.LayoutInflater;
 | 
		
	
		
			
			| 7 | 11 |  import android.view.View;
 | 
		
	
		
			
			| 8 | 12 |  import android.view.ViewGroup;
 | 
		
	
		
			
			|  | 13 | +import android.widget.ImageView;
 | 
		
	
		
			
			| 9 | 14 |  import android.widget.TextView;
 | 
		
	
		
			
			|  | 15 | +import android.widget.Toast;
 | 
		
	
		
			
			| 10 | 16 |  
 | 
		
	
		
			
			|  | 17 | +import com.amulyakhare.textdrawable.TextDrawable;
 | 
		
	
		
			
			|  | 18 | +import com.luticate.utils.business.LuPromise;
 | 
		
	
		
			
			| 11 | 19 |  import com.rthoni.stssaguenay.R;
 | 
		
	
		
			
			|  | 20 | +import com.rthoni.stssaguenay.business.RoutesBusiness;
 | 
		
	
		
			
			|  | 21 | +import com.rthoni.stssaguenay.business.STSBusiness;
 | 
		
	
		
			
			|  | 22 | +import com.rthoni.stssaguenay.business.SchedulesBusiness;
 | 
		
	
		
			
			|  | 23 | +import com.rthoni.stssaguenay.dbo.RoutesDbo;
 | 
		
	
		
			
			|  | 24 | +import com.rthoni.stssaguenay.dbo.SchedulesDbo;
 | 
		
	
		
			
			| 12 | 25 |  import com.rthoni.stssaguenay.dbo.StopsDbo;
 | 
		
	
		
			
			|  | 26 | +import com.rthoni.stssaguenay.dbo.UserFavouriteStopsDbo;
 | 
		
	
		
			
			|  | 27 | +
 | 
		
	
		
			
			|  | 28 | +import org.joda.time.DateTimeZone;
 | 
		
	
		
			
			|  | 29 | +import org.joda.time.LocalDateTime;
 | 
		
	
		
			
			|  | 30 | +
 | 
		
	
		
			
			|  | 31 | +import java.util.List;
 | 
		
	
		
			
			|  | 32 | +import java.util.TimeZone;
 | 
		
	
		
			
			|  | 33 | +import java.util.Vector;
 | 
		
	
		
			
			| 13 | 34 |  
 | 
		
	
		
			
			| 14 | 35 |  import butterknife.BindView;
 | 
		
	
		
			
			| 15 | 36 |  import butterknife.ButterKnife;
 | 
		
	
	
		
			
			|  | @@ -23,8 +44,97 @@ public class StopViewerFragment extends Fragment {
 | 
		
	
		
			
			| 23 | 44 |      @BindView(R.id.textView)
 | 
		
	
		
			
			| 24 | 45 |      TextView _textStop;
 | 
		
	
		
			
			| 25 | 46 |  
 | 
		
	
		
			
			|  | 47 | +    @BindView(R.id.listRoutes)
 | 
		
	
		
			
			|  | 48 | +    RecyclerView _listRoutes;
 | 
		
	
		
			
			|  | 49 | +
 | 
		
	
		
			
			| 26 | 50 |      private StopsDbo _stopsDbo;
 | 
		
	
		
			
			| 27 | 51 |  
 | 
		
	
		
			
			|  | 52 | +    private RoutesAdapter _adapter;
 | 
		
	
		
			
			|  | 53 | +
 | 
		
	
		
			
			|  | 54 | +    public static class ViewHolder extends RecyclerView.ViewHolder {
 | 
		
	
		
			
			|  | 55 | +        public ImageView _imageView;
 | 
		
	
		
			
			|  | 56 | +        public TextView _textView;
 | 
		
	
		
			
			|  | 57 | +        public TextView _textView2;
 | 
		
	
		
			
			|  | 58 | +        public View _view;
 | 
		
	
		
			
			|  | 59 | +        public ViewHolder(View v) {
 | 
		
	
		
			
			|  | 60 | +            super(v);
 | 
		
	
		
			
			|  | 61 | +            _view = v;
 | 
		
	
		
			
			|  | 62 | +        }
 | 
		
	
		
			
			|  | 63 | +    }
 | 
		
	
		
			
			|  | 64 | +
 | 
		
	
		
			
			|  | 65 | +    public class RoutesAdapter extends RecyclerView.Adapter<ViewHolder> {
 | 
		
	
		
			
			|  | 66 | +
 | 
		
	
		
			
			|  | 67 | +        private List<RoutesDbo> _routes;
 | 
		
	
		
			
			|  | 68 | +
 | 
		
	
		
			
			|  | 69 | +        private List<SchedulesDbo> _schedules;
 | 
		
	
		
			
			|  | 70 | +
 | 
		
	
		
			
			|  | 71 | +        @Override
 | 
		
	
		
			
			|  | 72 | +        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 | 
		
	
		
			
			|  | 73 | +            View v = LayoutInflater.from(parent.getContext())
 | 
		
	
		
			
			|  | 74 | +                    .inflate(R.layout.route_schedules_recycler_view_item, parent, false);
 | 
		
	
		
			
			|  | 75 | +            ViewHolder vh = new ViewHolder(v);
 | 
		
	
		
			
			|  | 76 | +            vh._imageView = (ImageView) v.findViewById(R.id.imageView);
 | 
		
	
		
			
			|  | 77 | +            vh._textView = (TextView) v.findViewById(R.id.textView);
 | 
		
	
		
			
			|  | 78 | +            vh._textView2 = (TextView) v.findViewById(R.id.textView2);
 | 
		
	
		
			
			|  | 79 | +            return vh;
 | 
		
	
		
			
			|  | 80 | +        }
 | 
		
	
		
			
			|  | 81 | +
 | 
		
	
		
			
			|  | 82 | +        @Override
 | 
		
	
		
			
			|  | 83 | +        public void onBindViewHolder(ViewHolder holder, int position) {
 | 
		
	
		
			
			|  | 84 | +            final RoutesDbo routeDbo = _routes.get(position);
 | 
		
	
		
			
			|  | 85 | +            TextDrawable drawable = TextDrawable.builder()
 | 
		
	
		
			
			|  | 86 | +                    .beginConfig()
 | 
		
	
		
			
			|  | 87 | +                    .textColor(Color.parseColor("#" + routeDbo.getFgColor()))
 | 
		
	
		
			
			|  | 88 | +                    .endConfig()
 | 
		
	
		
			
			|  | 89 | +                    .buildRound(routeDbo.getId(), Color.parseColor("#" + routeDbo.getBgColor()));
 | 
		
	
		
			
			|  | 90 | +            holder._imageView.setImageDrawable(drawable);
 | 
		
	
		
			
			|  | 91 | +            holder._textView.setText(routeDbo.getName());
 | 
		
	
		
			
			|  | 92 | +            SchedulesDbo schedules = null;
 | 
		
	
		
			
			|  | 93 | +            if (_schedules != null) {
 | 
		
	
		
			
			|  | 94 | +                for (SchedulesDbo schedulesDbo : _schedules) {
 | 
		
	
		
			
			|  | 95 | +                    if (schedulesDbo.getRouteId().equals(routeDbo.getId())) {
 | 
		
	
		
			
			|  | 96 | +                        schedules = schedulesDbo;
 | 
		
	
		
			
			|  | 97 | +                        break;
 | 
		
	
		
			
			|  | 98 | +                    }
 | 
		
	
		
			
			|  | 99 | +                }
 | 
		
	
		
			
			|  | 100 | +            }
 | 
		
	
		
			
			|  | 101 | +            if (schedules != null) {
 | 
		
	
		
			
			|  | 102 | +                StringBuilder text = new StringBuilder();
 | 
		
	
		
			
			|  | 103 | +
 | 
		
	
		
			
			|  | 104 | +                for (int i = 0; i < schedules.getSchedules().size(); ++i) {
 | 
		
	
		
			
			|  | 105 | +                    if (i > 0) {
 | 
		
	
		
			
			|  | 106 | +                        text.append(", ");
 | 
		
	
		
			
			|  | 107 | +                    }
 | 
		
	
		
			
			|  | 108 | +                    text.append(schedules.getSchedules().get(i).toString("HH:mm"));
 | 
		
	
		
			
			|  | 109 | +                }
 | 
		
	
		
			
			|  | 110 | +
 | 
		
	
		
			
			|  | 111 | +                holder._textView2.setText(text.toString());
 | 
		
	
		
			
			|  | 112 | +            }
 | 
		
	
		
			
			|  | 113 | +            else {
 | 
		
	
		
			
			|  | 114 | +                holder._textView2.setText("");
 | 
		
	
		
			
			|  | 115 | +            }
 | 
		
	
		
			
			|  | 116 | +        }
 | 
		
	
		
			
			|  | 117 | +
 | 
		
	
		
			
			|  | 118 | +        @Override
 | 
		
	
		
			
			|  | 119 | +        public int getItemCount() {
 | 
		
	
		
			
			|  | 120 | +            return _routes == null ? 0 : _routes.size();
 | 
		
	
		
			
			|  | 121 | +        }
 | 
		
	
		
			
			|  | 122 | +
 | 
		
	
		
			
			|  | 123 | +        public void setRoutes(List<RoutesDbo> routes) {
 | 
		
	
		
			
			|  | 124 | +            _routes = routes;
 | 
		
	
		
			
			|  | 125 | +            notifyDataSetChanged();
 | 
		
	
		
			
			|  | 126 | +        }
 | 
		
	
		
			
			|  | 127 | +
 | 
		
	
		
			
			|  | 128 | +        public List<RoutesDbo> getRoutes() {
 | 
		
	
		
			
			|  | 129 | +            return _routes;
 | 
		
	
		
			
			|  | 130 | +        }
 | 
		
	
		
			
			|  | 131 | +
 | 
		
	
		
			
			|  | 132 | +        public void setSchedules(List<SchedulesDbo> schedules) {
 | 
		
	
		
			
			|  | 133 | +            _schedules = schedules;
 | 
		
	
		
			
			|  | 134 | +            notifyDataSetChanged();
 | 
		
	
		
			
			|  | 135 | +        }
 | 
		
	
		
			
			|  | 136 | +    }
 | 
		
	
		
			
			|  | 137 | +
 | 
		
	
		
			
			| 28 | 138 |      @Nullable
 | 
		
	
		
			
			| 29 | 139 |      @Override
 | 
		
	
		
			
			| 30 | 140 |      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 | 
		
	
	
		
			
			|  | @@ -33,9 +143,79 @@ public class StopViewerFragment extends Fragment {
 | 
		
	
		
			
			| 33 | 143 |  
 | 
		
	
		
			
			| 34 | 144 |          _textStop.setText(_stopsDbo.getFullName());
 | 
		
	
		
			
			| 35 | 145 |  
 | 
		
	
		
			
			|  | 146 | +        _adapter = new RoutesAdapter();
 | 
		
	
		
			
			|  | 147 | +        _listRoutes.setAdapter(_adapter);
 | 
		
	
		
			
			|  | 148 | +        _listRoutes.setLayoutManager(new LinearLayoutManager(getContext()));
 | 
		
	
		
			
			|  | 149 | +
 | 
		
	
		
			
			|  | 150 | +        loadRoutesAndSchedules();
 | 
		
	
		
			
			|  | 151 | +
 | 
		
	
		
			
			| 36 | 152 |          return v;
 | 
		
	
		
			
			| 37 | 153 |      }
 | 
		
	
		
			
			| 38 | 154 |  
 | 
		
	
		
			
			|  | 155 | +    public void loadRoutesAndSchedules()
 | 
		
	
		
			
			|  | 156 | +    {
 | 
		
	
		
			
			|  | 157 | +        final ProgressDialog progressDialog = new ProgressDialog(getContext());
 | 
		
	
		
			
			|  | 158 | +        progressDialog.setIndeterminate(true);
 | 
		
	
		
			
			|  | 159 | +        progressDialog.setCancelable(false);
 | 
		
	
		
			
			|  | 160 | +        progressDialog.show();
 | 
		
	
		
			
			|  | 161 | +        progressDialog.setTitle(R.string.loading);
 | 
		
	
		
			
			|  | 162 | +        progressDialog.setMessage(getString(R.string.loading_routes));
 | 
		
	
		
			
			|  | 163 | +
 | 
		
	
		
			
			|  | 164 | +        RoutesBusiness.getAll(STSBusiness.getConfig(getContext()))
 | 
		
	
		
			
			|  | 165 | +                .then(new LuPromise.LuConsumer<List<RoutesDbo>>() {
 | 
		
	
		
			
			|  | 166 | +                    @Override
 | 
		
	
		
			
			|  | 167 | +                    public void execute(List<RoutesDbo> data) {
 | 
		
	
		
			
			|  | 168 | +                        progressDialog.dismiss();
 | 
		
	
		
			
			|  | 169 | +                        List<RoutesDbo> routes = RoutesBusiness.getRoutesDbos(data, _stopsDbo.getRoutes());
 | 
		
	
		
			
			|  | 170 | +                        _adapter.setRoutes(routes);
 | 
		
	
		
			
			|  | 171 | +                        loadSchedules();
 | 
		
	
		
			
			|  | 172 | +                    }
 | 
		
	
		
			
			|  | 173 | +                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
 | 
		
	
		
			
			|  | 174 | +                    @Override
 | 
		
	
		
			
			|  | 175 | +                    public void execute(LuPromise.LuPromiseError data) {
 | 
		
	
		
			
			|  | 176 | +                        progressDialog.dismiss();
 | 
		
	
		
			
			|  | 177 | +                        _adapter.setRoutes(new Vector<RoutesDbo>());
 | 
		
	
		
			
			|  | 178 | +                        Toast.makeText(getContext(), data.getError(), Toast.LENGTH_LONG).show();
 | 
		
	
		
			
			|  | 179 | +                    }
 | 
		
	
		
			
			|  | 180 | +                });
 | 
		
	
		
			
			|  | 181 | +    }
 | 
		
	
		
			
			|  | 182 | +
 | 
		
	
		
			
			|  | 183 | +    public void loadSchedules()
 | 
		
	
		
			
			|  | 184 | +    {
 | 
		
	
		
			
			|  | 185 | +        final ProgressDialog progressDialog = new ProgressDialog(getContext());
 | 
		
	
		
			
			|  | 186 | +        progressDialog.setIndeterminate(true);
 | 
		
	
		
			
			|  | 187 | +        progressDialog.setCancelable(false);
 | 
		
	
		
			
			|  | 188 | +        progressDialog.show();
 | 
		
	
		
			
			|  | 189 | +        progressDialog.setTitle(R.string.loading);
 | 
		
	
		
			
			|  | 190 | +        progressDialog.setMessage(getString(R.string.loading_schedules));
 | 
		
	
		
			
			|  | 191 | +
 | 
		
	
		
			
			|  | 192 | +        UserFavouriteStopsDbo favouriteStopsDbo = new UserFavouriteStopsDbo();
 | 
		
	
		
			
			|  | 193 | +        favouriteStopsDbo.setStop(_stopsDbo);
 | 
		
	
		
			
			|  | 194 | +        favouriteStopsDbo.setRoutes(_adapter.getRoutes());
 | 
		
	
		
			
			|  | 195 | +
 | 
		
	
		
			
			|  | 196 | +        List<UserFavouriteStopsDbo> favouriteStopsDbos = new Vector<>();
 | 
		
	
		
			
			|  | 197 | +        favouriteStopsDbos.add(favouriteStopsDbo);
 | 
		
	
		
			
			|  | 198 | +
 | 
		
	
		
			
			|  | 199 | +        DateTimeZone dtz = DateTimeZone.forOffsetMillis(TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings());
 | 
		
	
		
			
			|  | 200 | +        LocalDateTime date = LocalDateTime.now(dtz).plusMinutes(-30);
 | 
		
	
		
			
			|  | 201 | +
 | 
		
	
		
			
			|  | 202 | +        SchedulesBusiness.getMultiple(STSBusiness.getConfig(getContext()), favouriteStopsDbos, date, 20)
 | 
		
	
		
			
			|  | 203 | +                .then(new LuPromise.LuConsumer<List<SchedulesDbo>>() {
 | 
		
	
		
			
			|  | 204 | +                    @Override
 | 
		
	
		
			
			|  | 205 | +                    public void execute(List<SchedulesDbo> data) {
 | 
		
	
		
			
			|  | 206 | +                        progressDialog.dismiss();
 | 
		
	
		
			
			|  | 207 | +                        _adapter.setSchedules(data);
 | 
		
	
		
			
			|  | 208 | +                    }
 | 
		
	
		
			
			|  | 209 | +                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
 | 
		
	
		
			
			|  | 210 | +                    @Override
 | 
		
	
		
			
			|  | 211 | +                    public void execute(LuPromise.LuPromiseError data) {
 | 
		
	
		
			
			|  | 212 | +                        progressDialog.dismiss();
 | 
		
	
		
			
			|  | 213 | +//                        _routesAdapter.setRoutes(new Vector<RoutesDbo>()); TODO
 | 
		
	
		
			
			|  | 214 | +                        Toast.makeText(getContext(), data.getError(), Toast.LENGTH_LONG).show();
 | 
		
	
		
			
			|  | 215 | +                    }
 | 
		
	
		
			
			|  | 216 | +                });
 | 
		
	
		
			
			|  | 217 | +    }
 | 
		
	
		
			
			|  | 218 | +
 | 
		
	
		
			
			| 39 | 219 |      public void setStopsDbo(StopsDbo stopsDbo) {
 | 
		
	
		
			
			| 40 | 220 |          _stopsDbo = stopsDbo;
 | 
		
	
		
			
			| 41 | 221 |      }
 |