Esempio n. 1
0
        private async Task OnAppearing()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            SettingsModel settings     = GetSettings();
            GeoCacheModel lastGeoCache = settings.GeoCache;

            if (!IsLastGeoCacheValid(lastGeoCache))
            {
                lastGeoCache = await UpdateLastGeoCache(lastGeoCache, settings);
            }

            if (lastGeoCache == null)
            {
                lastGeoCache = new GeoCacheModel();
            }

            Device.BeginInvokeOnMainThread(() =>
            {
                GeoCachePosition = new Position(lastGeoCache.GeoCacheLatitude, lastGeoCache.GeoCacheLongitude);
            });

            await StartListeningGps();

            StartListeningCompass();

            //SleepMode.ActivateAutoSleepMode(false);

            IsBusy = false;
        }
Esempio n. 2
0
        private bool IsLastGeoCacheValid(GeoCacheModel lastGeoCache)
        {
            if (lastGeoCache == null || lastGeoCache.GeoCacheLatitude == 0 || lastGeoCache.GeoCacheLongitude == 0)
            {
                return(false);
            }

            DateTimeOffset geoCacheHiddenTimestamp =
                DateTimeOffset.FromUnixTimeSeconds(lastGeoCache.GeoCacheHiddenTimestamp);

            return(DateTimeOffset.Now - geoCacheHiddenTimestamp < TimeSpan.FromDays(6));
        }
Esempio n. 3
0
        private async Task UpdateGeoCache()
        {
            var lastGeoCache = await UpdateLastGeoCache(null, GetSettings());

            if (lastGeoCache == null)
            {
                lastGeoCache = new GeoCacheModel();
            }

            Device.BeginInvokeOnMainThread(() =>
            {
                GeoCachePosition = new Position(lastGeoCache.GeoCacheLatitude, lastGeoCache.GeoCacheLongitude);
            });
        }
Esempio n. 4
0
        private async Task <GeoCacheModel> UpdateLastGeoCache(GeoCacheModel lastGeoCache, SettingsModel settings)
        {
            try
            {
                lastGeoCache = await(new DocumentService()).GetDocument();
                lastGeoCache.GeoCacheHiddenTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
                settings.GeoCache = lastGeoCache;

                Settings.GeneralSettings = JsonConvert.SerializeObject(settings);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                lastGeoCache = new GeoCacheModel();
            }

            return(lastGeoCache);
        }
Esempio n. 5
0
        public async Task <GeoCacheModel> GetDocument()
        {
            GeoCacheModel geoCacheInfos = null;

            using (HttpClient client = GetHttpClient())
            {
                var url      = _baseCloudantUrl + _documentName;
                var response = await client.GetAsync(url);

                if (response?.StatusCode == HttpStatusCode.OK)
                {
                    var responseAsString = await response.Content.ReadAsStringAsync();

                    geoCacheInfos = JsonConvert.DeserializeObject <GeoCacheModel>(responseAsString);
                }
            }

            return(geoCacheInfos);
        }