|  | @@ -1,17 +1,27 @@
 | 
		
	
		
			
			| 1 | 1 |  package com.rthoni.stssaguenay.ui.fragments;
 | 
		
	
		
			
			| 2 | 2 |  
 | 
		
	
		
			
			|  | 3 | +import android.app.ProgressDialog;
 | 
		
	
		
			
			| 3 | 4 |  import android.os.Bundle;
 | 
		
	
		
			
			| 4 | 5 |  import android.support.annotation.Nullable;
 | 
		
	
		
			
			| 5 | 6 |  import android.support.v4.app.Fragment;
 | 
		
	
		
			
			|  | 7 | +import android.support.v7.widget.LinearLayoutManager;
 | 
		
	
		
			
			| 6 | 8 |  import android.support.v7.widget.RecyclerView;
 | 
		
	
		
			
			| 7 | 9 |  import android.view.LayoutInflater;
 | 
		
	
		
			
			| 8 | 10 |  import android.view.View;
 | 
		
	
		
			
			| 9 | 11 |  import android.view.ViewGroup;
 | 
		
	
		
			
			| 10 | 12 |  import android.widget.TextView;
 | 
		
	
		
			
			|  | 13 | +import android.widget.Toast;
 | 
		
	
		
			
			| 11 | 14 |  
 | 
		
	
		
			
			|  | 15 | +import com.luticate.utils.business.LuPromise;
 | 
		
	
		
			
			| 12 | 16 |  import com.rthoni.stssaguenay.R;
 | 
		
	
		
			
			|  | 17 | +import com.rthoni.stssaguenay.business.RoutesBusiness;
 | 
		
	
		
			
			|  | 18 | +import com.rthoni.stssaguenay.business.STSBusiness;
 | 
		
	
		
			
			|  | 19 | +import com.rthoni.stssaguenay.dbo.RoutesDbo;
 | 
		
	
		
			
			| 13 | 20 |  import com.rthoni.stssaguenay.dbo.StopsDbo;
 | 
		
	
		
			
			| 14 | 21 |  
 | 
		
	
		
			
			|  | 22 | +import java.util.List;
 | 
		
	
		
			
			|  | 23 | +import java.util.Vector;
 | 
		
	
		
			
			|  | 24 | +
 | 
		
	
		
			
			| 15 | 25 |  import butterknife.BindView;
 | 
		
	
		
			
			| 16 | 26 |  import butterknife.ButterKnife;
 | 
		
	
		
			
			| 17 | 27 |  
 | 
		
	
	
		
			
			|  | @@ -28,6 +38,51 @@ public class StopRoutesPickerFragment extends Fragment {
 | 
		
	
		
			
			| 28 | 38 |  
 | 
		
	
		
			
			| 29 | 39 |      private StopsDbo _stopsDbo;
 | 
		
	
		
			
			| 30 | 40 |  
 | 
		
	
		
			
			|  | 41 | +    private RoutesAdapter _routesAdapter;
 | 
		
	
		
			
			|  | 42 | +
 | 
		
	
		
			
			|  | 43 | +    public static class ViewHolder extends RecyclerView.ViewHolder {
 | 
		
	
		
			
			|  | 44 | +        public TextView _textView;
 | 
		
	
		
			
			|  | 45 | +        public ViewHolder(TextView v) {
 | 
		
	
		
			
			|  | 46 | +            super(v);
 | 
		
	
		
			
			|  | 47 | +            _textView = v;
 | 
		
	
		
			
			|  | 48 | +        }
 | 
		
	
		
			
			|  | 49 | +    }
 | 
		
	
		
			
			|  | 50 | +
 | 
		
	
		
			
			|  | 51 | +    public class RoutesAdapter extends RecyclerView.Adapter<ViewHolder> {
 | 
		
	
		
			
			|  | 52 | +
 | 
		
	
		
			
			|  | 53 | +        private List<RoutesDbo> _routes;
 | 
		
	
		
			
			|  | 54 | +
 | 
		
	
		
			
			|  | 55 | +        @Override
 | 
		
	
		
			
			|  | 56 | +        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 | 
		
	
		
			
			|  | 57 | +            View v = LayoutInflater.from(parent.getContext())
 | 
		
	
		
			
			|  | 58 | +                    .inflate(R.layout.simple_recycler_view_item, parent, false);
 | 
		
	
		
			
			|  | 59 | +            ViewHolder vh = new ViewHolder((TextView)v);
 | 
		
	
		
			
			|  | 60 | +            return vh;
 | 
		
	
		
			
			|  | 61 | +        }
 | 
		
	
		
			
			|  | 62 | +
 | 
		
	
		
			
			|  | 63 | +        @Override
 | 
		
	
		
			
			|  | 64 | +        public void onBindViewHolder(ViewHolder holder, int position) {
 | 
		
	
		
			
			|  | 65 | +            final RoutesDbo routeDbo = _routes.get(position);
 | 
		
	
		
			
			|  | 66 | +            holder._textView.setText(routeDbo.getFullName());
 | 
		
	
		
			
			|  | 67 | +            holder._textView.setOnClickListener(new View.OnClickListener() {
 | 
		
	
		
			
			|  | 68 | +                @Override
 | 
		
	
		
			
			|  | 69 | +                public void onClick(View v) {
 | 
		
	
		
			
			|  | 70 | +//                    onStopSelected(routeDbo);
 | 
		
	
		
			
			|  | 71 | +                }
 | 
		
	
		
			
			|  | 72 | +            });
 | 
		
	
		
			
			|  | 73 | +        }
 | 
		
	
		
			
			|  | 74 | +
 | 
		
	
		
			
			|  | 75 | +        @Override
 | 
		
	
		
			
			|  | 76 | +        public int getItemCount() {
 | 
		
	
		
			
			|  | 77 | +            return _routes == null ? 0 : _routes.size();
 | 
		
	
		
			
			|  | 78 | +        }
 | 
		
	
		
			
			|  | 79 | +
 | 
		
	
		
			
			|  | 80 | +        public void setRoutes(List<RoutesDbo> routes) {
 | 
		
	
		
			
			|  | 81 | +            _routes = routes;
 | 
		
	
		
			
			|  | 82 | +            notifyDataSetChanged();
 | 
		
	
		
			
			|  | 83 | +        }
 | 
		
	
		
			
			|  | 84 | +    }
 | 
		
	
		
			
			|  | 85 | +
 | 
		
	
		
			
			| 31 | 86 |      @Nullable
 | 
		
	
		
			
			| 32 | 87 |      @Override
 | 
		
	
		
			
			| 33 | 88 |      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 | 
		
	
	
		
			
			|  | @@ -36,6 +91,12 @@ public class StopRoutesPickerFragment extends Fragment {
 | 
		
	
		
			
			| 36 | 91 |  
 | 
		
	
		
			
			| 37 | 92 |          _textView.setText(getResources().getString(R.string.pick_routes, _stopsDbo.getFullName()));
 | 
		
	
		
			
			| 38 | 93 |  
 | 
		
	
		
			
			|  | 94 | +        _routesAdapter = new RoutesAdapter();
 | 
		
	
		
			
			|  | 95 | +        _listRoutes.setAdapter(_routesAdapter);
 | 
		
	
		
			
			|  | 96 | +        _listRoutes.setLayoutManager(new LinearLayoutManager(getContext()));
 | 
		
	
		
			
			|  | 97 | +
 | 
		
	
		
			
			|  | 98 | +        loadRoutes();
 | 
		
	
		
			
			|  | 99 | +
 | 
		
	
		
			
			| 39 | 100 |          return v;
 | 
		
	
		
			
			| 40 | 101 |      }
 | 
		
	
		
			
			| 41 | 102 |  
 | 
		
	
	
		
			
			|  | @@ -46,4 +107,28 @@ public class StopRoutesPickerFragment extends Fragment {
 | 
		
	
		
			
			| 46 | 107 |      public void setStopsDbo(StopsDbo stopsDbo) {
 | 
		
	
		
			
			| 47 | 108 |          _stopsDbo = stopsDbo;
 | 
		
	
		
			
			| 48 | 109 |      }
 | 
		
	
		
			
			|  | 110 | +
 | 
		
	
		
			
			|  | 111 | +    public void loadRoutes()
 | 
		
	
		
			
			|  | 112 | +    {
 | 
		
	
		
			
			|  | 113 | +        final ProgressDialog progressDialog = new ProgressDialog(getContext());
 | 
		
	
		
			
			|  | 114 | +        progressDialog.setIndeterminate(true);
 | 
		
	
		
			
			|  | 115 | +        progressDialog.show();
 | 
		
	
		
			
			|  | 116 | +
 | 
		
	
		
			
			|  | 117 | +        RoutesBusiness.getAll(STSBusiness.getConfig())
 | 
		
	
		
			
			|  | 118 | +                .then(new LuPromise.LuConsumer<List<RoutesDbo>>() {
 | 
		
	
		
			
			|  | 119 | +                    @Override
 | 
		
	
		
			
			|  | 120 | +                    public void execute(List<RoutesDbo> data) {
 | 
		
	
		
			
			|  | 121 | +                        progressDialog.dismiss();
 | 
		
	
		
			
			|  | 122 | +                        List<RoutesDbo> routes = RoutesBusiness.getRoutesDbos(data, _stopsDbo.getRoutes());
 | 
		
	
		
			
			|  | 123 | +                        _routesAdapter.setRoutes(routes);
 | 
		
	
		
			
			|  | 124 | +                    }
 | 
		
	
		
			
			|  | 125 | +                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
 | 
		
	
		
			
			|  | 126 | +                    @Override
 | 
		
	
		
			
			|  | 127 | +                    public void execute(LuPromise.LuPromiseError data) {
 | 
		
	
		
			
			|  | 128 | +                        progressDialog.dismiss();
 | 
		
	
		
			
			|  | 129 | +                        _routesAdapter.setRoutes(new Vector<RoutesDbo>());
 | 
		
	
		
			
			|  | 130 | +                        Toast.makeText(getContext(), data.getError(), Toast.LENGTH_LONG).show();
 | 
		
	
		
			
			|  | 131 | +                    }
 | 
		
	
		
			
			|  | 132 | +                });
 | 
		
	
		
			
			|  | 133 | +    }
 | 
		
	
		
			
			| 49 | 134 |  }
 |