Impl実装

This commit is contained in:
ろむねこ 2023-12-11 10:49:21 +09:00
parent e37328e1f5
commit cb687d51e4
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168
2 changed files with 53 additions and 1 deletions

View File

@ -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<String> getExistKeys() {
SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE);
// キーだけをArrayListに切り出す
return new ArrayList<>(pref.getAll().keySet());
}
}

View File

@ -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<String> getExistKeys();
ArrayList<String> getExistKeys();
}