feat: 日志保留天数完成

This commit is contained in:
niushuai233 2022-10-28 11:53:39 +08:00
parent d03612749c
commit 3a817c0fc5
7 changed files with 176 additions and 45 deletions

View File

@ -34,9 +34,9 @@ public class App {
// 初始化日志相关内容
initLog(activity);
XLog.i(Keys.Tag.APP_INIT, "====================================================================");
XLog.i(Keys.Tag.APP_INIT, "{}====================================", System.lineSeparator());
XLog.i(Keys.Tag.APP_INIT, "应用{}启动", BuildConfig.APP_NAME);
XLog.i(Keys.Tag.APP_INIT, "====================================================================");
XLog.i(Keys.Tag.APP_INIT, "====================================");
// 检查root权限
checkRoot(activity);

View File

@ -20,5 +20,6 @@ public interface Keys {
String MY_OPEN_HELPER = "MyOpenHelper";
String APP_INIT = "AppInit";
String KEEP_DAY_SELECT = "KeepDaySelect";
}
}

View File

@ -2,9 +2,11 @@ package cc.niushuai.project.devcontrol.db.util;
import java.util.List;
import cc.niushuai.project.devcontrol.base.util.IdWorker;
import cc.niushuai.project.devcontrol.db.DB;
import cc.niushuai.project.devcontrol.db.entity.SysConfig;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
/**
* 抽取的公共方法
@ -14,16 +16,36 @@ import cn.hutool.core.collection.CollUtil;
*/
public class DBHelper {
private static final String WHERE_KEY = "where key = ?";
private static final String CONFIG_WHERE_KEY = "where key = ?";
public static void configDeal(String key, String value) {
List<SysConfig> list = configListByKey(key);
if (CollUtil.isNotEmpty(list)) {
configUpdate(value, list.get(0));
return;
}
configInsert(key, value);
}
public static void configInsert(String key, String value) {
DB.getSysConfigDao().insert(new SysConfig(IdWorker.getNextId(), key, value, DateUtil.now(), DateUtil.now()));
}
public static void configUpdate(String level, SysConfig updateEntity) {
updateEntity.setValue(level);
updateEntity.setUpdateTime(DateUtil.now());
DB.getSysConfigDao().update(updateEntity);
}
public static List<SysConfig> configListByKey(String key) {
return DB.getSysConfigDao().queryRaw(WHERE_KEY, key);
return DB.getSysConfigDao().queryRaw(CONFIG_WHERE_KEY, key);
}
public static SysConfig configOneByKey(String key) {
List<SysConfig> list = DB.getSysConfigDao().queryRaw(WHERE_KEY, key);
List<SysConfig> list = DB.getSysConfigDao().queryRaw(CONFIG_WHERE_KEY, key);
return CollUtil.isEmpty(list) ? null : list.get(0);
}
}

View File

@ -0,0 +1,79 @@
package cc.niushuai.project.devcontrol.ui.nav.setup;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.NumberPicker;
import android.widget.TextView;
import androidx.fragment.app.DialogFragment;
import cc.niushuai.project.devcontrol.R;
import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.base.util.XLog;
import cc.niushuai.project.devcontrol.databinding.LayoutLogKeepDayBinding;
import cc.niushuai.project.devcontrol.db.DB;
import cc.niushuai.project.devcontrol.db.util.DBHelper;
/**
* 日志保留天数
*
* @author niushuai233
* @date 2022/10/28 11:09
*/
public class KeepDayDialogFragment extends DialogFragment {
private LayoutLogKeepDayBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = LayoutLogKeepDayBinding.inflate(getLayoutInflater());
this.init();
this.addListener();
return binding.getRoot();
}
private void init() {
this.initNumberPicker();
}
private void addListener() {
binding.layoutLogKeepDayConfirm.setOnClickListener(v -> {
String keepDayPickerValue = binding.keepDayPicker.getValue() + "";
DBHelper.configDeal(Keys.SETUP_LOG_KEEP_DAY, keepDayPickerValue);
XLog.v(Keys.Tag.KEEP_DAY_SELECT, "更新日志存储天数为: {}", keepDayPickerValue);
((TextView)getActivity().findViewById(R.id.setup_log_keep_day_display)).setText(keepDayPickerValue);
this.dismiss();
});
binding.layoutLogKeepDayCancel.setOnClickListener(v -> this.dismiss());
}
private void initNumberPicker() {
binding.keepDayPicker.setMinValue(1);
binding.keepDayPicker.setMaxValue(90);
binding.keepDayPicker.setValue(XLog.LOG_KEEP_DAY);
binding.keepDayPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
binding.keepDayPicker.setFormatter(value -> value + "");
binding.keepDayPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
XLog.v(Keys.Tag.KEEP_DAY_SELECT, "保留天数 ==> 旧值: {}, 新值: {}", oldVal, newVal);
});
binding.keepDayPicker.setOnScrollListener((_view, scrollState) -> {
XLog.v(Keys.Tag.KEEP_DAY_SELECT, "保留天数 ==> 滑动状态: {}", scrollState);
});
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
binding = null;
}
}

