From 99a7a8c0a5305a1d745930493b61119c1fca2ec1 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Tue, 14 Nov 2023 10:30:08 +0100 Subject: [PATCH] Use extensions Signed-off-by: alperozturk --- .../client/editimage/EditImageActivity.kt | 35 ++++----------- .../utils/extensions/ResourcesExtensions.kt | 45 +++++++++++++++++++ .../utils/extensions/ViewExtensions.kt | 30 +++++++++++++ 3 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/utils/extensions/ResourcesExtensions.kt create mode 100644 app/src/main/java/com/nextcloud/utils/extensions/ViewExtensions.kt diff --git a/app/src/main/java/com/nextcloud/client/editimage/EditImageActivity.kt b/app/src/main/java/com/nextcloud/client/editimage/EditImageActivity.kt index 161259c311..5836843059 100644 --- a/app/src/main/java/com/nextcloud/client/editimage/EditImageActivity.kt +++ b/app/src/main/java/com/nextcloud/client/editimage/EditImageActivity.kt @@ -20,7 +20,6 @@ */ package com.nextcloud.client.editimage -import android.annotation.SuppressLint import android.graphics.Bitmap import android.net.Uri import android.os.Build @@ -34,6 +33,9 @@ import androidx.core.graphics.drawable.DrawableCompat import com.canhub.cropper.CropImageView import com.nextcloud.client.di.Injectable import com.nextcloud.utils.extensions.getParcelableArgument +import com.nextcloud.utils.extensions.hasNavBar +import com.nextcloud.utils.extensions.navBarHeight +import com.nextcloud.utils.extensions.shiftUp import com.owncloud.android.R import com.owncloud.android.databinding.ActivityEditImageBinding import com.owncloud.android.datamodel.OCFile @@ -102,40 +104,19 @@ class EditImageActivity : setupCropper() - if (hasNavigationBar()) { + if (resources.hasNavBar()) { shiftLayout() } } private fun shiftLayout() { - val navBarHeight: Float = getNavigationBarHeight().toFloat() + val navBarHeight: Float = resources.navBarHeight().toFloat() @Suppress("MagicNumber") - val imageShiftValue = binding.editButtonsLayout.height * 1.4f + val imageShiftValue = DisplayUtils.convertDpToPixel(60f, this).toFloat() - binding.cropImageView.post { - binding.cropImageView.translationY = -imageShiftValue - } - binding.editButtonsLayout.post { - binding.editButtonsLayout.translationY = -navBarHeight - } - } - - @SuppressLint("DiscouragedApi") - private fun hasNavigationBar(): Boolean { - val id = resources.getIdentifier("config_showNavigationBar", "bool", "android") - return id > 0 && resources.getBoolean(id) - } - - @SuppressLint("InternalInsetResource", "DiscouragedApi") - private fun getNavigationBarHeight(): Int { - val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android") - - return if (resourceId > 0) { - resources.getDimensionPixelSize(resourceId) - } else { - 0 - } + binding.cropImageView.shiftUp(imageShiftValue) + binding.editButtonsLayout.shiftUp(navBarHeight) } override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) { diff --git a/app/src/main/java/com/nextcloud/utils/extensions/ResourcesExtensions.kt b/app/src/main/java/com/nextcloud/utils/extensions/ResourcesExtensions.kt new file mode 100644 index 0000000000..bd2559a843 --- /dev/null +++ b/app/src/main/java/com/nextcloud/utils/extensions/ResourcesExtensions.kt @@ -0,0 +1,45 @@ +/* + * Nextcloud Android client application + * + * @author Alper Ozturk + * Copyright (C) 2023 Alper Ozturk + * Copyright (C) 2023 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.utils.extensions + +import android.annotation.SuppressLint +import android.content.res.Resources + +@SuppressLint("DiscouragedApi", "InternalInsetResource") +fun Resources.navBarHeight(): Int { + val resourceId: Int = getIdentifier("navigation_bar_height", "dimen", "android") + + return if (resourceId > 0) { + getDimensionPixelSize(resourceId) + } else { + 0 + } +} + +/** + * This method only works in real device + */ +@SuppressLint("DiscouragedApi") +fun Resources.hasNavBar(): Boolean { + val id = getIdentifier("config_showNavigationBar", "bool", "android") + return id > 0 && getBoolean(id) +} diff --git a/app/src/main/java/com/nextcloud/utils/extensions/ViewExtensions.kt b/app/src/main/java/com/nextcloud/utils/extensions/ViewExtensions.kt new file mode 100644 index 0000000000..f189dada50 --- /dev/null +++ b/app/src/main/java/com/nextcloud/utils/extensions/ViewExtensions.kt @@ -0,0 +1,30 @@ +/* + * Nextcloud Android client application + * + * @author Alper Ozturk + * Copyright (C) 2023 Alper Ozturk + * Copyright (C) 2023 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.utils.extensions + +import android.view.View + +fun View.shiftUp(value: Float) { + post { + translationY = -value + } +}