Ejemplo n.º 1
0
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;

            if (view == null)
            {
                view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.RequestDetailsRow, parent, false);
                var name         = view.FindViewById <TextView>(Resource.Id.name);
                var femaleOnly   = view.FindViewById <TextView>(Resource.Id.female);
                var origin       = view.FindViewById <TextView>(Resource.Id.origin);
                var dest         = view.FindViewById <TextView>(Resource.Id.destination);
                var day          = view.FindViewById <TextView>(Resource.Id.day);
                var arriveTime   = view.FindViewById <TextView>(Resource.Id.time);
                var duration     = view.FindViewById <TextView>(Resource.Id.duration);
                var rate         = view.FindViewById <RatingBar>(Resource.Id.rating);
                var buttonAccept = view.FindViewById <Button>(Resource.Id.button_accept);
                var buttonReject = view.FindViewById <Button>(Resource.Id.button_reject);

                view.Tag = new RequestViewHolder()
                {
                    Name         = name,
                    Female       = femaleOnly,
                    Origin       = origin,
                    Dest         = dest,
                    Day          = day,
                    ArriveTime   = arriveTime,
                    Duration     = duration,
                    Rate         = rate,
                    ButtonAccept = buttonAccept,
                    ButtonReject = buttonReject
                };
            }

            var      holder        = (RequestViewHolder)view.Tag;
            var      time          = requests[position].TripPassenger.arriveTime.Split(':');
            int      totalInSecond = (int.Parse(time[0]) * 3600) + (int.Parse(time[1]) * 60);
            TimeSpan onTimeSet     = TimeSpan.FromSeconds(totalInSecond);
            string   strTime       = DateTime.ParseExact(onTimeSet.ToString(@"hh\:mm"), "HH:mm", null).ToString("hh:mm tt", CultureInfo.GetCultureInfo("en-US"));

            string strDay = requests[position].TripPassenger.days.Replace(",", " • ");

            if (requests[position].TripPassenger.femaleOnly.Equals("No"))
            {
                holder.Female.Visibility = ViewStates.Gone;
            }

            holder.Name.Text           = requests[position].TripPassenger.Member.username;
            holder.Origin.Text         = requests[position].TripPassenger.origin;
            holder.Dest.Text           = requests[position].TripPassenger.destination;
            holder.Day.Text            = strDay;
            holder.ArriveTime.Text     = strTime;
            holder.Duration.Text       = " • Approx " + requests[position].TripPassenger.durationStr;
            holder.Rate.NumStars       = (int)requests[position].TripPassenger.Member.rate;
            holder.ButtonAccept.Click += async(sender, e) =>
            {
                Android.Net.ConnectivityManager cm = (Android.Net.ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
                if (cm.ActiveNetworkInfo == null)
                {
                    Toast.MakeText(context, "Network error. Try again later.", ToastLength.Long).Show();
                }
                else
                {
                    if (requests[position].TripDriver.PassengerIDs != null)
                    {
                        string[] noOfPassenger = requests[position].TripDriver.PassengerIDs.Split(',');
                        if (noOfPassenger.Count() >= requests[position].TripDriver.availableSeat)
                        {
                            Toast.MakeText(context, "The available seat is full. ", ToastLength.Long).Show();
                        }
                        else
                        {
                            requests[position].status = "Accepted";
                            await RESTClient.AcceptRequestAsync(context, requests[position]);

                            view.Visibility = ViewStates.Gone;
                        }
                    }
                    else
                    {
                        requests[position].status = "Accepted";
                        await RESTClient.AcceptRequestAsync(context, requests[position]);

                        view.Visibility = ViewStates.Gone;
                    }
                }
            };

            holder.ButtonReject.Click += async(sender, e) =>
            {
                Android.Net.ConnectivityManager cm = (Android.Net.ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
                if (cm.ActiveNetworkInfo == null)
                {
                    Toast.MakeText(context, "Network error. Try again later.", ToastLength.Long).Show();
                }
                else
                {
                    requests[position].status = "Rejected";
                    await RESTClient.RejectRequestAsync(context, requests[position]);

                    view.Visibility = ViewStates.Gone;
                }
            };

            view.Click += (sender, e) =>
            {
                Intent intent = new Intent(context, typeof(RateListViewActivity));
                intent.AddFlags(ActivityFlags.ReorderToFront);
                intent.PutExtra("MemberID", requests[position].TripPassenger.Member.MemberID.ToString());
                context.StartActivity(intent);
            };

            return(view);
        }