Xamarin Forms Essentials Using Preferences to Store Data

This is how I use Xamarin Forms Essentials Preferences to save and get data. I use to store non sensitive stuff like URLs, Names, IDs, Dates.

  1. I add the Xamarin.Essentials Nuget and the Newtonsoft.Json Nuget in my Xamarin.Forms app
  2. I create a class called “Item.cs”.
  3. This will have whatever public properties you want i.e. Name, Date, ID, Url, Active, etc…
  4. I then create another class called “noSQL.cs”.
  5. This class is where I use the Xamarin.Essentials Preferences.
public class Item
{
public int ItemID { get; set; }
public string ItemTitle { get; set; }
public string ItemText { get; set; }
public DateTime ItemCreateDate { get; set; }
public bool ItemComplete { get; set; }
public string ItemCompleteDisplay { get; set; }
public string ItemCreateDateDisplay { get; set; }
}
public class noSql
{
private string dbName = "MyItems";
public async Task<ObservableCollection<Item>> GetItems()
{
ObservableCollection<Item> list = new ObservableCollection<Item>();
List<Item> sort = new List<Item>();
string ijson = Preferences.Get(dbName, "");
if (!string.IsNullOrEmpty(ijson))
sort = JsonConvert.DeserializeObject<List<Item>>(ijson);

foreach(Item i in sort.OrderBy(x => x.ItemID))
{
if (i.ItemComplete)
{
i.ItemCompleteDisplay = "Completed";
i.ItemCreateDateDisplay = string.Empty;
}
else
{
i.ItemCompleteDisplay = string.Empty;
i.ItemCreateDateDisplay = i.ItemCreateDate.ToString("MM/dd/yyyy");
}
list.Add(i);
}
return await Task.FromResult(list);
}

public bool AddItem(Item item)
{
bool isGood = false;
//get list first 
try
{
item.ItemCreateDate = DateTime.Now;
bool bItems = Preferences.ContainsKey(dbName);
if (bItems)
{
string ijson = Preferences.Get(dbName, "");
var list = JsonConvert.DeserializeObject<ObservableCollection<Item>>(ijson);
list.Add(item);
string jsonEnum = JsonConvert.SerializeObject(list);
Preferences.Set(dbName, jsonEnum);
isGood = true;
}
else
{
//first one so add it. 
List<Item> list = new List<Item>();
list.Add(item);
string jsonItem = JsonConvert.SerializeObject(list);
Preferences.Set(dbName, jsonItem);
isGood = true;
}
}
catch(Exception ex)
{
string err = ex.Message;
}
return isGood;
}

public async Task<bool> AddNewItem(Item item)
{
bool isGood = false;
try
{
item.ItemCreateDate = DateTime.Now;
bool bItems = Preferences.ContainsKey(dbName);
if (bItems)
{
string ijson = Preferences.Get(dbName, "");
List<Item> list = JsonConvert.DeserializeObject<List<Item>>(ijson);

if (list.Count > 0)
item.ItemID = list.LastOrDefault().ItemID + 1;
else
item.ItemID = 1;

list.Add(item);
string jsonEnum = JsonConvert.SerializeObject(list);
Preferences.Set(dbName, jsonEnum);
}
else
{
//first one so add it. 
item.ItemID = 1;
item.ItemCreateDate = DateTime.Now;
List<Item> list = new List<Item>();
list.Add(item);
string jsonItem = JsonConvert.SerializeObject(list);
Preferences.Set(dbName, jsonItem);
}
isGood = true;
}
catch (Exception ex)
{
string err = ex.Message;
}
return await Task.FromResult(isGood);

}
public async Task<bool> UpdateItem(Item uItem)
{
bool isGood = false;
ObservableCollection<Item> list = new ObservableCollection<Item>();
string ijson = Preferences.Get(dbName, "");
try
{
list = JsonConvert.DeserializeObject<ObservableCollection<Item>>(ijson);
Item i = new Item();
i = list.First(x => x.ItemID == uItem.ItemID);
list.Remove(i);
if (i != null)
{
list.Add(uItem);
string jsonItem = JsonConvert.SerializeObject(list);
Preferences.Set(dbName, jsonItem);
isGood = true;
}
}
catch
{

}
return await Task.FromResult(isGood); 
}

public async Task<bool> DeleteItem(Item dItem)
{
List<Item> list = new List<Item>();
bool isGood = false;
try
{
string ijson = Preferences.Get(dbName, "");
list = JsonConvert.DeserializeObject<List<Item>>(ijson);
if (list.Count > 0)
{
Item i = list.Where(x => x.ItemID == dItem.ItemID).FirstOrDefault();
list.Remove(i);
string jsonItem = JsonConvert.SerializeObject(list);
Preferences.Set(dbName, jsonItem);
isGood = true;
}
}
catch { 

}

return await Task.FromResult(isGood);
}
}