Skip to content

Commit

Permalink
Fixes when the screen is rotated
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitorbp committed Sep 5, 2023
1 parent 4f35192 commit ff7dcd3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.owncloud.android.extensions.ViewModelExt.runUseCaseWithResult
import com.owncloud.android.presentation.common.UIResult
import com.owncloud.android.providers.ContextProvider
import com.owncloud.android.providers.CoroutinesDispatcherProvider
import com.owncloud.android.ui.dialog.FileAlreadyExistsDialog
import com.owncloud.android.usecases.synchronization.SynchronizeFileUseCase
import com.owncloud.android.usecases.synchronization.SynchronizeFolderUseCase
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -92,6 +93,9 @@ class FileOperationsViewModel(
private val _createFileWithAppProviderFlow = MutableStateFlow<Event<UIResult<String>>?>(null)
val createFileWithAppProviderFlow: StateFlow<Event<UIResult<String>>?> = _createFileWithAppProviderFlow

val openDialogs = mutableListOf<FileAlreadyExistsDialog>()


// Used to save the last operation folder
private var lastTargetFolder: OCFile? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ class FileDisplayActivity : FileActivity(),

private val sharedPreferences: SharedPreferencesProvider by inject()

private val openDialogs = mutableListOf<FileAlreadyExistsDialog>()

var filesUploadHelper: FilesUploadHelper? = null
internal set

Expand Down Expand Up @@ -736,6 +734,7 @@ class FileDisplayActivity : FileActivity(),
// responsibility of restore is preferred in onCreate() before than in
// onRestoreInstanceState when there are Fragments involved
Timber.v("onSaveInstanceState() start")

super.onSaveInstanceState(outState)
outState.putParcelable(KEY_WAITING_TO_PREVIEW, fileWaitingToPreview)
outState.putBoolean(KEY_SYNC_IN_PROGRESS, syncInProgress)
Expand Down Expand Up @@ -771,6 +770,7 @@ class FileDisplayActivity : FileActivity(),
syncBroadcastReceiver = SyncBroadcastReceiver()
localBroadcastManager!!.registerReceiver(syncBroadcastReceiver!!, syncIntentFilter)

showDialogs()
Timber.v("onResume() end")
}

Expand All @@ -782,6 +782,7 @@ class FileDisplayActivity : FileActivity(),
}

super.onPause()
dismissDialogs()
Timber.v("onPause() end")
}

