From 3a817c0fc5e7c53a63c9c0a9177d418e488743ed Mon Sep 17 00:00:00 2001 From: niushuai233 Date: Fri, 28 Oct 2022 11:53:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=97=A5=E5=BF=97=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=A4=A9=E6=95=B0=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../niushuai/project/devcontrol/base/App.java | 4 +- .../project/devcontrol/base/util/Keys.java | 1 + .../project/devcontrol/db/util/DBHelper.java | 28 ++++++- .../ui/nav/setup/KeepDayDialogFragment.java | 79 +++++++++++++++++++ .../ui/nav/setup/NavSetUpFragment.java | 49 +++--------- .../main/res/layout/layout_log_keep_day.xml | 54 +++++++++++++ app/src/main/res/values/strings.xml | 6 ++ 7 files changed, 176 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/setup/KeepDayDialogFragment.java create mode 100644 app/src/main/res/layout/layout_log_keep_day.xml diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java index 39229ed..6617f2b 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java @@ -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); diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java index c49bd1e..12e1569 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java @@ -20,5 +20,6 @@ public interface Keys { String MY_OPEN_HELPER = "MyOpenHelper"; String APP_INIT = "AppInit"; + String KEEP_DAY_SELECT = "KeepDaySelect"; } } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java b/app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java index 79f449b..e052056 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java @@ -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 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 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 list = DB.getSysConfigDao().queryRaw(WHERE_KEY, key); + List list = DB.getSysConfigDao().queryRaw(CONFIG_WHERE_KEY, key); return CollUtil.isEmpty(list) ? null : list.get(0); } } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/setup/KeepDayDialogFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/setup/KeepDayDialogFragment.java new file mode 100644 index 0000000..82095d3 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/setup/KeepDayDialogFragment.java @@ -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; + } +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/setup/NavSetUpFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/setup/NavSetUpFragment.java index 4c491bb..7d1d02b 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/setup/NavSetUpFragment.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/setup/NavSetUpFragment.java @@ -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 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 diff --git a/app/src/main/res/layout/layout_log_keep_day.xml b/app/src/main/res/layout/layout_log_keep_day.xml new file mode 100644 index 0000000..328724f --- /dev/null +++ b/app/src/main/res/layout/layout_log_keep_day.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + +