mirror of
https://github.com/bitwarden/android.git
synced 2024-12-24 18:08:26 +03:00
init task
This commit is contained in:
parent
08c3aff60b
commit
fb50123af0
1 changed files with 32 additions and 14 deletions
|
@ -15,45 +15,63 @@ namespace Bit.Core.Services
|
||||||
};
|
};
|
||||||
private readonly string _dbPath;
|
private readonly string _dbPath;
|
||||||
private LiteCollection<JsonItem> _collection;
|
private LiteCollection<JsonItem> _collection;
|
||||||
|
private Task _initTask;
|
||||||
|
|
||||||
public LiteDbStorageService(string dbPath)
|
public LiteDbStorageService(string dbPath)
|
||||||
{
|
{
|
||||||
_dbPath = dbPath;
|
_dbPath = dbPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init()
|
public Task InitAsync()
|
||||||
{
|
{
|
||||||
if(_collection == null)
|
if(_collection != null)
|
||||||
{
|
{
|
||||||
var db = new LiteDatabase($"Filename={_dbPath};");
|
return Task.FromResult(0);
|
||||||
_collection = db.GetCollection<JsonItem>("json_items");
|
|
||||||
}
|
}
|
||||||
|
if(_initTask != null)
|
||||||
|
{
|
||||||
|
return _initTask;
|
||||||
|
}
|
||||||
|
Task doTask()
|
||||||
|
{
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var db = new LiteDatabase($"Filename={_dbPath};");
|
||||||
|
_collection = db.GetCollection<JsonItem>("json_items");
|
||||||
|
return Task.FromResult(0);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_initTask = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
_initTask = doTask();
|
||||||
|
return _initTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<T> GetAsync<T>(string key)
|
public async Task<T> GetAsync<T>(string key)
|
||||||
{
|
{
|
||||||
Init();
|
await InitAsync();
|
||||||
var item = _collection.Find(i => i.Id == key).FirstOrDefault();
|
var item = _collection.Find(i => i.Id == key).FirstOrDefault();
|
||||||
if(item == null)
|
if(item == null)
|
||||||
{
|
{
|
||||||
return Task.FromResult(default(T));
|
return default(T);
|
||||||
}
|
}
|
||||||
return Task.FromResult(JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings));
|
return JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SaveAsync<T>(string key, T obj)
|
public async Task SaveAsync<T>(string key, T obj)
|
||||||
{
|
{
|
||||||
Init();
|
await InitAsync();
|
||||||
var data = JsonConvert.SerializeObject(obj, _jsonSettings);
|
var data = JsonConvert.SerializeObject(obj, _jsonSettings);
|
||||||
_collection.Upsert(new JsonItem(key, data));
|
_collection.Upsert(new JsonItem(key, data));
|
||||||
return Task.FromResult(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task RemoveAsync(string key)
|
public async Task RemoveAsync(string key)
|
||||||
{
|
{
|
||||||
Init();
|
await InitAsync();
|
||||||
_collection.Delete(i => i.Id == key);
|
_collection.Delete(i => i.Id == key);
|
||||||
return Task.FromResult(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class JsonItem
|
private class JsonItem
|
||||||
|
|
Loading…
Reference in a new issue