From 4af24f1fb1a660c0e24acecdb389a35003b95d71 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 16 Sep 2024 11:33:22 +0200 Subject: [PATCH] Use Activity When Fragment Added Signed-off-by: alperozturk --- .../utils/extensions/FragmentExtensions.kt | 18 ++++++++++++++++++ .../ui/dialog/CreateFolderDialogFragment.kt | 6 +++--- 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/utils/extensions/FragmentExtensions.kt diff --git a/app/src/main/java/com/nextcloud/utils/extensions/FragmentExtensions.kt b/app/src/main/java/com/nextcloud/utils/extensions/FragmentExtensions.kt new file mode 100644 index 000000000000..e8a71ea9eef9 --- /dev/null +++ b/app/src/main/java/com/nextcloud/utils/extensions/FragmentExtensions.kt @@ -0,0 +1,18 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2024 Alper Ozturk + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +package com.nextcloud.utils.extensions + +import androidx.fragment.app.Fragment + +inline fun Fragment.typedActivity(): T? { + return if (isAdded && activity != null && activity is T) { + activity as T + } else { + null + } +} diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.kt b/app/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.kt index a2fe6821de6f..80fb96e8dd0b 100644 --- a/app/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.kt @@ -27,6 +27,7 @@ import com.nextcloud.client.account.CurrentAccountProvider import com.nextcloud.client.di.Injectable import com.nextcloud.client.network.ConnectivityService import com.nextcloud.utils.extensions.getParcelableArgument +import com.nextcloud.utils.extensions.typedActivity import com.nextcloud.utils.fileNameValidator.FileNameValidator import com.owncloud.android.R import com.owncloud.android.databinding.EditBoxDialogBinding @@ -185,7 +186,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList val path = parentFolder?.decryptedRemotePath + newFolderName + OCFile.PATH_SEPARATOR lifecycleScope.launch(Dispatchers.IO) { if (connectivityService.isNetworkAndServerAvailable()) { - (requireActivity() as ComponentsGetter).fileOperationsHelper.createFolder(path) + typedActivity()?.fileOperationsHelper?.createFolder(path) } else { Log_OC.d(TAG, "Network not available, creating offline operation") fileDataStorageManager.addCreateFolderOfflineOperation( @@ -196,8 +197,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList ) launch(Dispatchers.Main) { - val fileDisplayActivity = requireActivity() as? FileDisplayActivity - fileDisplayActivity?.syncAndUpdateFolder(true) + (requireActivity() as? FileDisplayActivity)?.syncAndUpdateFolder(true) } } }