-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from aivanovski/feature/add-option-to-copy-db…
…-key-into-private-storage Add "Make a copy and Select" option into built-in File Picker
- Loading branch information
Showing
29 changed files
with
722 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...main/kotlin/com/ivanovsky/passnotes/presentation/core/dialog/optionDialog/OptionDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.ivanovsky.passnotes.presentation.core.dialog.optionDialog | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.ivanovsky.passnotes.R | ||
import com.ivanovsky.passnotes.databinding.CoreBaseCellDialogBinding | ||
import com.ivanovsky.passnotes.extensions.cloneInContext | ||
import com.ivanovsky.passnotes.presentation.core.ViewModelTypes | ||
import com.ivanovsky.passnotes.presentation.core.adapter.ViewModelsAdapter | ||
import com.ivanovsky.passnotes.presentation.core.extensions.getMandatoryArgument | ||
import com.ivanovsky.passnotes.presentation.core.extensions.setViewModels | ||
import com.ivanovsky.passnotes.presentation.core.extensions.withArguments | ||
import com.ivanovsky.passnotes.presentation.core.viewmodel.DividerCellViewModel | ||
import com.ivanovsky.passnotes.presentation.core.viewmodel.OneLineTextCellViewModel | ||
import com.ivanovsky.passnotes.presentation.core.viewmodel.TwoLineTextCellViewModel | ||
|
||
class OptionDialog : DialogFragment() { | ||
|
||
private val args: OptionDialogArgs by lazy { | ||
getMandatoryArgument(ARGUMENTS) | ||
} | ||
|
||
private val viewModel: OptionDialogViewModel by lazy { | ||
ViewModelProvider( | ||
owner = this, | ||
factory = OptionDialogViewModel.Factory( | ||
args = args | ||
) | ||
)[OptionDialogViewModel::class.java] | ||
} | ||
|
||
private lateinit var binding: CoreBaseCellDialogBinding | ||
private var onItemClick: ((index: Int) -> Unit)? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
if (savedInstanceState != null) { | ||
dismiss() | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
val themedInflater = inflater.cloneInContext(R.style.AppDialogTheme) | ||
val binding = CoreBaseCellDialogBinding.inflate(themedInflater, container, false) | ||
.also { | ||
binding = it | ||
} | ||
|
||
binding.recyclerView.adapter = ViewModelsAdapter( | ||
lifecycleOwner = viewLifecycleOwner, | ||
viewTypes = ViewModelTypes() | ||
.add(OneLineTextCellViewModel::class, R.layout.cell_option_one_line) | ||
.add(TwoLineTextCellViewModel::class, R.layout.cell_option_two_line) | ||
.add(DividerCellViewModel::class, R.layout.cell_divider) | ||
) | ||
|
||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
subscribeToLiveData() | ||
subscribeToEvents() | ||
} | ||
|
||
private fun subscribeToLiveData() { | ||
viewModel.cellViewModels.observe(viewLifecycleOwner) { viewModels -> | ||
binding.recyclerView.setViewModels(viewModels) | ||
} | ||
} | ||
|
||
private fun subscribeToEvents() { | ||
viewModel.selectItemEvent.observe(viewLifecycleOwner) { index -> | ||
onItemClick?.invoke(index) | ||
dismiss() | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
val TAG = OptionDialog::class.java.simpleName | ||
|
||
private const val ARGUMENTS = "arguments" | ||
|
||
fun newInstance( | ||
args: OptionDialogArgs, | ||
onItemClick: (index: Int) -> Unit | ||
): OptionDialog = | ||
OptionDialog() | ||
.withArguments { | ||
putParcelable(ARGUMENTS, args) | ||
} | ||
.apply { | ||
this.onItemClick = onItemClick | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
.../kotlin/com/ivanovsky/passnotes/presentation/core/dialog/optionDialog/OptionDialogArgs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.ivanovsky.passnotes.presentation.core.dialog.optionDialog | ||
|
||
import android.os.Parcelable | ||
import com.ivanovsky.passnotes.presentation.core.dialog.optionDialog.model.OptionItem | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
data class OptionDialogArgs( | ||
val options: List<OptionItem> | ||
) : Parcelable |
74 changes: 74 additions & 0 deletions
74
...in/com/ivanovsky/passnotes/presentation/core/dialog/optionDialog/OptionDialogViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.ivanovsky.passnotes.presentation.core.dialog.optionDialog | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.ivanovsky.passnotes.injection.GlobalInjector | ||
import com.ivanovsky.passnotes.presentation.core.BaseCellViewModel | ||
import com.ivanovsky.passnotes.presentation.core.BaseScreenViewModel | ||
import com.ivanovsky.passnotes.presentation.core.dialog.optionDialog.factory.OptionDialogCellModelFactory | ||
import com.ivanovsky.passnotes.presentation.core.dialog.optionDialog.factory.OptionDialogCellViewModelFactory | ||
import com.ivanovsky.passnotes.presentation.core.event.SingleLiveEvent | ||
import com.ivanovsky.passnotes.presentation.core.viewmodel.OneLineTextCellViewModel | ||
import com.ivanovsky.passnotes.presentation.core.viewmodel.TwoLineTextCellViewModel | ||
import com.ivanovsky.passnotes.util.toIntSafely | ||
import org.koin.core.parameter.parametersOf | ||
|
||
class OptionDialogViewModel( | ||
private val modelFactory: OptionDialogCellModelFactory, | ||
private val viewModelFactory: OptionDialogCellViewModelFactory, | ||
private val args: OptionDialogArgs | ||
) : BaseScreenViewModel() { | ||
|
||
val selectItemEvent = SingleLiveEvent<Int>() | ||
|
||
init { | ||
setCellElements(createCellViewModels()) | ||
subscribeToEvents() | ||
} | ||
|
||
override fun onCleared() { | ||
super.onCleared() | ||
unsubscribeFromEvents() | ||
} | ||
|
||
private fun subscribeToEvents() { | ||
eventProvider.subscribe(this) { event -> | ||
event.getString(OneLineTextCellViewModel.CLICK_EVENT) | ||
?.let { cellId -> | ||
onCellClicked(cellId) | ||
} | ||
|
||
event.getString(TwoLineTextCellViewModel.CLICK_EVENT) | ||
?.let { cellId -> | ||
onCellClicked(cellId) | ||
} | ||
} | ||
} | ||
|
||
private fun onCellClicked(cellId: String) { | ||
val index = cellId.toIntSafely() ?: return | ||
|
||
selectItemEvent.call(index) | ||
} | ||
|
||
private fun unsubscribeFromEvents() { | ||
eventProvider.unSubscribe(this) | ||
} | ||
|
||
private fun createCellViewModels(): List<BaseCellViewModel> { | ||
val models = modelFactory.createCellModels(args.options) | ||
return viewModelFactory.createCellViewModels(models, eventProvider) | ||
} | ||
|
||
class Factory( | ||
private val args: OptionDialogArgs | ||
) : ViewModelProvider.Factory { | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return GlobalInjector.get<OptionDialogViewModel>( | ||
parametersOf(args) | ||
) as T | ||
} | ||
} | ||
} |
Oops, something went wrong.