Allow changing base URL from UI
This commit is contained in:
@@ -68,6 +68,7 @@ class MainActivity : AppCompatActivity(R.layout.main_activity) {
|
||||
val directions = when (menuItem.itemId) {
|
||||
R.id.add_recipe -> NavGraphDirections.actionGlobalAddRecipeFragment()
|
||||
R.id.recipes_list -> NavGraphDirections.actionGlobalRecipesFragment()
|
||||
R.id.change_url -> NavGraphDirections.actionGlobalBaseURLFragment()
|
||||
else -> throw IllegalArgumentException("Unknown menu item id: ${menuItem.itemId}")
|
||||
}
|
||||
navigateTo(directions)
|
||||
|
||||
@@ -5,8 +5,10 @@ import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import gq.kirmanak.mealient.data.baseurl.ServerInfoRepo
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import gq.kirmanak.mealient.ui.OperationUiState
|
||||
@@ -16,6 +18,8 @@ import javax.inject.Inject
|
||||
@HiltViewModel
|
||||
class BaseURLViewModel @Inject constructor(
|
||||
private val serverInfoRepo: ServerInfoRepo,
|
||||
private val authRepo: AuthRepo,
|
||||
private val recipeRepo: RecipeRepo,
|
||||
private val versionDataSource: VersionDataSource,
|
||||
private val logger: Logger,
|
||||
) : ViewModel() {
|
||||
@@ -33,10 +37,17 @@ class BaseURLViewModel @Inject constructor(
|
||||
|
||||
private suspend fun checkBaseURL(baseURL: String) {
|
||||
logger.v { "checkBaseURL() called with: baseURL = $baseURL" }
|
||||
if (baseURL == serverInfoRepo.getUrl()) {
|
||||
logger.d { "checkBaseURL: new URL matches current" }
|
||||
_uiState.value = OperationUiState.fromResult(Result.success(Unit))
|
||||
return
|
||||
}
|
||||
val result = runCatchingExceptCancel {
|
||||
// If it returns proper version info then it must be a Mealie
|
||||
val version = versionDataSource.getVersionInfo(baseURL).version
|
||||
serverInfoRepo.storeBaseURL(baseURL, version)
|
||||
authRepo.logout()
|
||||
recipeRepo.clearLocalData()
|
||||
}
|
||||
logger.i { "checkBaseURL: result is $result" }
|
||||
_uiState.value = OperationUiState.fromResult(result)
|
||||
|
||||
Reference in New Issue
Block a user