Exemple #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            string dbPath = System.IO.Path.Combine(
                System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
                "localkimaidata.db3");

            db = new SQLiteConnection(dbPath);
            // Fetch App Prefs
            Context mContext = Application.Context;

            ap = new AppPreferences(mContext);

            if (string.IsNullOrEmpty(ap.getAccessKey("URL")) ||
                string.IsNullOrEmpty(ap.getAccessKey("APIKEY")) ||
                string.IsNullOrEmpty(ap.getAccessKey("USERNAME")) ||
                string.IsNullOrEmpty(ap.getAccessKey("PASSWORD"))
                )
            {
                StartActivity(typeof(Settings));
            }
            else
            {
                // Set up TimetView Font and Colour
                TextView TimerViewer = FindViewById <TextView>(Resource.Id.TimerView);
                //TODO: Remove ant unsued font files for release build
                Typeface tf = Typeface.CreateFromAsset(Application.Context.Assets, "DS-DIGIT.TTF");
                TimerViewer.SetTypeface(tf, TypefaceStyle.Normal);
                //  TimerViewer.SetTextColor(Android.Graphics.Color.Green);

                PopulateCustomersSpinner();
                MyKimai.url = ap.getAccessKey("URL") + "/core/json.php";
                RunUpdateLoop();

                Button update_button = FindViewById <Button>(Resource.Id.update);
                update_button.Click += delegate
                {
                    GetActiveRecord();
                };

                strApiKey = ap.getAccessKey("APIKEY");
                // Do we haave an api key?
                try
                {
                    if (string.IsNullOrEmpty(strApiKey))
                    {
                        // No, Let's log in

                        LoginToKimai();
                    }
                    else
                    {
                        //
                        ToggleButton togglebutton = FindViewById <ToggleButton>(Resource.Id.toggleButton1);
                        // Let's get the data for any current active recording and update the start/stop button states

                        if (GetActiveRecord())
                        {
                            togglebutton.Checked = true;
                            RunUpdateLoopState   = true;
                            Spinner CustomersSpinner = FindViewById <Spinner>(Resource.Id.spinnerCustomers);
                            CustomersSpinner.SetSelection(
                                GetDictionaryKeyFromValue(CustomerLookupList, CurrentCustomerInTimer)
                                );
                            Tv2      = FindViewById <TextView>(Resource.Id.textView2);
                            Tv2.Text = RunUpdateLoopState.ToString();
                        }
                        else
                        {
                            togglebutton.Checked = false;
                            RunUpdateLoopState   = false;
                            Tv2      = FindViewById <TextView>(Resource.Id.textView2);
                            Tv2.Text = RunUpdateLoopState.ToString();
                        }

                        togglebutton.Click += (o, e) =>
                        {
                            // Perform action on clicks
                            if (togglebutton.Checked)
                            {
                                if (!GetActiveRecord())
                                {
                                    Spinner       ProjectSpinner  = FindViewById <Spinner>(Resource.Id.spinnerProjects);
                                    Spinner       ActivitySpinner = FindViewById <Spinner>(Resource.Id.spinnerActivities);
                                    List <string> Parameters      = new List <string>();

                                    int pos = (int)ProjectSpinner.SelectedItemId;
                                    Parameters.Add(strApiKey);
                                    Parameters.Add(
                                        ProjectLookupList[pos].ToString()

                                        );
                                    Parameters.Add(
                                        // ActivitiesLookupList[CurrentActivityInTimer].ToString()
                                        ActivitiesLookupList[
                                            (int)ActivitySpinner.SelectedItemId
                                        ].ToString()
                                        );
                                    //   string s = String.Format("{0} - {1} - {2}", CurrentCustomerInTimer, CurrentProjectInTimer, CurrentActivityInTimer);
                                    //   Toast.MakeText(this, s  ,ToastLength.Long).Show();
                                    System.Threading.Tasks.Task taskA = System.Threading.Tasks.Task.Factory.StartNew(() => MyKimai.ConnectAsync("startRecord", Parameters));
                                    taskA.Wait();
                                    RunUpdateLoopState = true;
                                    DurationCount      = 0;
                                }
                                else
                                {
                                    // We did not start a new recording we just switvhed to the active one.
                                    //Todo: this has big repercussions, as many devices could start mnay recordings with out stopping any exiting ones
                                    // If this proves to be a problem the best approach may be just to exit and warn the user multiple timers are running.
                                    Toast.MakeText(this, "There appears to be an active recording", ToastLength.Long).Show();
                                    RunUpdateLoopState = true;
                                    GetActiveRecord();
                                }
                            }
                            else
                            {
                                List <string> Parameters = new List <string>
                                {
                                    strApiKey
                                };
                                System.Threading.Tasks.Task taskA = System.Threading.Tasks.Task.Factory.StartNew(() => MyKimai.ConnectAsync("stopRecord", Parameters));
                                taskA.Wait();
                                RunUpdateLoopState = false;
                                DurationCount      = 0;

                                GetActiveRecord();
                            }
                        };
                    }
                }
                catch (AggregateException ex)
                {
                    Toast mesg = Toast.MakeText(this, ex.Message, ToastLength.Long);
                    mesg.Show();
                }
            }
        }
