public NoteDataService(IMvxFileStore mvxFileStorage) { filePath = "noteStorage.json"; converter = new MvxJsonConverter(); storage = mvxFileStorage; if (!storage.Exists(filePath)) { storage.WriteFile(filePath, converter.SerializeObject(Enumerable.Empty<Note>())); } }
/// <summary> /// Check if a file exists on a certain location /// </summary> /// <param name="folder">The name of the folder</param> /// <param name="fullName">The name of the file</param> /// <returns>True if the file exists, otherwise false.</returns> public override bool Exists(string folder, string fullName) { var fullPath = _fileStore.PathCombine(folder, fullName); return(_fileStore.Exists(fullPath)); }
private async Task LoadDataService(IDataService dataService, bool overrideCache = false) { List <Item> currentItems = new List <Item>(); bool loadedFromCache = false; if (fileStore != null) { string lastRefreshText; if (fileStore.TryReadTextFile("LastRefresh-" + dataService.GetType().ToString(), out lastRefreshText)) { var lastRefreshTime = DateTime.Parse(lastRefreshText); var timeSinceLastRefreshInMinutes = (DateTime.Now - lastRefreshTime).TotalMinutes; //has cache expired? if (overrideCache || timeSinceLastRefreshInMinutes > AppSettings.CacheIntervalInMinutes) { currentItems = await dataService.GetItems(); } else //load from cache { string cachedItemsText; pool.WaitOne(); try { if (fileStore.TryReadTextFile("CachedItems-" + dataService.GetType().ToString(), out cachedItemsText)) { currentItems = mvxJsonConverter.DeserializeObject <List <Item> >(cachedItemsText); loadedFromCache = true; } } catch { ServiceLocator.MessageService.ShowErrorAsync("Error Deserializing " + dataService.GetType().ToString(), "Error loading cache"); } finally { pool.Release(); } } } else { currentItems = await dataService.GetItems(); } } try { if (!loadedFromCache && currentItems.Count > 0) { if (fileStore.Exists("CachedItems-" + dataService.GetType().ToString())) { fileStore.DeleteFile("CachedItems-" + dataService.GetType().ToString()); } if (fileStore.Exists("LastRefresh-" + dataService.GetType().ToString())) { fileStore.DeleteFile("LastRefresh-" + dataService.GetType().ToString()); } fileStore.WriteFile("CachedItems-" + dataService.GetType().ToString(), mvxJsonConverter.SerializeObject(currentItems)); fileStore.WriteFile("LastRefresh-" + dataService.GetType().ToString(), DateTime.Now.ToString()); } items.AddRange(currentItems); } catch { ServiceLocator.MessageService.ShowErrorAsync("Error retrieving items from " + dataService.GetType().ToString() + "\n\nPossible Causes:\nNo internet connection\nRemote Service unavailable", "Application Error"); } }