From 45206b256fffba0355b1092984febb30f3a978d5 Mon Sep 17 00:00:00 2001 From: FongMi Date: Thu, 28 Jul 2022 23:24:28 +0800 Subject: [PATCH] Clean code --- .../main/java/com/fongmi/bear/ApiConfig.java | 51 ++++++++++++------- .../fongmi/bear/ui/activity/HomeActivity.java | 4 +- .../bear/ui/activity/SettingActivity.java | 3 +- .../bear/ui/activity/SplashActivity.java | 2 +- .../java/com/fongmi/bear/utils/Clock.java | 17 +++---- 5 files changed, 44 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/fongmi/bear/ApiConfig.java b/app/src/main/java/com/fongmi/bear/ApiConfig.java index 75b51b17d..d632bfdb5 100644 --- a/app/src/main/java/com/fongmi/bear/ApiConfig.java +++ b/app/src/main/java/com/fongmi/bear/ApiConfig.java @@ -35,13 +35,13 @@ import okhttp3.Response; public class ApiConfig { - private final List ads; - private final List flags; - private final List parses; - private final List lives; - private final List sites; - private final JarLoader loader; - private final Handler handler; + private List ads; + private List flags; + private List parses; + private List lives; + private List sites; + private JarLoader loader; + private Handler handler; private Parse parse; private Site home; @@ -53,23 +53,15 @@ public class ApiConfig { return Loader.INSTANCE; } - public ApiConfig() { + public ApiConfig init() { this.ads = new ArrayList<>(); + this.sites = new ArrayList<>(); + this.lives = new ArrayList<>(); this.flags = new ArrayList<>(); this.parses = new ArrayList<>(); - this.lives = new ArrayList<>(); - this.sites = new ArrayList<>(); this.loader = new JarLoader(); this.handler = new Handler(Looper.getMainLooper()); - } - - public void clear() { - this.ads.clear(); - this.flags.clear(); - this.parses.clear(); - this.lives.clear(); - this.sites.clear(); - this.home = null; + return this; } public void loadConfig(Callback callback) { @@ -189,4 +181,25 @@ public class ApiConfig { this.home.setHome(true); Prefers.putHome(home.getKey()); } + + public ApiConfig clear() { + this.ads.clear(); + this.sites.clear(); + this.lives.clear(); + this.flags.clear(); + this.parses.clear(); + this.home = null; + return this; + } + + public void release() { + this.ads = null; + this.home = null; + this.sites = null; + this.lives = null; + this.flags = null; + this.parses = null; + this.loader = null; + this.handler = null; + } } \ No newline at end of file diff --git a/app/src/main/java/com/fongmi/bear/ui/activity/HomeActivity.java b/app/src/main/java/com/fongmi/bear/ui/activity/HomeActivity.java index efad94c54..35707aa6e 100644 --- a/app/src/main/java/com/fongmi/bear/ui/activity/HomeActivity.java +++ b/app/src/main/java/com/fongmi/bear/ui/activity/HomeActivity.java @@ -162,10 +162,10 @@ public class HomeActivity extends BaseActivity implements VodPresenter.OnClickLi @Override protected void onDestroy() { - ApiConfig.get().clear(); + ApiConfig.get().release(); Players.get().release(); + Clock.get().release(); Server.get().stop(); super.onDestroy(); - Clock.destroy(); } } \ No newline at end of file diff --git a/app/src/main/java/com/fongmi/bear/ui/activity/SettingActivity.java b/app/src/main/java/com/fongmi/bear/ui/activity/SettingActivity.java index ae6bf66ce..a75f84bff 100644 --- a/app/src/main/java/com/fongmi/bear/ui/activity/SettingActivity.java +++ b/app/src/main/java/com/fongmi/bear/ui/activity/SettingActivity.java @@ -70,7 +70,6 @@ public class SettingActivity extends BaseActivity { Prefers.putUrl(bindingDialog.text.getText().toString().trim()); mBinding.url.setText(Prefers.getUrl()); Notify.progress(this); - ApiConfig.get().clear(); checkUrl(); }); bindingDialog.text.setOnEditorActionListener((textView, actionId, event) -> { @@ -90,7 +89,7 @@ public class SettingActivity extends BaseActivity { } private void loadConfig() { - ApiConfig.get().loadConfig(new Callback() { + ApiConfig.get().clear().loadConfig(new Callback() { @Override public void success() { mBinding.home.setText(ApiConfig.get().getHome().getName()); diff --git a/app/src/main/java/com/fongmi/bear/ui/activity/SplashActivity.java b/app/src/main/java/com/fongmi/bear/ui/activity/SplashActivity.java index ec758296c..c0bae5e9b 100644 --- a/app/src/main/java/com/fongmi/bear/ui/activity/SplashActivity.java +++ b/app/src/main/java/com/fongmi/bear/ui/activity/SplashActivity.java @@ -39,7 +39,7 @@ public class SplashActivity extends BaseActivity { } private void loadConfig() { - ApiConfig.get().loadConfig(new Callback() { + ApiConfig.get().init().loadConfig(new Callback() { @Override public void success() { HomeActivity.start(getActivity()); diff --git a/app/src/main/java/com/fongmi/bear/utils/Clock.java b/app/src/main/java/com/fongmi/bear/utils/Clock.java index 69d7f2a65..4b9a4931c 100644 --- a/app/src/main/java/com/fongmi/bear/utils/Clock.java +++ b/app/src/main/java/com/fongmi/bear/utils/Clock.java @@ -12,8 +12,8 @@ import java.util.TimerTask; public class Clock { - private final SimpleDateFormat formatter; - private final Handler handler; + private SimpleDateFormat formatter; + private Handler handler; private Timer timer; private static class Loader { @@ -24,20 +24,16 @@ public class Clock { return Loader.INSTANCE; } - public Clock() { + public void init() { this.formatter = new SimpleDateFormat("MM/dd HH:mm:ss", Locale.getDefault()); this.handler = new Handler(Looper.getMainLooper()); } public static void start(TextView view) { - get().cancel(); + get().init(); get().run(view); } - public static void destroy() { - get().cancel(); - } - private void run(TextView view) { timer = new Timer(); timer.schedule(new TimerTask() { @@ -48,7 +44,10 @@ public class Clock { }, 0, 1000); } - private void cancel() { + public void release() { if (timer != null) timer.cancel(); + formatter = null; + handler = null; + timer = null; } }