Exemple #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Settings);
            string dbPath = System.IO.Path.Combine(
                System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
                "localkimaidata.db3");

            db = new SQLiteConnection(dbPath);
            // Create your application here
            SetContentView(Resource.Layout.Settings);
            Context        mContext = Application.Context;
            AppPreferences ap       = new AppPreferences(mContext);
            TextView       txtField = FindViewById <TextView>(Resource.Id.edit_url);

            txtField.Text = ap.getAccessKey("URL");

            txtField      = FindViewById <TextView>(Resource.Id.edit_username);
            txtField.Text = ap.getAccessKey("USERNAME");

            txtField      = FindViewById <TextView>(Resource.Id.edit_password);
            txtField.Text = ap.getAccessKey("PASSWORD");

            Button sql_lite = FindViewById <Button>(Resource.Id.btn_view_data);

            sql_lite.Click += delegate
            {
                StartActivity(typeof(SqliteActivity));
            };


            Button save_button = FindViewById <Button>(Resource.Id.btn_save);

            save_button.Click += delegate
            {
                char[]   charsToTrim = { '*', ' ', '\'' };
                TextView url         = FindViewById <TextView>(Resource.Id.edit_url);
                string   strApiUrl   = url.Text.Trim(charsToTrim);

                TextView username       = FindViewById <TextView>(Resource.Id.edit_username);
                string   strApiUserName = username.Text.Trim(charsToTrim);

                TextView password       = FindViewById <TextView>(Resource.Id.edit_password);
                string   strApiPassword = password.Text.Trim(charsToTrim);
                TextView statusMessage  = FindViewById <TextView>(Resource.Id.status_message);

                ap.saveAccessKey("URL", strApiUrl, true);
                ap.saveAccessKey("USERNAME", strApiUserName, true);
                ap.saveAccessKey("PASSWORD", strApiPassword, true);

                List <string> Parameters = new List <string>();
                Parameters.Add(strApiUserName);
                Parameters.Add(strApiPassword);
                KimaiServer MyKimai = new KimaiServer(strApiUrl + "/core/json.php");
                System.Threading.Tasks.Task taskA = System.Threading.Tasks.Task.Factory.StartNew(() => MyKimai.ConnectAsync("authenticate", Parameters));
                taskA.Wait();

                AuthenticateMap AuthObject = new AuthenticateMap();
                AuthObject = JsonConvert.DeserializeObject <AuthenticateMap>(MyKimai.JsonResultString);
                bool Success = AuthObject.Result.Success;
                if (Success)
                {
                    string apikey = AuthObject.Result.Items[0].apiKey;
                    ap.saveAccessKey("APIKEY", AuthObject.Result.Items[0].apiKey, true);
                    Parameters.Clear();
                    Parameters.Add(apikey);
                    System.Threading.Tasks.Task CustomersTask = System.Threading.Tasks.Task.Factory.StartNew(() => MyKimai.ConnectAsync("getCustomers", Parameters));
                    CustomersTask.Wait();
                    CustomerMap CustomerObject = new CustomerMap();
                    CustomerObject = JsonConvert.DeserializeObject <CustomerMap>(MyKimai.JsonResultString);
                    Success        = CustomerObject.Result.Success;
                    if (Success)
                    {
                        db.CreateTable <KimaiDatadase.Customer>();
                        db.DeleteAll <KimaiDatadase.Customer>();
                        var newCustomer = new KimaiDatadase.Customer();
                        foreach (JsonKimaiMaps.Customer item in CustomerObject.Result.Items)
                        {
                            newCustomer.CustomerID = item.customerID;
                            newCustomer.Name       = item.name;

                            db.Insert(newCustomer);
                        }
                    }

                    Parameters.Clear();
                    Parameters.Add(apikey);
                    Parameters.Add("1"); // nest in the activities
                    System.Threading.Tasks.Task ProjectsTask = System.Threading.Tasks.Task.Factory.StartNew(() => MyKimai.ConnectAsync("getProjects", Parameters));
                    ProjectsTask.Wait();
                    ProjectMap ProjectObject = new ProjectMap();
                    ProjectObject = JsonConvert.DeserializeObject <ProjectMap>(MyKimai.JsonResultString);
                    Success       = ProjectObject.Result.Success;
                    if (Success)
                    {
                        db.CreateTable <KimaiDatadase.Project>();
                        db.DeleteAll <KimaiDatadase.Project>();
                        db.CreateTable <ProjectActivity>();
                        db.DeleteAll <ProjectActivity>();
                        var newProject = new KimaiDatadase.Project();
                        foreach (JsonKimaiMaps.Project item in ProjectObject.Result.Items)
                        {
                            newProject.CustomerID = item.customerID;
                            newProject.ProjectID  = item.projectID;
                            newProject.Name       = item.name;
                            var newActivity = new ProjectActivity();
                            foreach (JsonKimaiMaps.Task i in item.Tasks)
                            {
                                newActivity.ActivityID = i.activityID;
                                newActivity.ProjectID  = item.projectID;
                                newActivity.Name       = i.name;
                                db.Insert(newActivity);
                            }
                            db.Insert(newProject);
                        }
                        StartActivity(typeof(MainActivity));
                    }
                }
                else
                {
                    Toast mesg = Toast.MakeText(this, "Login Failed", ToastLength.Short);
                    mesg.Show();
                }
            };

            Button cancel_button = FindViewById <Button>(Resource.Id.btn_cancel);

            cancel_button.Click += delegate
            {
                StartActivity(typeof(MainActivity));
            };
        }