View File

@ -5,23 +5,15 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import java.util.List;
import android.widget.Spinner;
import cc.niushuai.project.devcontrol.R;
import cc.niushuai.project.devcontrol.base.ui.BaseFragment;
import cc.niushuai.project.devcontrol.base.util.IdWorker;
import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.base.util.XLog;
import cc.niushuai.project.devcontrol.databinding.MainNavFragmentSetUpBinding;
import cc.niushuai.project.devcontrol.db.DB;
import cc.niushuai.project.devcontrol.db.entity.SysConfig;
import cc.niushuai.project.devcontrol.db.util.DBHelper;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
public class NavSetUpFragment extends BaseFragment {
@ -46,14 +38,14 @@ public class NavSetUpFragment extends BaseFragment {
SysConfig switchConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_SWITCH);
if (null == switchConfig) {
dealConfig(Keys.SETUP_LOG_SWITCH, XLog.LOG_SWITCH.toString());
DBHelper.configDeal(Keys.SETUP_LOG_SWITCH, XLog.LOG_SWITCH.toString());
} else {
binding.setupLogSwitchSwitch.setChecked(Boolean.parseBoolean(switchConfig.getValue()));
}
SysConfig levelConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_LEVEL);
if (null == levelConfig) {
dealConfig(Keys.SETUP_LOG_LEVEL, XLog.SET_ROOT_LEVEL_NAME);
DBHelper.configDeal(Keys.SETUP_LOG_LEVEL, XLog.SET_ROOT_LEVEL_NAME);
} else {
String[] logLevel = getResources().getStringArray(R.array.logLevel);
for (int i = 0; i < logLevel.length; i++) {
@ -67,7 +59,7 @@ public class NavSetUpFragment extends BaseFragment {
SysConfig keepDayConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_KEEP_DAY);
if (null == keepDayConfig) {
dealConfig(Keys.SETUP_LOG_KEEP_DAY, XLog.LOG_KEEP_DAY.toString());
DBHelper.configDeal(Keys.SETUP_LOG_KEEP_DAY, XLog.LOG_KEEP_DAY.toString());
} else {
binding.setupLogKeepDayDisplay.setText(keepDayConfig.getValue());
}
@ -86,19 +78,17 @@ public class NavSetUpFragment extends BaseFragment {
binding.setupLogLevelSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String level = ((TextView) view).getText().toString();
String level = ((Spinner) parent.findViewById(R.id.setup_log_level_spinner)).getSelectedItem().toString();
dealConfig(Keys.SETUP_LOG_LEVEL, level);
DBHelper.configDeal(Keys.SETUP_LOG_LEVEL, level);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// 日志保留天数点击事件
binding.setupLlLogKeepDay.setOnClickListener(this::setupKeepDayClickListener);
}
@ -119,33 +109,12 @@ public class NavSetUpFragment extends BaseFragment {
Boolean checked = binding.setupLogSwitchSwitch.isChecked();
XLog.LOG_SWITCH = checked;
dealConfig(Keys.SETUP_LOG_SWITCH, checked.toString());
DBHelper.configDeal(Keys.SETUP_LOG_SWITCH, checked.toString());
}
private void setupKeepDayClickListener(View view) {
}
private void dealConfig(String key, String value) {
List<SysConfig> list = DBHelper.configListByKey(key);
if (CollUtil.isNotEmpty(list)) {
configUpdate(value, list.get(0));
return;
}
configInsert(key, value);
}
private void configInsert(String key, String value) {
DB.getSysConfigDao().insert(new SysConfig(IdWorker.getNextId(), key, value, DateUtil.now(), DateUtil.now()));
}
private void configUpdate(String level, SysConfig updateEntity) {
updateEntity.setValue(level);
updateEntity.setUpdateTime(DateUtil.now());
DB.getSysConfigDao().update(updateEntity);
KeepDayDialogFragment keepDayDialogFragment = new KeepDayDialogFragment();
keepDayDialogFragment.show(getActivity().getSupportFragmentManager(), KeepDayDialogFragment.class.getSimpleName());
}
@Override

View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layout_log_keep_day_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<NumberPicker
android:id="@+id/keep_day_picker"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:layout_weight="8" />
<TextView
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="@string/day"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_log_keep_day_ll"
android:gravity="right">
<Button
android:id="@+id/layout_log_keep_day_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel" />
<Button
android:id="@+id/layout_log_keep_day_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/confirm" />
</LinearLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -12,6 +12,12 @@
<string name="open">开启</string>
<string name="close">关闭</string>
<string name="save">保存</string>
<string name="year"></string>
<string name="month"></string>
<string name="day"></string>
<string name="hour"></string>
<string name="minute"></string>
<string name="second"></string>
<!--common end-->
<!--测试字符串 start-->