PrefUtils追加, ログ永続化 #71

Merged
Fujimatsu merged 9 commits from feature/add_preference_manager into main 2024-06-14 03:24:53 +00:00
7 changed files with 124 additions and 9 deletions

View File

@ -58,9 +58,11 @@ public class DebugCommandProcessor {
}
private String executeLog(String[] commandArray) {
switch (commandArray[1]) {
commandArray = shiftArray(commandArray);
switch (commandArray[0]) {
case "get":
switch (commandArray[2]) {
commandArray = shiftArray(commandArray);
switch (commandArray[0]) {
case "all":
List<LogModel> logs = ksLogger.getHistory();
StringBuilder logString = new StringBuilder();
@ -73,8 +75,9 @@ public class DebugCommandProcessor {
return "TODO";
}
case "insert":
String[] logArray = Arrays.copyOfRange(commandArray, 3, commandArray.length);
switch (commandArray[2]) {
commandArray = shiftArray(commandArray);
String[] logArray = Arrays.copyOfRange(commandArray, 1, commandArray.length);
switch (commandArray[0]) {
case "info":
ksLogger.info(String.join(" ", logArray));
return "Log inserted";

View File

@ -1,5 +1,6 @@
[versions]
agp = "8.3.2"
gson = "2.11.0"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
@ -10,6 +11,7 @@ constraintlayout = "2.1.4"
nav = "2.7.7"
[libraries]
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }

View File

@ -37,4 +37,7 @@ dependencies {
// Hilt (DI)
implementation libs.com.google.dagger.hilt.android
annotationProcessor libs.com.google.dagger.hilt.compiler
// Gson
implementation libs.gson
}

View File

@ -0,0 +1,18 @@
package one.nem.kidshift.utils;
import android.content.SharedPreferences;
import java.util.List;
public interface SharedPrefUtils {
// Single
<T> String saveObject(String key, T object);
<T> String saveObject(T object); // auto generate key
<T> T getObject(String key, Class<T> clazz);
<T> List<T> getObjects(Class<T> clazz);
// Common
void reset();
void remove(String key);
}

View File

@ -0,0 +1,9 @@
package one.nem.kidshift.utils.factory;
import dagger.assisted.AssistedFactory;
import one.nem.kidshift.utils.impl.SharedPrefUtilsImpl;
@AssistedFactory
public interface SharedPrefUtilsFactory {
SharedPrefUtilsImpl create(String name);
}

View File

@ -10,18 +10,21 @@ import java.util.List;
import javax.inject.Inject;
import one.nem.kidshift.utils.KSLogger;
import one.nem.kidshift.utils.SharedPrefUtils;
import one.nem.kidshift.utils.enums.LogLevelEnum;
import one.nem.kidshift.utils.factory.SharedPrefUtilsFactory;
import one.nem.kidshift.utils.models.LogModel;
public class KSLoggerImpl implements KSLogger {
private ArrayList<LogModel> logs = new ArrayList<LogModel>();
private ArrayList<String> tags = new ArrayList<String>();
private SharedPrefUtils sharedPrefUtils;
@Inject
public KSLoggerImpl() {
public KSLoggerImpl(SharedPrefUtilsFactory sharedPrefUtilsFactory) {
tags.add("UNTAGGED");
this.sharedPrefUtils = sharedPrefUtilsFactory.create("KSLogger");
}
public KSLoggerImpl(String tag) {
@ -54,7 +57,7 @@ public class KSLoggerImpl implements KSLogger {
@Override
public List<LogModel> getHistory() {
return logs;
return sharedPrefUtils.getObjects(LogModel.class);
}
@Override
@ -93,7 +96,7 @@ public class KSLoggerImpl implements KSLogger {
}
private void addLog(LogModel log) {
logs.add(log);
sharedPrefUtils.saveObject(log);
}
private void outputLog(LogModel log) {

View File

@ -0,0 +1,77 @@
package one.nem.kidshift.utils.impl;
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import dagger.assisted.Assisted;
import dagger.assisted.AssistedInject;
import dagger.hilt.android.qualifiers.ApplicationContext;
import one.nem.kidshift.utils.SharedPrefUtils;
public class SharedPrefUtilsImpl implements SharedPrefUtils {
private final Context applicationContext;
private final String name;
private final Gson gson;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
@AssistedInject
public SharedPrefUtilsImpl(@ApplicationContext Context applicationContext, @Assisted String name) {
this.applicationContext = applicationContext;
this.name = name;
this.gson = new Gson();
// Get the shared preferences
sharedPreferences = applicationContext.getSharedPreferences(name, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
@Override
public <T> String saveObject(String key, T object) {
String json = gson.toJson(object);
editor.putString(key, json).apply();
return key;
}
@Override
public <T> String saveObject(T object) {
String key = String.valueOf(sharedPreferences.getAll().size());
return saveObject(key, object);
}
@Override
public <T> T getObject(String key, Class<T> clazz) {
String json = sharedPreferences.getString(key, null);
return gson.fromJson(json, clazz);
}
@Override
public <T> List<T> getObjects(Class<T> clazz) {
// SharedPreferenceの中身を全て取得
List<T> list = new ArrayList<>();
for (String key : sharedPreferences.getAll().keySet()) {
String json = sharedPreferences.getString(key, null);
T object = gson.fromJson(json, clazz);
list.add(object);
}
return list;
}
@Override
public void reset() {
editor.clear().apply();
}
@Override
public void remove(String key) {
editor.remove(key).apply();
}
}