diff --git a/data/src/main/java/one/nem/lacerta/source/pref/impl/CommonImpl.java b/data/src/main/java/one/nem/lacerta/source/pref/impl/CommonImpl.java index c8aa14e8..f9791c76 100644 --- a/data/src/main/java/one/nem/lacerta/source/pref/impl/CommonImpl.java +++ b/data/src/main/java/one/nem/lacerta/source/pref/impl/CommonImpl.java @@ -1,4 +1,55 @@ package one.nem.lacerta.source.pref.impl; +import android.content.Context; +import android.content.SharedPreferences; + +import androidx.appcompat.app.AppCompatActivity; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import javax.inject.Inject; + public class CommonImpl { + + private final AppCompatActivity activity; + + @Inject + public CommonImpl(AppCompatActivity activity) { + this.activity = activity; + } + + public String getStringValue(String key) { + SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); + return pref.getString(key, ""); + } + + public void setStringValue(String key, String value) { + SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); + SharedPreferences.Editor editor = pref.edit(); + editor.putString(key, value); + editor.apply(); + } + + public boolean isExist(String key) { + SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); + return pref.contains(key); + } + + public void remove(String key) { + SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); + SharedPreferences.Editor editor = pref.edit(); + editor.remove(key); + editor.apply(); + } + + public ArrayList getExistKeys() { + SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); + // キーだけをArrayListに切り出す + return new ArrayList<>(pref.getAll().keySet()); + } + + + } diff --git a/data/src/main/java/one/nem/lacerta/source/pref/repository/Common.java b/data/src/main/java/one/nem/lacerta/source/pref/repository/Common.java index 3fb0f187..dec372d0 100644 --- a/data/src/main/java/one/nem/lacerta/source/pref/repository/Common.java +++ b/data/src/main/java/one/nem/lacerta/source/pref/repository/Common.java @@ -1,5 +1,6 @@ package one.nem.lacerta.source.pref.repository; +import java.util.ArrayList; import java.util.List; public interface Common { @@ -13,5 +14,5 @@ public interface Common { void remove(String key); - List getExistKeys(); + ArrayList getExistKeys(); }