Skip to content

Commit 9f38d95

Browse files
committedApr 30, 2023
updates
1 parent 39dd337 commit 9f38d95

File tree

8 files changed

+82
-7
lines changed

8 files changed

+82
-7
lines changed
 

‎IndexDb.Example/Pages/Index.razor

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
@page "/"
22
@inject IMagicDbFactory _MagicDb
33

4+
<p>Storage: @storageQuota - @storageUsage</p>
5+
46
<PageTitle>Example</PageTitle>
57

68
<h3>People In IndexedDb!</h3>
9+
710
<table class="table">
811
<thead>
912
<tr>

‎IndexDb.Example/Pages/Index.razor.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public partial class Index
99

1010
private IEnumerable<Person> WhereExample { get; set; } = Enumerable.Empty<Person>();
1111

12-
12+
private double storageQuota { get; set; }
13+
private double storageUsage { get; set; }
1314
protected override async Task OnAfterRenderAsync(bool firstRender)
1415
{
1516
if (firstRender)
@@ -38,6 +39,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
3839
await manager.AddRange(persons);
3940
}
4041

42+
43+
//var StorageLimit = await manager.GetStorageEstimateAsync();
44+
var storageInfo = await manager.GetStorageEstimateAsync();
45+
storageQuota = storageInfo.quota;
46+
storageUsage = storageInfo.usage;
47+
4148
var allPeopleDecrypted = await manager.GetAll<Person>();
4249

4350
foreach (Person person in allPeopleDecrypted)

‎LICENSE.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) [year] [fullname]
3+
Copyright (c) 2023 Magic Man
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

