From cac566ecf65fcc5d645f50e07ffd8cdf2d2110e3 Mon Sep 17 00:00:00 2001 From: niushuai233 Date: Thu, 27 Oct 2022 17:40:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=97=A5=E5=BF=97=E7=BA=A7=E5=88=AB?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/devcontrol/db/util/DBHelper.java | 22 +++++++++++ .../ui/nav/setup/NavSetUpFragment.java | 39 ++++++++++++------- 2 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java 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 new file mode 100644 index 0000000..cccd349 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java @@ -0,0 +1,22 @@ +package cc.niushuai.project.devcontrol.db.util; + +import java.util.List; + +import cc.niushuai.project.devcontrol.db.DB; +import cc.niushuai.project.devcontrol.db.entity.SysConfig; + +/** + * 抽取的公共方法 + * + * @author niushuai233 + * @date 2022/10/27 17:23 + */ +public class DBHelper { + + private static final String WHERE_KEY = "where key = ?"; + + public static List configListByKey(String key) { + + return DB.getSysConfigDao().queryRaw(WHERE_KEY, key); + } +} 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 8e776c4..bae4656 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,19 +5,20 @@ 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 cc.niushuai.project.devcontrol.base.ui.BaseFragment; -import cc.niushuai.project.devcontrol.base.util.Global; 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; @@ -56,7 +57,9 @@ 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(); + dealConfig(Keys.SETUP_LOG_LEVEL, level); } @Override @@ -69,6 +72,7 @@ public class NavSetUpFragment extends BaseFragment { } + private void setupBackupClickListener(View view) { } @@ -86,23 +90,32 @@ public class NavSetUpFragment extends BaseFragment { Boolean checked = binding.setupLogSwitchSwitch.isChecked(); XLog.LOG_SWITCH = checked; - List list = DB.getSysConfigDao().queryRaw("where key = ?", Keys.SETUP_LOG_SWITCH); - if (CollUtil.isNotEmpty(list)) { - // 已存在 更新操作 - SysConfig updateEntity = list.get(0); - updateEntity.setValue(checked.toString()); - updateEntity.setUpdateTime(DateUtil.now()); - - DB.getSysConfigDao().update(updateEntity); - return; - } - // 新增操作 - DB.getSysConfigDao().insert(new SysConfig(IdWorker.getNextId(), Keys.SETUP_LOG_SWITCH, checked.toString(), DateUtil.now(), DateUtil.now())); + dealConfig(Keys.SETUP_LOG_SWITCH, checked.toString()); } + private void setupKeepDayClickListener(View view) { } + private void dealConfig(String key, String level) { + List list = DBHelper.configListByKey(key); + if (CollUtil.isNotEmpty(list)) { + configUpdate(level, list.get(0)); + return; + } + configInsert(key, level); + } + + 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); + } @Override public void onDestroyView() {