Expand Down Expand Up @@ -1059,50 +1060,40 @@ class FileDisplayActivity : FileActivity(),
replace: MutableList<Boolean?>,
launchAction: (List<OCFile>, List<Boolean?>) -> Unit,
) {
val context = this@FileDisplayActivity
if (!uiResult.data.isNullOrEmpty()) {
var pos = 0
val fragmentManager = supportFragmentManager
var posDialog = data.lastIndex
data.asReversed().forEachIndexed { index, file ->
val countDownValue = index + 1

val customDialog = FileAlreadyExistsDialog.Builder()
.setTitle(
this.getString(
if (file.isFolder) {
R.string.folder_already_exists
} else {
R.string.file_already_exists
}
)
)
.setDescription(
this.getString(
if (file.isFolder) {
R.string.folder_already_exists_description
} else {
R.string.folder_already_exists_description
}, file.fileName
)
)
.setCheckboxText(this.getString(R.string.apply_to_all_conflicts, countDownValue.toString()))
.build()


if (countDownValue > 1) {
customDialog.checkboxVisible = true
}
val customDialog = FileAlreadyExistsDialog.newInstance(
this.getString(
if (file.isFolder) {
R.string.folder_already_exists
} else {
R.string.file_already_exists
}
),
this.getString(
if (file.isFolder) {
R.string.folder_already_exists_description
} else {
R.string.file_already_exists_description
}, file.fileName
),
this.getString(R.string.apply_to_all_conflicts, countDownValue.toString()),
countDownValue > 1
)
customDialog.isCancelable = false
customDialog.show(this.supportFragmentManager, CUSTOM_DIALOG_TAG)

fragmentManager?.let {
customDialog.show(it, CUSTOM_DIALOG_TAG)
}
fileOperationsViewModel.openDialogs.add(customDialog)

openDialogs.add(customDialog)

customDialog.setDialogButtonClickListener(object : FileAlreadyExistsDialog.DialogButtonClickListener {

override fun onKeepBothButtonClick() {
if (customDialog.isCheckBoxChecked) {
if (fileOperationsViewModel.openDialogs[posDialog].isCheckBoxChecked) {
repeat(data.asReversed().size) {
replace.add(false)
pos++
Expand All @@ -1117,12 +1108,18 @@ class FileDisplayActivity : FileActivity(),
if (pos == data.size) {
launchAction(uiResult.data, replace)
}
customDialog.dismiss()
fileOperationsViewModel.openDialogs[posDialog].dismiss()
fileOperationsViewModel.openDialogs.removeAt(posDialog)
if (posDialog == 0) {
fileOperationsViewModel.openDialogs.clear()
} else {
posDialog--
}
}
}

override fun onSkipButtonClick() {
if (customDialog.isCheckBoxChecked) {
if (fileOperationsViewModel.openDialogs[posDialog].isCheckBoxChecked) {
repeat(data.asReversed().size) {
replace.add(null)
pos++
Expand All @@ -1137,12 +1134,19 @@ class FileDisplayActivity : FileActivity(),
if (pos == data.size) {
launchAction(uiResult.data, replace)
}
customDialog.dismiss()

fileOperationsViewModel.openDialogs[posDialog].dismiss()
fileOperationsViewModel.openDialogs.removeAt(posDialog)
if (posDialog == 0) {
fileOperationsViewModel.openDialogs.clear()
} else {
posDialog--
}
}
}

override fun onReplaceButtonClick() {
if (customDialog.isCheckBoxChecked) {
if (fileOperationsViewModel.openDialogs[posDialog].isCheckBoxChecked) {
repeat(data.asReversed().size) {
replace.add(true)
pos++
Expand All @@ -1157,7 +1161,13 @@ class FileDisplayActivity : FileActivity(),
if (pos == data.size) {
launchAction(uiResult.data, replace)
}
customDialog.dismiss()
fileOperationsViewModel.openDialogs[posDialog].dismiss()
fileOperationsViewModel.openDialogs.removeAt(posDialog)
if (posDialog == 0) {
fileOperationsViewModel.openDialogs.clear()
} else {
posDialog--
}
}
}
}
Expand All @@ -1167,12 +1177,23 @@ class FileDisplayActivity : FileActivity(),
}

private fun dismissAllOpenDialogs() {
openDialogs.forEach { dialog ->
fileOperationsViewModel.openDialogs.forEach { dialog ->
dialog.dismiss()
}
openDialogs.clear()
fileOperationsViewModel.openDialogs.clear()
}


private fun showDialogs() {
fileOperationsViewModel.openDialogs.forEach { dialog ->
dialog.show(this.supportFragmentManager, CUSTOM_DIALOG_TAG)
}
}
private fun dismissDialogs() {
fileOperationsViewModel.openDialogs.forEach { dialog ->
dialog.dismiss()
}
}
private fun launchCopyFile(files: List<OCFile>, replace: List<Boolean?>) {
fileOperationsViewModel.performOperation(
FileOperation.CopyOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,45 @@ import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.owncloud.android.databinding.DialogFileAlreadyExistsBinding

class FileAlreadyExistsDialog private constructor(
private val titleText: String?,
private val descriptionText: String?,
private val checkboxText: String?,
private var dialogClickListener: DialogButtonClickListener? = null,
) : DialogFragment() {
class FileAlreadyExistsDialog : DialogFragment() {

private lateinit var binding: DialogFileAlreadyExistsBinding
internal var isCheckBoxChecked: Boolean = false
internal var checkboxVisible: Boolean = false

interface DialogButtonClickListener {
fun onKeepBothButtonClick()
fun onSkipButtonClick()
fun onReplaceButtonClick()
}

data class Builder(
private var titleText: String? = null,
private var descriptionText: String? = null,
private var checkboxText: String? = null,
private var checkboxVisible: Boolean? = false,
) {

fun setTitle(titleText: String) = apply { this.titleText = titleText }
fun setDescription(descriptionText: String) = apply { this.descriptionText = descriptionText }
fun setCheckboxText(checkboxText: String) = apply { this.checkboxText = checkboxText }
fun build() = FileAlreadyExistsDialog(
titleText = titleText,
descriptionText = descriptionText,
checkboxText = checkboxText,
)
companion object {
var mListener: DialogButtonClickListener? = null

const val TITLE_TEXT = "titleText"
const val DESCRIPTION_TEXT = "descriptionText"
const val CHECKBOX_TEXT = "checkboxText"
private const val CHECKBOX_VISIBLE = "checkboxVisible"

fun newInstance(
titleText: String?, descriptionText: String?, checkboxText: String?,
checkboxVisible: Boolean,
dialogClickListener: DialogButtonClickListener? = null,

): FileAlreadyExistsDialog {
val fragment = FileAlreadyExistsDialog()
val args = Bundle()
args.putString(TITLE_TEXT, titleText)
args.putString(DESCRIPTION_TEXT, descriptionText)
args.putString(CHECKBOX_TEXT, checkboxText)
args.putBoolean(CHECKBOX_VISIBLE, checkboxVisible)

mListener = dialogClickListener
fragment.arguments = args
return fragment
}
}

fun setDialogButtonClickListener(listener: DialogButtonClickListener) = apply { dialogClickListener = listener }
fun setDialogButtonClickListener(listener: DialogButtonClickListener) = apply { mListener = listener }

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -55,18 +59,23 @@ class FileAlreadyExistsDialog private constructor(

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val titleText = arguments?.getString(TITLE_TEXT)
val descriptionText = arguments?.getString(DESCRIPTION_TEXT)
val checkboxText = arguments?.getString(CHECKBOX_TEXT)
val checkboxVisible = arguments?.getBoolean(CHECKBOX_VISIBLE)

binding.dialogFileAlreadyExistsTitle.text = titleText
binding.dialogFileAlreadyExistsInformation.text = descriptionText
binding.dialogFileAlreadyExistsCheckbox.text = checkboxText
binding.dialogFileAlreadyExistsKeepBoth.setOnClickListener { dialogClickListener?.onKeepBothButtonClick() }

binding.dialogFileAlreadyExistsKeepBoth.setOnClickListener { mListener?.onKeepBothButtonClick() }
binding.dialogFileAlreadyExistsCheckbox.setOnCheckedChangeListener { _, isChecked ->
isCheckBoxChecked = isChecked
}
binding.dialogFileAlreadyExistsReplace.setOnClickListener { dialogClickListener?.onReplaceButtonClick() }
binding.dialogFileAlreadyExistsSkip.setOnClickListener { dialogClickListener?.onSkipButtonClick() }
binding.dialogFileAlreadyExistsReplace.setOnClickListener { mListener?.onReplaceButtonClick() }
binding.dialogFileAlreadyExistsSkip.setOnClickListener { mListener?.onSkipButtonClick() }

if (!checkboxVisible) {
if (!checkboxVisible!!) {
binding.dialogFileAlreadyExistsCheckbox.visibility = View.GONE
} else {
binding.dialogFileAlreadyExistsCheckbox.visibility = View.VISIBLE
Expand Down

0 comments on commit ff7dcd3

Please sign in to comment.