Fix handling result in BaseURLFragment and AuthenticationFragment
This commit is contained in:
@@ -11,7 +11,6 @@ import gq.kirmanak.mealient.R
|
||||
import gq.kirmanak.mealient.data.network.NetworkError
|
||||
import gq.kirmanak.mealient.databinding.FragmentBaseUrlBinding
|
||||
import gq.kirmanak.mealient.extensions.checkIfInputIsEmpty
|
||||
import gq.kirmanak.mealient.extensions.launchWithViewLifecycle
|
||||
import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -24,6 +23,7 @@ class BaseURLFragment : Fragment(R.layout.fragment_base_url) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
Timber.v("onViewCreated() called with: view = $view, savedInstanceState = $savedInstanceState")
|
||||
binding.button.setOnClickListener(::onProceedClick)
|
||||
viewModel.checkURLResult.observe(viewLifecycleOwner, ::onCheckURLResult)
|
||||
}
|
||||
|
||||
private fun onProceedClick(view: View) {
|
||||
@@ -33,7 +33,7 @@ class BaseURLFragment : Fragment(R.layout.fragment_base_url) {
|
||||
lifecycleOwner = viewLifecycleOwner,
|
||||
stringId = R.string.fragment_baseurl_url_input_empty,
|
||||
) ?: return
|
||||
launchWithViewLifecycle { onCheckURLResult(viewModel.saveBaseUrl(url)) }
|
||||
viewModel.saveBaseUrl(url)
|
||||
}
|
||||
|
||||
private fun onCheckURLResult(result: Result<Unit>) {
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package gq.kirmanak.mealient.ui.baseurl
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
||||
import gq.kirmanak.mealient.extensions.runCatchingExceptCancel
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -14,21 +18,25 @@ class BaseURLViewModel @Inject constructor(
|
||||
private val versionDataSource: VersionDataSource,
|
||||
) : ViewModel() {
|
||||
|
||||
suspend fun saveBaseUrl(baseURL: String): Result<Unit> {
|
||||
private val _checkURLResult = MutableLiveData<Result<Unit>>()
|
||||
val checkURLResult: LiveData<Result<Unit>> get() = _checkURLResult
|
||||
|
||||
fun saveBaseUrl(baseURL: String) {
|
||||
Timber.v("saveBaseUrl() called with: baseURL = $baseURL")
|
||||
val hasPrefix = ALLOWED_PREFIXES.any { baseURL.startsWith(it) }
|
||||
val url = baseURL.takeIf { hasPrefix } ?: WITH_PREFIX_FORMAT.format(baseURL)
|
||||
return checkBaseURL(url)
|
||||
viewModelScope.launch { checkBaseURL(url) }
|
||||
}
|
||||
|
||||
private suspend fun checkBaseURL(baseURL: String): Result<Unit> {
|
||||
private suspend fun checkBaseURL(baseURL: String) {
|
||||
Timber.v("checkBaseURL() called with: baseURL = $baseURL")
|
||||
val result = runCatchingExceptCancel {
|
||||
// If it returns proper version info then it must be a Mealie
|
||||
versionDataSource.getVersionInfo(baseURL)
|
||||
baseURLStorage.storeBaseURL(baseURL)
|
||||
}
|
||||
baseURLStorage.storeBaseURL(baseURL)
|
||||
return result.map { }
|
||||
Timber.i("checkBaseURL: result is $result")
|
||||
_checkURLResult.value = result
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
Reference in New Issue
Block a user