Skip to content

Commit 6bc834e

Browse files
committed
change onChanged() to accept nullable argument
1 parent fa6def9 commit 6bc834e

5 files changed

Lines changed: 41 additions & 16 deletions

File tree

app/src/main/java/ch/pete/appconfigapp/configdetail/ConfigDetailFragment.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,17 @@ class ConfigDetailFragment : Fragment(), ConfigDetailView, TitleFragment {
9090

9191
val configLiveDataKeyValues = viewModel.keyValueEntriesByConfigId(config.id)
9292
configLiveDataKeyValues.observe(viewLifecycleOwner, object : Observer<List<KeyValue>> {
93-
override fun onChanged(keyValues: List<KeyValue>) {
93+
override fun onChanged(keyValues: List<KeyValue>?) {
9494
configLiveDataKeyValues.removeObserver(this)
95+
9596
keyValue.text =
96-
context?.resources?.getQuantityString(
97-
R.plurals.keys_count,
98-
keyValues.size,
99-
keyValues.size
100-
) ?: ""
97+
if (keyValues != null) {
98+
context?.resources?.getQuantityString(
99+
R.plurals.keys_count,
100+
keyValues.size,
101+
keyValues.size
102+
) ?: ""
103+
} else ""
101104

102105
editKeyValue.setOnClickListener {
103106
viewModel.onEditKeyValueClicked(config)

app/src/main/java/ch/pete/appconfigapp/configdetail/ConfigDetailViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ConfigDetailViewModel(application: Application) : AndroidViewModel(applica
3838
configLiveData.removeObserver(this)
3939
view.initViewWithConfig(config)
4040
} else {
41+
Timber.w("config is null, close fragment")
4142
view.close()
4243
}
4344
}

app/src/main/java/ch/pete/appconfigapp/externalconfiglocationdetails/ExternalConfigLocationDetailFragment.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlinx.android.synthetic.main.fragment_external_config_location_detail.u
1818
import kotlinx.android.synthetic.main.fragment_external_config_location_detail.view.explanation
1919
import kotlinx.android.synthetic.main.fragment_external_config_location_detail.view.name
2020
import kotlinx.android.synthetic.main.fragment_external_config_location_detail.view.url
21+
import timber.log.Timber
2122

2223
class ExternalConfigLocationDetailFragment :
2324
Fragment(), ExternalConfigLocationDetailView, TitleFragment {
@@ -59,10 +60,16 @@ class ExternalConfigLocationDetailFragment :
5960
val liveData =
6061
viewModel.externalConfigLocation()
6162
liveData.observe(viewLifecycleOwner, object : Observer<ExternalConfigLocation> {
62-
override fun onChanged(externalConfigLocation: ExternalConfigLocation) {
63+
override fun onChanged(externalConfigLocation: ExternalConfigLocation?) {
6364
liveData.removeObserver(this)
64-
rootView.name.setText(externalConfigLocation.name)
65-
rootView.url.setText(externalConfigLocation.url)
65+
66+
if (externalConfigLocation != null) {
67+
rootView.name.setText(externalConfigLocation.name)
68+
rootView.url.setText(externalConfigLocation.url)
69+
} else {
70+
Timber.w("externalConfigLocation is null, close fragment")
71+
close()
72+
}
6673
}
6774
})
6875
}

app/src/main/java/ch/pete/appconfigapp/keyvaluedetails/KeyValueDialogFragment.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import kotlinx.android.synthetic.main.dialogfragment_keyvalues.view.key
1616
import kotlinx.android.synthetic.main.dialogfragment_keyvalues.view.nullCheckbox
1717
import kotlinx.android.synthetic.main.dialogfragment_keyvalues.view.ok
1818
import kotlinx.android.synthetic.main.dialogfragment_keyvalues.view.value
19+
import timber.log.Timber
1920

2021

2122
class KeyValueDialogFragment : DialogFragment(), KeyValueDialogView {
@@ -82,11 +83,17 @@ class KeyValueDialogFragment : DialogFragment(), KeyValueDialogView {
8283
val keyValueLiveData =
8384
viewModel.keyValueEntry()
8485
keyValueLiveData.observe(viewLifecycleOwner, object : Observer<KeyValue> {
85-
override fun onChanged(keyValue: KeyValue) {
86+
override fun onChanged(keyValue: KeyValue?) {
8687
keyValueLiveData.removeObserver(this)
87-
rootView.key.setText(keyValue.key)
88-
rootView.value.setText(keyValue.value)
89-
rootView.nullCheckbox.isChecked = keyValue.value == null
88+
89+
if (keyValue != null) {
90+
rootView.key.setText(keyValue.key)
91+
rootView.value.setText(keyValue.value)
92+
rootView.nullCheckbox.isChecked = keyValue.value == null
93+
} else {
94+
Timber.w("keyValue is null, close dialog")
95+
close()
96+
}
9097
}
9198
})
9299
}

app/src/main/java/ch/pete/appconfigapp/nameauthority/NameAuthorityFragment.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlinx.android.synthetic.main.fragment_name_authority.name
1818
import kotlinx.android.synthetic.main.fragment_name_authority.view.authority
1919
import kotlinx.android.synthetic.main.fragment_name_authority.view.explanation
2020
import kotlinx.android.synthetic.main.fragment_name_authority.view.name
21+
import timber.log.Timber
2122

2223
class NameAuthorityFragment : Fragment(), TitleFragment, NameAuthorityView {
2324
companion object {
@@ -53,10 +54,16 @@ class NameAuthorityFragment : Fragment(), TitleFragment, NameAuthorityView {
5354
val liveData =
5455
viewModel.config()
5556
liveData.observe(viewLifecycleOwner, object : Observer<Config> {
56-
override fun onChanged(config: Config) {
57+
override fun onChanged(config: Config?) {
5758
liveData.removeObserver(this)
58-
rootView.name.setText(config.name)
59-
rootView.authority.setText(config.authority)
59+
60+
if (config != null) {
61+
rootView.name.setText(config.name)
62+
rootView.authority.setText(config.authority)
63+
} else {
64+
Timber.w("config is null, close fragment")
65+
close()
66+
}
6067
}
6168
})
6269
}

0 commit comments

Comments
 (0)