Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ch8n/modular-dialog-fragment-design
Sample project that displays a dialog fragment which shows communication between activity and fragment using shared viewmodel
https://github.com/ch8n/modular-dialog-fragment-design
Last synced: 14 days ago
JSON representation
Sample project that displays a dialog fragment which shows communication between activity and fragment using shared viewmodel
- Host: GitHub
- URL: https://github.com/ch8n/modular-dialog-fragment-design
- Owner: ch8n
- Created: 2020-09-27T11:00:26.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-02-26T11:38:28.000Z (almost 4 years ago)
- Last Synced: 2024-10-30T20:13:36.389Z (2 months ago)
- Language: Kotlin
- Size: 146 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# modular-dialog-fragment-design
Sample project that displays a dialog fragment which shows communication between activity and fragment using shared viewmodel## BaseDialogFragment
this class contains skeletal code for getting ViewModel
- ViewModel is created according to the scope of parent using
```
private fun viewModelOfActivityOrFragment(): DialogViewModel {
return if (parentFragment != null) {
ViewModelProvider(requireParentFragment()).get(DialogViewModel::class.java)
} else {
ViewModelProvider(requireActivity()).get(DialogViewModel::class.java)
}
}
```## Applying properties to Dialog
In Activity, get the `DialogViewModel` using `ViewModelProvider`
```
val dialogVM = ViewModelProvider(this@MainActivity).get(DialogViewModel::class.java)
// apply properties
with(dialogVM) {
dialogTitle.value = "sample title"
dialogDescription.value = "sample description"
confirmClickListener = {..doStuff}
cancelClickListener = {..doStuff}
dismissClickListener = {..doStuff}
}```
In Fragments, get the `DialogViewModel` using `ViewModelProvider`
```
val dialogVM = ViewModelProvider(this@SampleFragment).get(DialogViewModel::class.java)
// apply properties
with(dialogVM) {
dialogTitle.value = "sample title"
dialogDescription.value = "sample description"
confirmClickListener = {..doStuff}
cancelClickListener = {..doStuff}
dismissClickListener = {..doStuff}
}```
## License
```
Copyright [2020] [Chetan gupta] [chetangupta.net]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```