Эх сурвалжийг харах

Use extensions

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 жил өмнө
parent
commit
99a7a8c0a5

+ 8 - 27
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) {

+ 45 - 0
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 <https://www.gnu.org/licenses/>.
+ */
+
+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)
+}

+ 30 - 0
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 <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.utils.extensions
+
+import android.view.View
+
+fun View.shiftUp(value: Float) {
+    post {
+        translationY = -value
+    }
+}