Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit 0e3de46

Browse files
author
Mohammad Ali Riazati
committed
Initial commit
Initial commit
1 parent 0a83123 commit 0e3de46

6 files changed

Lines changed: 55 additions & 46 deletions

File tree

.idea/codeStyles/Project.xml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.idea/encodings.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/ir/farsroidx/application/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protected void onCreate(Bundle savedInstanceState) {
1212
super.onCreate(savedInstanceState);
1313
setContentView(R.layout.activity_main);
1414

15-
SPUtils utils = new SPUtils(this);
15+
SPUtils utils = SPUtils.getInstance(this);
1616

1717
// Write To Preference
1818
utils.writeString ("key" , "text");

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.3.2'
9+
classpath 'com.android.tools.build:gradle:3.4.2'
1010
}
1111
}
1212

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Sun Apr 14 12:48:11 IRDT 2019
1+
#Sun Jul 14 14:58:43 IRDT 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,103 @@
11
package ir.farsroidx;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
45
import android.content.SharedPreferences;
56
import android.preference.PreferenceManager;
67

78
public class SPUtils {
89

910
private Context context;
11+
@SuppressLint("StaticFieldLeak")
12+
private static SPUtils spUtils = null;
13+
private SharedPreferences sharedPreferences = null;
14+
private SharedPreferences.Editor editor = null;
1015

11-
public SPUtils(Context context) {
16+
private SPUtils(Context context) {
1217
this.context = context;
1318
}
1419

15-
private SharedPreferences preference() {
16-
return PreferenceManager.getDefaultSharedPreferences(context);
20+
public static SPUtils getInstance(Context context){
21+
if(spUtils == null){
22+
spUtils = new SPUtils(context);
23+
}
24+
return spUtils;
25+
}
26+
27+
private SharedPreferences getPreferenceInstance() {
28+
if(sharedPreferences == null){
29+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
30+
}
31+
return sharedPreferences;
32+
}
33+
34+
private SharedPreferences.Editor getPreferenceEditor() {
35+
if(editor == null){
36+
editor = getPreferenceInstance().edit();
37+
}
38+
return editor;
1739
}
1840

1941
public String readString(String key, String alternative) {
20-
return preference().getString(key, alternative);
42+
return getPreferenceInstance().getString(key, alternative);
2143
}
2244

2345
public float readFloat(String key, float alternative) {
24-
return preference().getFloat(key, alternative);
46+
return getPreferenceInstance().getFloat(key, alternative);
2547
}
2648

2749
public long readLong(String key, long alternative) {
28-
return preference().getLong(key, alternative);
50+
return getPreferenceInstance().getLong(key, alternative);
2951
}
3052

3153
public int readInteger(String key, int alternative) {
32-
return preference().getInt(key, alternative);
54+
return getPreferenceInstance().getInt(key, alternative);
3355
}
3456

3557
public boolean readBoolean(String key, boolean alternative) {
36-
return preference().getBoolean(key, alternative);
58+
return getPreferenceInstance().getBoolean(key, alternative);
3759
}
3860

3961
public void writeString(String key, String str) {
40-
SharedPreferences.Editor editor = preference().edit();
62+
SharedPreferences.Editor editor = getPreferenceEditor();
4163
editor.putString(key, str);
4264
editor.apply();
4365
}
4466

4567
public void writeFloat(String key, float flt) {
46-
SharedPreferences.Editor editor = preference().edit();
68+
SharedPreferences.Editor editor = getPreferenceEditor();
4769
editor.putFloat(key , flt);
4870
editor.apply();
4971
}
5072

5173
public void writeLong(String key, long lng) {
52-
SharedPreferences.Editor editor = preference().edit();
74+
SharedPreferences.Editor editor = getPreferenceEditor();
5375
editor.putLong(key,lng);
5476
editor.apply();
5577
}
5678

5779
public void writeInteger(String key, int integer) {
58-
SharedPreferences.Editor editor = preference().edit();
80+
SharedPreferences.Editor editor = getPreferenceEditor();
5981
editor.putInt(key, integer);
6082
editor.apply();
6183
}
6284

6385
public void writeBoolean(String key, boolean bln) {
64-
SharedPreferences.Editor editor = preference().edit();
86+
SharedPreferences.Editor editor = getPreferenceEditor();
6587
editor.putBoolean(key, bln);
6688
editor.apply();
6789
}
6890

91+
public void remove(String key) {
92+
SharedPreferences.Editor editor = getPreferenceEditor();
93+
editor.remove(key);
94+
editor.apply();
95+
}
96+
97+
public void clearAll() {
98+
SharedPreferences.Editor editor = getPreferenceEditor();
99+
editor.clear();
100+
editor.apply();
101+
}
102+
69103
}

0 commit comments

Comments
 (0)