Esempio n. 1
0
        private async Task <MetaData> GetMetaData(bool forceServerRefresh = false)
        {
            if (forceServerRefresh && _metaLoadingTask != null && !_metaLoadingTask.IsCompleted && !_metaLoadingTask.IsFaulted && !_metaLoadingTask.IsCanceled)
            {
                return(await _metaLoadingTask);
            }
            _metaLoadingTask = Task.Run(async() =>
            {
                Debug.WriteLine("[MainVm] GetMetaData (forcerefresh={0})", forceServerRefresh);
                if (!forceServerRefresh)
                {
                    var offlineMetaData = await GetOfflineMetaData();
                    if (offlineMetaData != null)
                    {
                        Debug.WriteLine("[MainVm] GetMetaData (forcerefresh={0}): found offline data",
                                        forceServerRefresh);
                        return(offlineMetaData);
                    }
                }

                if (!InternetAvailable)
                {
                    Debug.WriteLine("[MainVm] GetMetaData (forcerefresh={0}): internet not available, returning null",
                                    forceServerRefresh);
                    return(null);
                }
                Debug.WriteLine("[MainVm] GetMetaData (forcerefresh={0}): perform request", forceServerRefresh);
                MetaData metaData;
                try
                {
                    metaData = await _client.GetMetaDataAsync();
                }
                catch (ApiException e)
                {
                    _exceptionService.HandleApiExceptionForMetaData(e);
                    return(null);
                }
                Debug.WriteLine("[MainVm] GetMetaData (forcerefresh={0}): got response", forceServerRefresh);
                _metaDataIsOnlineData = true;
                _storage.SaveMetaData(metaData);
                UpdateServiceData();
                return(metaData);
            });
            return(await _metaLoadingTask);
        }