From acb5051eecaaa1646191f7236a22527937628f67 Mon Sep 17 00:00:00 2001 From: olegivo Date: Sat, 15 Aug 2020 11:46:13 -0500 Subject: [PATCH] Konvert BaseActivity Co-authored-by: Alinson S. Xavier --- .../androidbase/activities/BaseActivity.java | 129 ------------------ .../androidbase/activities/BaseActivity.kt | 96 +++++++++++++ 2 files changed, 96 insertions(+), 129 deletions(-) delete mode 100644 android/android-base/src/main/java/org/isoron/androidbase/activities/BaseActivity.java create mode 100644 android/android-base/src/main/java/org/isoron/androidbase/activities/BaseActivity.kt diff --git a/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseActivity.java b/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseActivity.java deleted file mode 100644 index 2dea22e50..000000000 --- a/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseActivity.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.androidbase.activities; - -import android.content.*; -import android.os.*; - -import androidx.annotation.Nullable; -import androidx.appcompat.app.*; -import android.view.*; - -import org.isoron.androidbase.*; - -import static android.R.anim.fade_in; -import static android.R.anim.fade_out; - -/** - * Base class for all activities in the application. - *

- * This class delegates the responsibilities of an Android activity to other - * classes. For example, callbacks related to menus are forwarded to a {@link - * BaseMenu}, while callbacks related to activity results are forwarded to a - * {@link BaseScreen}. - *

- * A BaseActivity also installs an {@link java.lang.Thread.UncaughtExceptionHandler} - * to the main thread. By default, this handler is an instance of - * BaseExceptionHandler, which logs the exception to the disk before the application - * crashes. To the default handler, you should override the method - * getExceptionHandler. - */ -abstract public class BaseActivity extends AppCompatActivity -{ - @Nullable - private BaseMenu baseMenu; - - @Nullable - private BaseScreen screen; - - @Override - public boolean onCreateOptionsMenu(@Nullable Menu menu) - { - if (menu == null) return true; - if (baseMenu == null) return true; - baseMenu.onCreate(getMenuInflater(), menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(@Nullable MenuItem item) - { - if (item == null) return false; - if (baseMenu == null) return false; - return baseMenu.onItemSelected(item); - } - - public void restartWithFade(Class cls) - { - new Handler().postDelayed(() -> - { - finish(); - overridePendingTransition(fade_in, fade_out); - startActivity(new Intent(this, cls)); - - }, 500); // HACK: Let the menu disappear first - } - - public void setBaseMenu(@Nullable BaseMenu baseMenu) - { - this.baseMenu = baseMenu; - } - - public void setScreen(@Nullable BaseScreen screen) - { - this.screen = screen; - } - - public void showDialog(AppCompatDialogFragment dialog, String tag) - { - dialog.show(getSupportFragmentManager(), tag); - } - - public void showDialog(AppCompatDialog dialog) - { - dialog.show(); - } - - @Override - protected void onActivityResult(int request, int result, Intent data) - { - if (screen == null) super.onActivityResult(request, result, data); - else screen.onResult(request, result, data); - } - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - Thread.setDefaultUncaughtExceptionHandler(getExceptionHandler()); - } - - protected Thread.UncaughtExceptionHandler getExceptionHandler() - { - return new BaseExceptionHandler(this); - } - - @Override - protected void onResume() - { - super.onResume(); - if(screen != null) screen.reattachDialogs(); - } -} diff --git a/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseActivity.kt b/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseActivity.kt new file mode 100644 index 000000000..efb68c278 --- /dev/null +++ b/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseActivity.kt @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.androidbase.activities + +import android.R.anim +import android.content.* +import android.os.* +import android.view.* +import androidx.appcompat.app.* +import org.isoron.androidbase.* + +/** + * Base class for all activities in the application. + * + * This class delegates the responsibilities of an Android activity to other classes. For example, + * callbacks related to menus are forwarded to a []BaseMenu], while callbacks related to activity + * results are forwarded to a [BaseScreen]. + * + * + * A BaseActivity also installs an [java.lang.Thread.UncaughtExceptionHandler] to the main thread. + * By default, this handler is an instance of BaseExceptionHandler, which logs the exception to the + * disk before the application crashes. To the default handler, you should override the method + * getExceptionHandler. + */ +abstract class BaseActivity : AppCompatActivity() { + private var baseMenu: BaseMenu? = null + private var screen: BaseScreen? = null + + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + if (menu != null) baseMenu?.onCreate(menuInflater, menu) + return true + } + + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + if (item == null) return false + return baseMenu?.onItemSelected(item) ?: false + } + + fun restartWithFade(cls: Class<*>?) { + Handler().postDelayed({ + finish() + overridePendingTransition(anim.fade_in, anim.fade_out) + startActivity(Intent(this, cls)) + }, 500) // HACK: Let the menu disappear first + } + + fun setBaseMenu(baseMenu: BaseMenu?) { + this.baseMenu = baseMenu + } + + fun setScreen(screen: BaseScreen?) { + this.screen = screen + } + + fun showDialog(dialog: AppCompatDialogFragment, tag: String?) { + dialog.show(supportFragmentManager, tag) + } + + fun showDialog(dialog: AppCompatDialog) { + dialog.show() + } + + override fun onActivityResult(request: Int, result: Int, data: Intent?) { + val screen = screen + if(screen == null) super.onActivityResult(request, result, data) + else screen.onResult(request, result, data) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + Thread.setDefaultUncaughtExceptionHandler(getExceptionHandler()) + } + + private fun getExceptionHandler() = BaseExceptionHandler(this) + + override fun onResume() { + super.onResume() + screen?.reattachDialogs() + } +} \ No newline at end of file