Beispiel #1
0
        private async void sync()
        {
            _refreshView.IsRefreshing = true;

            var toast = DependencyService.Get <IToastNotificator>();

            if (!CrossConnectivity.Current.IsConnected)
            { //non connesso a internet
                await toast.Notify(ToastNotificationType.Error, "Errore", "Nessun accesso a internet", TimeSpan.FromSeconds(3));

                return;
            }

            bool success = false;

            for (int i = 0; i < 3; i++)
            {
                success = await _db.SynchronizeAzureDb();

                if (!success)
                {
                    //await toast.Notify(ToastNotificationType.Error, "Errore", "Sincronizzazione fallita!", TimeSpan.FromSeconds(3));
                    if (i == 2)
                    {
                        await toast.Notify(ToastNotificationType.Error, "Errore", "Sincronizzazione fallita!", TimeSpan.FromSeconds(3));
                    }

                    updateLabelInfo(string.Format("Errore sincronizzazione - Tentativo {0}", i + 1), Color.Red);
                    continue; //riprovo
                }

                await updateDbOrariUtenza();

                break;
            }

            MessagingCenter.Send <TabbedDayView, int>(this, "pullToRefresh", 0);
            Debug.WriteLine("Command executed");
            updateLabelInfo(); //aggiorna la label delle information (da aggiornare oppure rilassati)
            _refreshView.IsRefreshing = false;
        }
        private async void _btnAccedi_Clicked(object sender, EventArgs e)
        {
            await _service.Initialize();

            _btnAccedi.IsVisible         = false;
            _lblSalta.IsVisible          = false;
            _lblAlert.IsVisible          = true;
            _activityIndicator.IsVisible = true;



            _lblAlert.Text = string.Format("Accesso in corso");
            var user = await DependencyService.Get <IAuthentication>().LoginAsync(_service.MobileService, MobileServiceAuthenticationProvider.Google);

            _lblAlert.Text = string.Format("Sto ottenendo le tue informazioni");

            //aggiunge utente alla tabella
            var isNewUser = await addUser();

            //**NELL'IMPLEMENTAZIONE Dell'authentication
            //Settings.Email = user.Message.Email;
            //Settings.Username = Settings.Email.Split('@')[0];
            //Settings.Name = user.Message.Name;
            //Settings.SocialId = user.Message.SocialId;

            var name = Settings.Name;

            NavigationPage nav;

            //if informazioni facoltà, laurea, anno NON sono già presenti -> è il primo accesso -> information view
            //if (true) ///*********************************SOLO PER PROVA DEBUG TEST AZURE
            if (isNewUser || !checkUniversityInformation())
            {
                nav = new NavigationPage(new InformationView()
                {
                    Service = _service
                })
                {
                    BarBackgroundColor = ColorHelper.Blue700,
                    BarTextColor       = ColorHelper.White,
                };
                Settings.SuccessLogin = true;
                await Navigation.PushModalAsync(nav);
            }
            else //altrimenti accedo direttamente
            {
                _lblAlert.Text = string.Format("Sto sincronizzando i tuoi dati");
                _db            = new DbSQLite();

                var toast = DependencyService.Get <IToastNotificator>();
                if (!CrossConnectivity.Current.IsConnected)
                { //non connesso a internet
                    await toast.Notify(ToastNotificationType.Error, "Errore", "Nessun accesso a internet", TimeSpan.FromSeconds(3));

                    return;
                }

                var success = await _db.SynchronizeAzureDb();

                if (!success)
                {
                    await toast.Notify(ToastNotificationType.Error, "Errore", "Sincronizzazione fallita!", TimeSpan.FromSeconds(3));
                }

                Settings.SuccessLogin = true;
                Settings.ToUpdate     = true;

                if (Settings.BackgroundSync)
                {
                    DependencyService.Get <INotification>().BackgroundSync();
                }

                await Navigation.PushModalAsync(new MasterDetailView());
            }
        }
//		public SampleSchedulingService() : base("SchedulingIntentService")
//		{ }

        protected async override void OnHandleIntent(Android.Content.Intent intent)
        {
            Logcat.Write("ALARM: " + DateTime.Now.ToLongTimeString());

            _db = App.Database;

            if (_db == null)
            {
                App.Init(new DbSQLite(new SQLite_Android().GetConnection()));
                _db = App.Database;
//				if (_db == null)
//					Logcat.Write ("_db ancora NULL dopo INIT");
//				else
//					Logcat.Write ("_db NOT NULL dopo INIT");
            }
            else
            {
                Logcat.Write("_db NOT NULL");
            }

            Logcat.WriteDB(_db, "Allarme attivato");
            Logcat.Write("NUMERO MIEI CORSI: " + _db.GetAllMieiCorsi().Count());

            _intent = intent;
            //_listOrariGiorno = _db.GetAllOrari();

            Logcat.Write("Updating Date Time");
            if (DateTime.Now.Hour > Settings.UpdateHour) // && DateTime.Now.Minute > Settings.UpdateMinute)
            {
                _oggi = new DayViewModel()
                {
                    Data = DateTime.Today.AddDays(1)
                };
                _domani = new DayViewModel()
                {
                    Data = _oggi.Data.AddDays(1)
                };
                _dopodomani = new DayViewModel()
                {
                    Data = _domani.Data.AddDays(1)
                };
            }
            else
            {
                _oggi = new DayViewModel()
                {
                    Data = DateTime.Today
                };
                _domani = new DayViewModel()
                {
                    Data = _oggi.Data.AddDays(1)
                };
                _dopodomani = new DayViewModel()
                {
                    Data = _domani.Data.AddDays(1)
                };
            }


            if (!CrossConnectivity.Current.IsConnected)
            { //non connesso a internet
                Logcat.WriteDB(_db, "*************ERRORE");
                Logcat.WriteDB(_db, "AGGIORNAMENTO NON RIUSCITO, nessun accesso a internet");
                Logcat.Write("AGGIORNAMENTO NON RIUSCITO, nessun accesso a internet");
                //var toast = DependencyService.Get<IToastNotificator>();
                //await toast.Notify (ToastNotificationType.Error, "Errore", "Nessun accesso a internet", TimeSpan.FromSeconds (3));
                return;
            }

            /*SYNCRONIZZAZIONE BACKGROUND
             * lista dei corsi salvati. Scandisco ogni elemento della lista con la lista di tutti i corsi di giornata
             * se insegnamento+ora sono uguali, verifico se le Note cambiano. Se cambiano, la salvo nel database e invio la notifica
             * */
            try
            {
                for (int i = 0; i < 3; i++)
                {
                    Logcat.WriteDB(_db, string.Format("Tentativo {0} - SyncAzure", i + 1));
                    var success = await _db.SynchronizeAzureDb();

                    if (!success)
                    {
                        Logcat.WriteDB(_db, "*************ERRORE");
                        Logcat.WriteDB(_db, "SINCRONIZZAZIONE AZURE NON RIUSCITA, nessun accesso a internet");
                        //RETRYING??
                        continue; //riprovo
                        //return;
                    }
                    Logcat.WriteDB(_db, "---Autenticazione avvenuta con succeso!");

                    await updateDbOrariUtenza();

                    break; //se autenticazione avvenuta con successo, esco dal ciclo
                }
            }
            catch (Exception ex)
            {
                Logcat.WriteDB(_db, ex.Message);
                Logcat.Write(ex.Message);
            }

            // Release the wake lock provided by the BroadcastReceiver.
            SampleAlarmReceiver.CompleteWakefulIntent(intent);
        }