Example #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.WelcomeHome);

            homesView = FindViewById <ListView>(Resource.Id.homeListView);

            string retriedData     = Intent.GetStringExtra("HomeDataService");
            var    homeDateService = JsonConvert.DeserializeObject <HomeDataService>(retriedData);

            var homeRepository = homeDateService.GetHomeRepository();

            this.homes = homeRepository.GetHome();

            HomeListAdapter homeListAdapter = new HomeListAdapter(this, this.homes);

            homesView.Adapter = homeListAdapter;

            homesView.ItemClick += HomeListView_ItemClick;
        }
Example #2
0
        private async void LoadData(View v, Controller ctrl)
        {
            play  = v.FindViewById <Button>(Resource.Id.Home_Play);
            pause = v.FindViewById <Button>(Resource.Id.Home_Pause);
            var taskComplete   = v.FindViewById <CheckBox>(Resource.Id.Home_TaskComplete);
            var recentTask     = v.FindViewById <Button>(Resource.Id.Home_RecentTask);
            var recentProject  = v.FindViewById <Button>(Resource.Id.Home_CurrentProject);
            var expandableList = v.FindViewById <ExpandableListView>(Android.Resource.Id.List);


            var pb = new ProgressDialog(_mActivity)
            {
                Indeterminate = true
            };

            pb.SetTitle("Loading");

            //TODO: This is under the assumption that whatever task is on the top of the recent task list
            // is the currently logged timer task. Check whether this assumption is valid or not.

            if (TimeLoggingController.GetInstance().IsTimerRunning())
            {
                ModifyPlayPauseState(true);
            }

            play.Click += (sender, args) =>
            {
                Debug.WriteLine("Play Clicked");

                //var timerServiceIntent = new Intent("com.tumasolutions.processdashboard.TimerService");
                //var timerServiceConnection = new TimerServiceConnection((MainActivity)this.Activity);
                //Activity.ApplicationContext.BindService(timerServiceIntent, timerServiceConnection, Bind.AutoCreate);
                if (TimeLoggingController.GetInstance().WasNetworkAvailable)
                {
                    Intent intent = new Intent(Activity, typeof(TimerService));
                    intent.PutExtra("taskId", _currenttaskid);
                    Activity.StartService(intent);
                }
                else
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
                    builder.SetTitle("Previous changes not saved")
                    .SetMessage("An earlier time log entry has not yet been synchronized with the server. Please try again later.")
                    .SetNeutralButton("Okay", (sender2, args2) =>
                    {
                        builder.Dispose();
                    })
                    .SetCancelable(false);
                    AlertDialog alert = builder.Create();
                    alert.Show();
                }
            };

            pause.Click += (sender, args) =>
            {
                Debug.WriteLine("Pause Clicked");
                Activity.StopService(new Intent(Activity, typeof(TimerService)));
                Toast.MakeText(this.Activity, "Time Log Entry Saved", ToastLength.Short).Show();
            };


            taskComplete.CheckedChange += (sender, args) =>
            {
                string text = "";
                if (args.IsChecked)
                {
                    // Mark a task as complete
                    DateTime convertedTime = Util.GetInstance().GetServerTime(DateTime.UtcNow);
                    //taskDetail.CompletionDate = convertedTime;

                    try
                    {
                        ((MainActivity)(Activity)).Ctrl.UpdateATask(AccountStorage.DataSet,
                                                                    _currenttaskid, null, convertedTime, false);
                        text = "Task Marked Complete";
                    }
                    catch (CannotReachServerException)
                    {
                        if (pb.IsShowing)
                        {
                            pb.Dismiss();
                        }
                        Debug.WriteLine("We could not reach the server");
                        taskComplete.Checked = false;
                        text = "Please check your internet connection and try again.";
                        Toast.MakeText(this.Activity, text, ToastLength.Short).Show();
                    }
                    catch (StatusNotOkayException)
                    {
                        pb.Dismiss();
                        taskComplete.Checked = false;
                        //TODO: Should we report this ?
                        text = "An error has occured. Please try again.";
                        Toast.MakeText(this.Activity, text, ToastLength.Short).Show();
                    }
                    catch (Exception e)
                    {
                        // For any other weird exceptions
                        pb.Dismiss();

                        taskComplete.Checked = false;
                        // Sending to HockeyApp
                        ExceptionHandler.SaveException(Java.Lang.Throwable.FromException(e), null, null);
                        text = "Unable to make the change. Please try again.";
                        Toast.MakeText(this.Activity, text, ToastLength.Short).Show();
                    }
                }
                else
                {
                    // Unmark the task

                    try
                    {
                        ((MainActivity)(Activity)).Ctrl.UpdateATask(AccountStorage.DataSet,
                                                                    _currenttaskid, null, null, true);
                        text = "Task Marked Incomplete";
                    }
                    catch (CannotReachServerException)
                    {
                        if (pb.IsShowing)
                        {
                            pb.Dismiss();
                        }
                        Debug.WriteLine("We could not reach the server");
                        taskComplete.Checked = true;
                        text = "Please check your internet connection and try again.";
                        Toast.MakeText(this.Activity, text, ToastLength.Short).Show();
                    }
                    //TODO: Should we handle this ?
                    catch (StatusNotOkayException)
                    {
                        pb.Dismiss();
                        taskComplete.Checked = true;
                        text = "An error has occured. Please try again.";
                        Toast.MakeText(this.Activity, text, ToastLength.Short).Show();
                    }
                    catch (Exception e)
                    {
                        // For any other weird exceptions
                        taskComplete.Checked = true;
                        ExceptionHandler.SaveException(Java.Lang.Throwable.FromException(e), null, null);
                        text = "Unable to make the change. Please try again.";
                        Toast.MakeText(this.Activity, text, ToastLength.Short).Show();
                    }
                }
                Debug.WriteLine("We have changed content ");
                Toast.MakeText(Activity, text, ToastLength.Short).Show();
            };
            recentTask.Text = "Loading..";

            recentProject.Text = "Loading..";

            pb.SetCanceledOnTouchOutside(false);
            pb.Show();

            try
            {
                var output = await ctrl.GetRecentTasks(AccountStorage.DataSet);

                pb.Dismiss();

                var recent = output[0];

                recentTask.Text    = recent.FullName;
                recentProject.Text = recent.Project.Name;
                _currenttaskid     = recent.Id;

                _headings.Add(recent.Project.Name, new List <Task>());

                output.RemoveAt(0);

                foreach (Task t in output)
                {
                    if (_headings.ContainsKey(t.Project.Name))
                    {
                        List <Task> tt = _headings[t.Project.Name];
                        tt.Add(t);
                        _headings[t.Project.Name] = tt;
                    }
                    else
                    {
                        List <Task> tt = new List <Task>();
                        tt.Add(t);
                        _headings[t.Project.Name] = tt;
                    }
                }

                //var listAdapter = new TaskAdapter(_mActivity, Android.Resource.Layout.SimpleListItem1, output.ToArray());
                var listAdapter = new HomeListAdapter(this.Activity, _headings);
                expandableList.SetAdapter(listAdapter);
                //ListAdapter = listAdapter;

                var refresher = v.FindViewById <SwipeRefreshLayout>(Resource.Id.refresher);

                refresher.Refresh += async delegate
                {
                    try
                    {
                        output = await ctrl.GetRecentTasks(AccountStorage.DataSet);

                        recent             = output[0];
                        _headings          = new Dictionary <string, List <Task> >();
                        recentTask.Text    = recent.FullName;
                        recentProject.Text = recent.Project.Name;
                        _headings.Add(recent.Project.Name, new List <Task>());

                        output.RemoveAt(0);
                        foreach (Task t in output)
                        {
                            if (_headings.ContainsKey(t.Project.Name))
                            {
                                List <Task> tt = _headings[t.Project.Name];
                                tt.Add(t);
                                _headings[t.Project.Name] = tt;
                            }
                            else
                            {
                                List <Task> tt = new List <Task>();
                                tt.Add(t);
                                _headings[t.Project.Name] = tt;
                            }
                        }
                        listAdapter = new HomeListAdapter(this.Activity, _headings);
                        expandableList.SetAdapter(listAdapter);

                        refresher.Refreshing = false;
                    }
                    catch (CannotReachServerException)
                    {
                        AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
                        builder.SetTitle("Unable to Connect")
                        .SetMessage("Please check your network connection and try again")
                        .SetNeutralButton("Okay", (sender, args) =>
                        {
                            builder.Dispose();
                        })
                        .SetCancelable(false);
                        AlertDialog alert = builder.Create();
                        refresher.Refreshing = false;
                        alert.Show();
                    }
                    catch (StatusNotOkayException se)
                    {
                        AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
                        builder.SetTitle("An Error has occured")
                        .SetMessage("Error :" + se.GetMessage())
                        .SetNeutralButton("Okay", (sender, args) =>
                        {
                            builder.Dispose();
                        })
                        .SetCancelable(false);
                        AlertDialog alert = builder.Create();
                        refresher.Refreshing = false;
                        alert.Show();
                    }
                    catch (Exception e)
                    {
                        // For any other weird exceptions
                        AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
                        builder.SetTitle("An Error has occured")
                        .SetNeutralButton("Okay", (sender, args) =>
                        {
                            builder.Dispose();
                        })
                        .SetMessage("Error :" + e.Message)
                        .SetCancelable(false);
                        AlertDialog alert = builder.Create();
                        refresher.Refreshing = false;
                        alert.Show();
                    }
                };

                recentProject.Click += (sender, args) =>
                {
                    ((MainActivity)Activity).ListOfProjectsCallback(recent.Project.Id, recent.Project.Name);
                };

                recentTask.Click += (sender, args) =>
                {
                    ((MainActivity)Activity).PassTaskDetailsInfo(recent.Id, recent.FullName, recent.Project.Name,
                                                                 recent.CompletionDate,
                                                                 recent.EstimatedTime, recent.ActualTime);
                };
            }
            catch (CannotReachServerException)
            {
                pb.Dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
                builder.SetTitle("Unable to Connect")
                .SetMessage("Please check your network connection and try again")
                .SetNeutralButton("Okay", (sender, args) =>
                {
                    builder.Dispose();
                })
                .SetCancelable(false);
                AlertDialog alert = builder.Create();
                alert.Show();
            }
            catch (StatusNotOkayException se)
            {
                pb.Dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
                builder.SetTitle("An Error has occured")
                .SetMessage("Error :" + se.GetMessage())
                .SetNeutralButton("Okay", (sender, args) =>
                {
                    builder.Dispose();
                })
                .SetCancelable(false);
                AlertDialog alert = builder.Create();
                alert.Show();
            }
            catch (WebException we)
            {
                if (we.Status == WebExceptionStatus.ProtocolError)
                {
                    var response = we.Response as HttpWebResponse;
                    if (response != null)
                    {
                        Console.WriteLine("HTTP Status Code: " + (int)response.StatusCode);
                        if (response.StatusCode == HttpStatusCode.Forbidden)
                        {
                            try
                            {
                                Toast.MakeText(this.Activity, "Username and password error.", ToastLength.Long).Show();
                                System.Diagnostics.Debug.WriteLine("We are about to logout");
                                AccountStorage.ClearStorage();
                                System.Diagnostics.Debug.WriteLine("Main Activity is :" + Activity == null);
                                System.Diagnostics.Debug.WriteLine("Items in the backstack :" + Activity.FragmentManager.BackStackEntryCount);
                                System.Diagnostics.Debug.WriteLine("Main Activity is :" + Activity == null);
                                Activity.FragmentManager.PopBackStack(null, PopBackStackFlags.Inclusive);
                                System.Diagnostics.Debug.WriteLine("Items in the backstack 2 :" + Activity.FragmentManager.BackStackEntryCount);
                                ((MainActivity)(Activity)).SetDrawerState(false);
                                ((MainActivity)(Activity)).SwitchToFragment(MainActivity.FragmentTypes.Login);
                            }
                            catch (System.Exception e)
                            {
                                System.Diagnostics.Debug.WriteLine("We encountered an error :" + e.Message);
                            }
                        }
                    }
                    else
                    {
                        // no http status code available
                        Toast.MakeText(Activity, "Unable to load the data. Please restart the application.", ToastLength.Short).Show();
                    }
                }
                else
                {
                    // no http status code available
                    Toast.MakeText(Activity, "Unable to load the data. Please restart the application.", ToastLength.Short).Show();
                }
            }
            catch (Exception e)
            {
                // For any other weird exceptions
                pb.Dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
                builder.SetTitle("An Error has occured")
                .SetNeutralButton("Okay", (sender, args) =>
                {
                    builder.Dispose();
                })
                .SetMessage("Error :" + e.Message)
                .SetCancelable(false);
                AlertDialog alert = builder.Create();
                alert.Show();
            }

            /*
             *  private void LoadDummyData(View v)
             *  {
             *
             *      ListView lv = v.FindViewById<ListView>(Android.Resource.Id.List);
             *      var items = new[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };
             *      ArrayAdapter listAdapter = new ArrayAdapter<String>(_mActivity, Android.Resource.Layout.SimpleListItem1, items);
             *      ListAdapter = listAdapter;
             *
             *      TextView recentTask = v.FindViewById<TextView>(Resource.Id.Home_RecentTask);
             *      recentTask.Text = "Project / Mobile App l1 / Iteration 1 / View Skeletons / Create Android Skeletons / Home Screen ";
             *
             *  }
             */
        }