WIP Pref Editor

This commit is contained in:
ろむねこ 2023-12-11 11:07:40 +09:00
parent fd04610e6c
commit f77e19556a
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168
3 changed files with 74 additions and 19 deletions

View File

@ -36,4 +36,5 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(this, "testMessage", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "testMessage", Toast.LENGTH_SHORT).show();
} }
} }

View File

@ -11,41 +11,42 @@ import java.util.stream.Collectors;
import javax.inject.Inject; import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
import dagger.hilt.android.qualifiers.ApplicationContext;
@AndroidEntryPoint
public class CommonImpl { public class CommonImpl {
private final AppCompatActivity activity; @ApplicationContext
@Inject @Inject
public CommonImpl(AppCompatActivity activity) { Context applicationContext;
this.activity = activity;
}
public String getStringValue(String key) { public String getStringValue(String key) {
SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); SharedPreferences pref = applicationContext.getSharedPreferences("common", Context.MODE_PRIVATE);
return pref.getString(key, ""); return pref.getString(key, "");
} }
public void setStringValue(String key, String value) { public void setStringValue(String key, String value) {
SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); SharedPreferences pref = applicationContext.getSharedPreferences("common", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit(); SharedPreferences.Editor editor = pref.edit();
editor.putString(key, value); editor.putString(key, value);
editor.apply(); editor.apply();
} }
public boolean isExist(String key) { public boolean isExist(String key) {
SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); SharedPreferences pref = applicationContext.getSharedPreferences("common", Context.MODE_PRIVATE);
return pref.contains(key); return pref.contains(key);
} }
public void remove(String key) { public void remove(String key) {
SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); SharedPreferences pref = applicationContext.getSharedPreferences("common", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit(); SharedPreferences.Editor editor = pref.edit();
editor.remove(key); editor.remove(key);
editor.apply(); editor.apply();
} }
public ArrayList<String> getExistKeys() { public ArrayList<String> getExistKeys() {
SharedPreferences pref = activity.getSharedPreferences("common", Context.MODE_PRIVATE); SharedPreferences pref = applicationContext.getSharedPreferences("common", Context.MODE_PRIVATE);
// キーだけをArrayListに切り出す // キーだけをArrayListに切り出す
return new ArrayList<>(pref.getAll().keySet()); return new ArrayList<>(pref.getAll().keySet());
} }

View File

@ -1,16 +1,69 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".DebugSharedPrefEditorFragment" > android:orientation="vertical"
tools:context=".DebugSharedPrefEditorFragment">
<androidx.recyclerview.widget.RecyclerView <com.google.android.material.textview.MaterialTextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent" android:text="Save pref" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" <LinearLayout
app:layout_constraintTop_toTopOf="parent" /> android:id="@+id/SaveLinearLayout"
</androidx.constraintlayout.widget.ConstraintLayout> android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<EditText
android:id="@+id/saveKeyEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="2"
android:hint="Key" />
<com.google.android.material.button.MaterialButton
android:id="@+id/saveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:lauout_weight="1"
android:text="Save" />
</LinearLayout>
<com.google.android.material.textview.MaterialTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load pref" />
<LinearLayout
android:id="@+id/LoadLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<EditText
android:id="@+id/loadKeyEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="Key" />
<com.google.android.material.button.MaterialButton
android:id="@+id/loadButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:lauout_weight="1"
android:text="Load" />
</LinearLayout>
</LinearLayout>