Skip to content

Commit

Permalink
Fix crash if dismiss setting before network check end fix #11
Browse files Browse the repository at this point in the history
by checking if doing nothing is fragment has no more context
  • Loading branch information
e-marchand committed Jan 19, 2024
1 parent b1f29ce commit 9fe7d7e
Showing 1 changed file with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,36 +194,48 @@ class SettingsFragment :
connectivityViewModel.isConnected()
}

private fun checkIfFragmentAttached(operation: Context.() -> Unit) {
if (isAdded && context != null) {
operation(requireContext())
}
}

override fun onServerAccessible() {
activity?.apply {
remoteUrlPref?.summary =
this.getString(
R.string.remote_url_placeholder,
remoteUrl,
getString(R.string.server_accessible)
)
checkIfFragmentAttached {
activity?.apply {
remoteUrlPref?.summary =
this.getString(
R.string.remote_url_placeholder,
remoteUrl,
getString(R.string.server_accessible)
)
}
remoteUrlPref?.icon = ContextCompat.getDrawable(requireContext(), R.drawable.network_ok_circle)
}
remoteUrlPref?.icon = ContextCompat.getDrawable(requireContext(), R.drawable.network_ok_circle)
}

override fun onServerInaccessible() {
activity?.apply {
remoteUrlPref?.summary =
this.getString(
R.string.remote_url_placeholder,
remoteUrl,
getString(R.string.server_not_accessible)
)
checkIfFragmentAttached {
activity?.apply {
remoteUrlPref?.summary =
this.getString(
R.string.remote_url_placeholder,
remoteUrl,
getString(R.string.server_not_accessible)
)
}
remoteUrlPref?.icon = ContextCompat.getDrawable(requireContext(), R.drawable.network_nok_circle)
}
remoteUrlPref?.icon = ContextCompat.getDrawable(requireContext(), R.drawable.network_nok_circle)
}

override fun onNoInternet() {
activity?.apply {
remoteUrlPref?.summary =
this.getString(R.string.remote_url_placeholder, remoteUrl, getString(R.string.no_internet))
checkIfFragmentAttached {
activity?.apply {
remoteUrlPref?.summary =
this.getString(R.string.remote_url_placeholder, remoteUrl, getString(R.string.no_internet))
}
remoteUrlPref?.icon = ContextCompat.getDrawable(requireContext(), R.drawable.network_nok_circle)
}
remoteUrlPref?.icon = ContextCompat.getDrawable(requireContext(), R.drawable.network_nok_circle)
}

override fun onValidRemoteUrlChange(newRemoteUrl: String) {
Expand Down

0 comments on commit 9fe7d7e

Please sign in to comment.