Convert to kotlin
This commit is contained in:
1 parent
0fae5a128f
commit
64cbd189ad
2 files changed
+180
-213
No files matched your search
@@ -31,7 +31,6 @@ import android.text.TextUtils
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.core.view.GravityCompat
|
||||
@@ -67,7 +66,9 @@ import com.hippo.util.getClipboardManager
|
||||
import com.hippo.util.getUrlFromClipboard
|
||||
import com.hippo.yorozuya.IOUtils
|
||||
import eu.kanade.tachiyomi.util.lang.launchUI
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
@@ -332,13 +333,13 @@ class MainActivity : EhActivity() {
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
lifecycleScope.launchUI {
|
||||
lifecycleScope.launch {
|
||||
delay(300)
|
||||
checkClipboardUrl()
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkClipboardUrl() {
|
||||
private suspend fun checkClipboardUrl() {
|
||||
val text = this.getClipboardManager().getUrlFromClipboard(this)
|
||||
val hashCode = text?.hashCode() ?: 0
|
||||
if (text != null && hashCode != 0 && Settings.getClipboardTextHashCode() != hashCode) {
|
||||
@@ -361,15 +362,17 @@ class MainActivity : EhActivity() {
|
||||
launch = { navController.navigate(R.id.progressScene, args) }
|
||||
}
|
||||
launch?.let {
|
||||
val snackbar = Snackbar.make(
|
||||
binding.drawView,
|
||||
R.string.clipboard_gallery_url_snack_message,
|
||||
Snackbar.LENGTH_SHORT
|
||||
)
|
||||
snackbar.setAction(R.string.clipboard_gallery_url_snack_action) {
|
||||
it()
|
||||
withUIContext {
|
||||
val snackbar = Snackbar.make(
|
||||
binding.drawView,
|
||||
R.string.clipboard_gallery_url_snack_message,
|
||||
Snackbar.LENGTH_SHORT
|
||||
)
|
||||
snackbar.setAction(R.string.clipboard_gallery_url_snack_action) {
|
||||
it()
|
||||
}
|
||||
snackbar.show()
|
||||
}
|
||||
snackbar.show()
|
||||
}
|
||||
}
|
||||
Settings.putClipboardTextHashCode(hashCode)
|
||||
|
||||
@@ -13,279 +13,243 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.hippo.ehviewer.ui.scene
|
||||
|
||||
package com.hippo.ehviewer.ui.scene;
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.res.Resources
|
||||
import android.content.res.Resources.Theme
|
||||
import android.os.Bundle
|
||||
import android.os.Parcelable
|
||||
import android.util.SparseArray
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.core.view.GravityCompat
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.core.view.WindowInsetsControllerCompat
|
||||
import androidx.drawerlayout.widget.DrawerLayout
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.navigation.NavOptions
|
||||
import androidx.navigation.fragment.NavHostFragment
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.ui.MainActivity
|
||||
import rikka.core.res.resolveDrawable
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.util.SparseArray;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.IdRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.core.view.GravityCompat;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
import androidx.core.view.WindowInsetsControllerCompat;
|
||||
import androidx.drawerlayout.widget.DrawerLayout;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.navigation.NavOptions;
|
||||
import androidx.navigation.fragment.NavHostFragment;
|
||||
|
||||
import com.hippo.ehviewer.R;
|
||||
import com.hippo.ehviewer.ui.MainActivity;
|
||||
|
||||
import rikka.core.res.ResourcesKt;
|
||||
|
||||
public abstract class BaseScene extends Fragment {
|
||||
|
||||
public static final int LENGTH_SHORT = 0;
|
||||
public static final int LENGTH_LONG = 1;
|
||||
|
||||
public static final String KEY_DRAWER_VIEW_STATE =
|
||||
"com.hippo.ehviewer.ui.scene.BaseScene:DRAWER_VIEW_STATE";
|
||||
|
||||
private WindowInsetsControllerCompat insetsController;
|
||||
|
||||
@Nullable
|
||||
private View drawerView;
|
||||
@Nullable
|
||||
private SparseArray<Parcelable> drawerViewState;
|
||||
|
||||
public void addAboveSnackView(View view) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
((MainActivity) activity).addAboveSnackView(view);
|
||||
abstract class BaseScene : Fragment() {
|
||||
private var insetsController: WindowInsetsControllerCompat? = null
|
||||
private var drawerView: View? = null
|
||||
private var drawerViewState: SparseArray<Parcelable?>? = null
|
||||
fun addAboveSnackView(view: View?) {
|
||||
val activity = activity
|
||||
if (activity is MainActivity) {
|
||||
activity.addAboveSnackView(view)
|
||||
}
|
||||
}
|
||||
|
||||
public void removeAboveSnackView(View view) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
((MainActivity) activity).removeAboveSnackView(view);
|
||||
fun removeAboveSnackView(view: View?) {
|
||||
val activity = activity
|
||||
if (activity is MainActivity) {
|
||||
activity.removeAboveSnackView(view)
|
||||
}
|
||||
}
|
||||
|
||||
public void setDrawerLockMode(int lockMode, int edgeGravity) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
((MainActivity) activity).setDrawerLockMode(lockMode, edgeGravity);
|
||||
fun setDrawerLockMode(lockMode: Int, edgeGravity: Int) {
|
||||
val activity = activity
|
||||
if (activity is MainActivity) {
|
||||
activity.setDrawerLockMode(lockMode, edgeGravity)
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getDrawerLockMode(int edgeGravity) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
return ((MainActivity) activity).getDrawerLockMode(edgeGravity);
|
||||
}
|
||||
return null;
|
||||
fun getDrawerLockMode(edgeGravity: Int): Int? {
|
||||
val activity = activity
|
||||
return if (activity is MainActivity) {
|
||||
activity.getDrawerLockMode(edgeGravity)
|
||||
} else null
|
||||
}
|
||||
|
||||
public void openDrawer(int drawerGravity) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
((MainActivity) activity).openDrawer(drawerGravity);
|
||||
fun openDrawer(drawerGravity: Int) {
|
||||
val activity = activity
|
||||
if (activity is MainActivity) {
|
||||
activity.openDrawer(drawerGravity)
|
||||
}
|
||||
}
|
||||
|
||||
public void closeDrawer(int drawerGravity) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
((MainActivity) activity).closeDrawer(drawerGravity);
|
||||
fun closeDrawer(drawerGravity: Int) {
|
||||
val activity = activity
|
||||
if (activity is MainActivity) {
|
||||
activity.closeDrawer(drawerGravity)
|
||||
}
|
||||
}
|
||||
|
||||
public void toggleDrawer(int drawerGravity) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
((MainActivity) activity).toggleDrawer(drawerGravity);
|
||||
fun toggleDrawer(drawerGravity: Int) {
|
||||
val activity = activity
|
||||
if (activity is MainActivity) {
|
||||
activity.toggleDrawer(drawerGravity)
|
||||
}
|
||||
}
|
||||
|
||||
public void showTip(CharSequence message, int length) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
((MainActivity) activity).showTip(message, length);
|
||||
fun showTip(message: CharSequence?, length: Int) {
|
||||
val activity = activity
|
||||
if (activity is MainActivity) {
|
||||
activity.showTip(message!!, length)
|
||||
}
|
||||
}
|
||||
|
||||
public void showTip(@StringRes int id, int length) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
((MainActivity) activity).showTip(id, length);
|
||||
fun showTip(@StringRes id: Int, length: Int) {
|
||||
val activity = activity
|
||||
if (activity is MainActivity) {
|
||||
activity.showTip(id, length)
|
||||
}
|
||||
}
|
||||
|
||||
public boolean needShowLeftDrawer() {
|
||||
return true;
|
||||
open fun needShowLeftDrawer(): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
public void recreateDrawerView() {
|
||||
MainActivity activity = getMainActivity();
|
||||
if (activity != null) {
|
||||
activity.createDrawerView(this);
|
||||
}
|
||||
fun recreateDrawerView() {
|
||||
val activity = mainActivity
|
||||
activity?.createDrawerView(this)
|
||||
}
|
||||
|
||||
public final View createDrawerView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
drawerView = onCreateDrawerView(inflater, container, savedInstanceState);
|
||||
|
||||
fun createDrawerView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?, savedInstanceState: Bundle?
|
||||
): View? {
|
||||
drawerView = onCreateDrawerView(inflater, container, savedInstanceState)
|
||||
if (drawerView != null) {
|
||||
SparseArray<Parcelable> saved = drawerViewState;
|
||||
var saved = drawerViewState
|
||||
if (saved == null && savedInstanceState != null) {
|
||||
saved = savedInstanceState.getSparseParcelableArray(KEY_DRAWER_VIEW_STATE);
|
||||
saved = savedInstanceState.getSparseParcelableArray(KEY_DRAWER_VIEW_STATE)
|
||||
}
|
||||
if (saved != null) {
|
||||
drawerView.restoreHierarchyState(saved);
|
||||
drawerView!!.restoreHierarchyState(saved)
|
||||
}
|
||||
}
|
||||
|
||||
return drawerView;
|
||||
return drawerView
|
||||
}
|
||||
|
||||
public View onCreateDrawerView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
return null;
|
||||
open fun onCreateDrawerView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?, savedInstanceState: Bundle?
|
||||
): View? {
|
||||
return null
|
||||
}
|
||||
|
||||
public final void destroyDrawerView() {
|
||||
private fun destroyDrawerView() {
|
||||
if (drawerView != null) {
|
||||
drawerViewState = new SparseArray<>();
|
||||
drawerView.saveHierarchyState(drawerViewState);
|
||||
drawerViewState = SparseArray()
|
||||
drawerView!!.saveHierarchyState(drawerViewState)
|
||||
}
|
||||
|
||||
onDestroyDrawerView();
|
||||
|
||||
drawerView = null;
|
||||
onDestroyDrawerView()
|
||||
drawerView = null
|
||||
}
|
||||
|
||||
public void onDestroyDrawerView() {
|
||||
}
|
||||
open fun onDestroyDrawerView() {}
|
||||
|
||||
@SuppressLint("RtlHardcoded")
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
view.setBackground(ResourcesKt.resolveDrawable(requireActivity().getTheme(), android.R.attr.windowBackground));
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
view.background = requireActivity().theme.resolveDrawable(android.R.attr.windowBackground)
|
||||
|
||||
// Update left drawer locked state
|
||||
if (needShowLeftDrawer()) {
|
||||
setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START);
|
||||
setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START)
|
||||
} else {
|
||||
setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.START);
|
||||
setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.START)
|
||||
}
|
||||
|
||||
// Hide soft ime
|
||||
hideSoftInput();
|
||||
|
||||
getMainActivity().createDrawerView(this);
|
||||
hideSoftInput()
|
||||
mainActivity!!.createDrawerView(this)
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
destroyDrawerView();
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
destroyDrawerView()
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Resources getResourcesOrNull() {
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
return context.getResources();
|
||||
} else {
|
||||
return null;
|
||||
val resourcesOrNull: Resources?
|
||||
get() {
|
||||
val context = context
|
||||
return context?.resources
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public MainActivity getMainActivity() {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity instanceof MainActivity) {
|
||||
return (MainActivity) activity;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void hideSoftInput() {
|
||||
var insetsController = getInsetsController();
|
||||
if (insetsController != null) {
|
||||
insetsController.hide(WindowInsetsCompat.Type.ime());
|
||||
}
|
||||
}
|
||||
|
||||
public void showSoftInput(@Nullable View view) {
|
||||
if (view != null) {
|
||||
view.requestFocus();
|
||||
view.post(() -> {
|
||||
var insetsController = getInsetsController();
|
||||
if (insetsController != null) {
|
||||
insetsController.show(WindowInsetsCompat.Type.ime());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public WindowInsetsControllerCompat getInsetsController() {
|
||||
if (insetsController == null) {
|
||||
var activity = getActivity();
|
||||
if (activity != null) {
|
||||
insetsController = ViewCompat.getWindowInsetsController(activity.getWindow().getDecorView());
|
||||
val mainActivity: MainActivity?
|
||||
get() {
|
||||
val activity = activity
|
||||
return if (activity is MainActivity) {
|
||||
activity
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
return insetsController;
|
||||
|
||||
fun hideSoftInput() {
|
||||
val insetsController = getInsetsController()
|
||||
insetsController?.hide(WindowInsetsCompat.Type.ime())
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
insetsController = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
if (drawerView != null) {
|
||||
drawerViewState = new SparseArray<>();
|
||||
drawerView.saveHierarchyState(drawerViewState);
|
||||
outState.putSparseParcelableArray(KEY_DRAWER_VIEW_STATE, drawerViewState);
|
||||
fun showSoftInput(view: View?) {
|
||||
if (view != null) {
|
||||
view.requestFocus()
|
||||
view.post(Runnable {
|
||||
val insetsController = getInsetsController()
|
||||
insetsController?.show(WindowInsetsCompat.Type.ime())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public Resources.Theme getTheme() {
|
||||
return requireActivity().getTheme();
|
||||
private fun getInsetsController(): WindowInsetsControllerCompat? {
|
||||
if (insetsController == null) {
|
||||
val activity = activity
|
||||
if (activity != null) {
|
||||
insetsController = ViewCompat.getWindowInsetsController(activity.window.decorView)
|
||||
}
|
||||
}
|
||||
return insetsController
|
||||
}
|
||||
|
||||
public void navigate(int id, Bundle args) {
|
||||
navigate(id, args, false);
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
insetsController = null
|
||||
}
|
||||
|
||||
public void navigate(int id, Bundle args, boolean singleTop) {
|
||||
var options = new NavOptions.Builder().setLaunchSingleTop(singleTop)
|
||||
.setEnterAnim(R.anim.scene_open_enter).setExitAnim(R.anim.scene_open_exit)
|
||||
.setPopEnterAnim(R.anim.scene_close_enter).setPopExitAnim(R.anim.scene_close_exit)
|
||||
.build();
|
||||
NavHostFragment.findNavController(this).navigate(id, args, options);
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
if (drawerView != null) {
|
||||
drawerViewState = SparseArray()
|
||||
drawerView!!.saveHierarchyState(drawerViewState)
|
||||
outState.putSparseParcelableArray(KEY_DRAWER_VIEW_STATE, drawerViewState)
|
||||
}
|
||||
}
|
||||
|
||||
public void navigateToTop() {
|
||||
var navController = NavHostFragment.findNavController(this);
|
||||
var id = navController.getGraph().getStartDestinationId();
|
||||
var options = new NavOptions.Builder()
|
||||
.setEnterAnim(R.anim.scene_open_enter).setExitAnim(R.anim.scene_open_exit)
|
||||
.setPopEnterAnim(R.anim.scene_close_enter).setPopExitAnim(R.anim.scene_close_exit)
|
||||
.setPopUpTo(id, true)
|
||||
.build();
|
||||
navController.navigate(id, null, options);
|
||||
val theme: Theme
|
||||
get() = requireActivity().theme
|
||||
|
||||
@JvmOverloads
|
||||
fun navigate(id: Int, args: Bundle?, singleTop: Boolean = false) {
|
||||
val options: NavOptions = NavOptions.Builder().setLaunchSingleTop(singleTop)
|
||||
.setEnterAnim(R.anim.scene_open_enter).setExitAnim(R.anim.scene_open_exit)
|
||||
.setPopEnterAnim(R.anim.scene_close_enter).setPopExitAnim(R.anim.scene_close_exit)
|
||||
.build()
|
||||
NavHostFragment.findNavController(this).navigate(id, args, options)
|
||||
}
|
||||
}
|
||||
|
||||
fun navigateToTop() {
|
||||
val navController = NavHostFragment.findNavController(this)
|
||||
val id = navController.graph.startDestinationId
|
||||
val options: NavOptions = NavOptions.Builder()
|
||||
.setEnterAnim(R.anim.scene_open_enter).setExitAnim(R.anim.scene_open_exit)
|
||||
.setPopEnterAnim(R.anim.scene_close_enter).setPopExitAnim(R.anim.scene_close_exit)
|
||||
.setPopUpTo(id, true)
|
||||
.build()
|
||||
navController.navigate(id, null, options)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val LENGTH_SHORT = 0
|
||||
const val LENGTH_LONG = 1
|
||||
const val KEY_DRAWER_VIEW_STATE = "com.hippo.ehviewer.ui.scene.BaseScene:DRAWER_VIEW_STATE"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user