‎Magic.IndexedDb.sln

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.5.33502.453
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Magic.IndexedDb", "Magic.IndexedDb\Magic.IndexedDb.csproj", "{A92429BE-E180-4150-BFC7-6C09558978D0}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Magic.IndexedDb", "Magic.IndexedDb\Magic.IndexedDb.csproj", "{A92429BE-E180-4150-BFC7-6C09558978D0}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IndexDb.Example", "IndexDb.Example\IndexDb.Example.csproj", "{4607CA8C-3AE3-4288-A52A-B3887B254657}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IndexDb.Example", "IndexDb.Example\IndexDb.Example.csproj", "{4607CA8C-3AE3-4288-A52A-B3887B254657}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -14,11 +14,9 @@ Global
1414
EndGlobalSection
1515
GlobalSection(ProjectConfigurationPlatforms) = postSolution
1616
{A92429BE-E180-4150-BFC7-6C09558978D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17-
{A92429BE-E180-4150-BFC7-6C09558978D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
1817
{A92429BE-E180-4150-BFC7-6C09558978D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
1918
{A92429BE-E180-4150-BFC7-6C09558978D0}.Release|Any CPU.Build.0 = Release|Any CPU
2019
{4607CA8C-3AE3-4288-A52A-B3887B254657}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{4607CA8C-3AE3-4288-A52A-B3887B254657}.Debug|Any CPU.Build.0 = Debug|Any CPU
2220
{4607CA8C-3AE3-4288-A52A-B3887B254657}.Release|Any CPU.ActiveCfg = Release|Any CPU
2321
{4607CA8C-3AE3-4288-A52A-B3887B254657}.Release|Any CPU.Build.0 = Release|Any CPU
2422
EndGlobalSection

‎Magic.IndexedDb/IndexDbManager.cs

+25
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,26 @@ void AddConditionInternal(MemberExpression left, ConstantExpression right, strin
695695
return JsonConvert.SerializeObject(orConditions, serializerSettings);
696696
}
697697

698+
/// <summary>
699+
/// Returns Mb
700+
/// </summary>
701+
/// <returns></returns>
702+
public async Task<(double quota, double usage)> GetStorageEstimateAsync()
703+
{
704+
//var storageInfo = await _jsRuntime.InvokeAsync<(long, long)>("storageInterop.getStorageEstimate");
705+
var storageInfo = await CallJavascriptNoTransaction<(long, long)>(IndexedDbFunctions.GET_STORAGE_ESTIMATE);
706+
707+
double quotaInMB = ConvertBytesToMegabytes(storageInfo.Item1);
708+
double usageInMB = ConvertBytesToMegabytes(storageInfo.Item2);
709+
return (quotaInMB, usageInMB);
710+
}
711+
712+
713+
private static double ConvertBytesToMegabytes(long bytes)
714+
{
715+
return (double)bytes / (1024 * 1024);
716+
}
717+
698718

699719
public async Task<IEnumerable<T>> GetAll<T>() where T : class
700720
{
@@ -870,6 +890,11 @@ public void CalledFromJS(Guid transaction, bool failed, string message)
870890
}
871891
}
872892

893+
async Task<TResult> CallJavascriptNoTransaction<TResult>(string functionName, params object[] args)
894+
{
895+
return await _jsRuntime.InvokeAsync<TResult>($"{InteropPrefix}.{functionName}", args);
896+
}
897+
873898
async Task<TResult> CallJavascript<TResult>(string functionName, Guid transaction, params object[] args)
874899
{
875900
var newArgs = GetNewArgs(transaction, args);

‎Magic.IndexedDb/IndexedDbFunctions.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ internal struct IndexedDbFunctions
1717
public const string WHERE = "where";
1818
public const string WHEREV2 = "wherev2";
1919
public const string BULK_DELETE = "bulkDelete";
20+
public const string GET_STORAGE_ESTIMATE = "getStorageEstimate";
21+
2022

21-
2223
}
2324
}

‎Magic.IndexedDb/Magic.IndexedDb.csproj

+27
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,35 @@
44
<TargetFramework>net7.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<Title>Magic.IndexedDb</Title>
8+
<Authors>Magic Man</Authors>
9+
<Copyright>MIT</Copyright>
10+
<Description>Open source library provides an IndexedDb wrapper for C# and Blazor WebAssembly applications. It simplifies working with IndexedDb and makes it similar to using LINQ to SQL.</Description>
11+
<PackageProjectUrl></PackageProjectUrl>
12+
<RepositoryUrl>https://github.com/magiccodingman/Magic.IndexedDb</RepositoryUrl>
13+
<PackageTags>IndexedDb, Blazor IndexedDb, c# IndexedDb, local database, blazor</PackageTags>
14+
<PackageIcon>icons8-wizard-96.png</PackageIcon>
15+
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
16+
<AssemblyVersion>1.0.0</AssemblyVersion>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
18+
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
719
</PropertyGroup>
820

21+
<ItemGroup>
22+
<None Include="..\..\..\Users\lance\Downloads\icons8-wizard-color\icons8-wizard-96.png">
23+
<Pack>True</Pack>
24+
<PackagePath>\</PackagePath>
25+
</None>
26+
<None Include="..\LICENSE.txt">
27+
<Pack>True</Pack>
28+
<PackagePath>\</PackagePath>
29+
</None>
30+
<None Include="..\README.md">
31+
<Pack>True</Pack>
32+
<PackagePath>\</PackagePath>
33+
</None>
34+
</ItemGroup>
35+
936

1037
<ItemGroup>
1138
<SupportedPlatform Include="browser" />

‎Magic.IndexedDb/wwwroot/magicDB.js

+14
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,20 @@ window.magicBlazorDB = {
579579
});
580580
});
581581
});
582+
},
583+
getStorageEstimate: async () => {
584+
if (navigator.storage && navigator.storage.estimate) {
585+
const estimate = await navigator.storage.estimate();
586+
return {
587+
quota: estimate.quota,
588+
usage: estimate.usage
589+
};
590+
} else {
591+
return {
592+
quota: -1,
593+
usage: -1
594+
};
595+
}
582596
}
583597
}
584598

0 commit comments

Comments
 (0)
Please sign in to comment.