1
1
// Copyright (c) 2023 Files Community
2
2
// Licensed under the MIT License. See the LICENSE.
3
3
4
- using System ;
5
- using System . Collections . Generic ;
6
- using System . Diagnostics ;
4
+ using System . Collections . Concurrent ;
7
5
using System . Text . Json ;
8
6
9
7
namespace Files . App . Utils . Serialization . Implementation
@@ -20,7 +18,7 @@ public DefaultJsonSettingsDatabase(ISettingsSerializer settingsSerializer, IJson
20
18
JsonSettingsSerializer = jsonSettingsSerializer ;
21
19
}
22
20
23
- protected Dictionary < string , object ? > GetFreshSettings ( )
21
+ protected IDictionary < string , object ? > GetFreshSettings ( )
24
22
{
25
23
string data = SettingsSerializer . ReadFromFile ( ) ;
26
24
@@ -29,10 +27,10 @@ public DefaultJsonSettingsDatabase(ISettingsSerializer settingsSerializer, IJson
29
27
data = "null" ;
30
28
}
31
29
32
- return JsonSettingsSerializer . DeserializeFromJson < Dictionary < string , object ? > ? > ( data ) ?? new ( ) ;
30
+ return JsonSettingsSerializer . DeserializeFromJson < ConcurrentDictionary < string , object ? > ? > ( data ) ?? new ( ) ;
33
31
}
34
32
35
- protected bool SaveSettings ( Dictionary < string , object ? > data )
33
+ protected bool SaveSettings ( IDictionary < string , object ? > data )
36
34
{
37
35
var jsonData = JsonSettingsSerializer . SerializeToJson ( data ) ;
38
36
@@ -58,14 +56,8 @@ public virtual bool SetValue<TValue>(string key, TValue? newValue)
58
56
{
59
57
var data = GetFreshSettings ( ) ;
60
58
61
- if ( ! data . ContainsKey ( key ) )
62
- {
63
- data . Add ( key , newValue ) ;
64
- }
65
- else
66
- {
59
+ if ( ! data . TryAdd ( key , newValue ) )
67
60
data [ key ] = newValue ;
68
- }
69
61
70
62
return SaveSettings ( data ) ;
71
63
}
@@ -88,7 +80,7 @@ public virtual bool ImportSettings(object? import)
88
80
try
89
81
{
90
82
// Try convert
91
- var data = ( Dictionary < string , object ? > ? ) import ;
83
+ var data = ( IDictionary < string , object ? > ? ) import ;
92
84
if ( data is null )
93
85
{
94
86
return false ;
0 commit comments