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) {
|
val directions = when (menuItem.itemId) {
|
||||||
R.id.add_recipe -> NavGraphDirections.actionGlobalAddRecipeFragment()
|
R.id.add_recipe -> NavGraphDirections.actionGlobalAddRecipeFragment()
|
||||||
R.id.recipes_list -> NavGraphDirections.actionGlobalRecipesFragment()
|
R.id.recipes_list -> NavGraphDirections.actionGlobalRecipesFragment()
|
||||||
|
R.id.change_url -> NavGraphDirections.actionGlobalBaseURLFragment()
|
||||||
else -> throw IllegalArgumentException("Unknown menu item id: ${menuItem.itemId}")
|
else -> throw IllegalArgumentException("Unknown menu item id: ${menuItem.itemId}")
|
||||||
}
|
}
|
||||||
navigateTo(directions)
|
navigateTo(directions)
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ import androidx.lifecycle.MutableLiveData
|
|||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
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.ServerInfoRepo
|
||||||
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
||||||
|
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||||
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
import gq.kirmanak.mealient.logging.Logger
|
||||||
import gq.kirmanak.mealient.ui.OperationUiState
|
import gq.kirmanak.mealient.ui.OperationUiState
|
||||||
@@ -16,6 +18,8 @@ import javax.inject.Inject
|
|||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class BaseURLViewModel @Inject constructor(
|
class BaseURLViewModel @Inject constructor(
|
||||||
private val serverInfoRepo: ServerInfoRepo,
|
private val serverInfoRepo: ServerInfoRepo,
|
||||||
|
private val authRepo: AuthRepo,
|
||||||
|
private val recipeRepo: RecipeRepo,
|
||||||
private val versionDataSource: VersionDataSource,
|
private val versionDataSource: VersionDataSource,
|
||||||
private val logger: Logger,
|
private val logger: Logger,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
@@ -33,10 +37,17 @@ class BaseURLViewModel @Inject constructor(
|
|||||||
|
|
||||||
private suspend fun checkBaseURL(baseURL: String) {
|
private suspend fun checkBaseURL(baseURL: String) {
|
||||||
logger.v { "checkBaseURL() called with: baseURL = $baseURL" }
|
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 {
|
val result = runCatchingExceptCancel {
|
||||||
// If it returns proper version info then it must be a Mealie
|
// If it returns proper version info then it must be a Mealie
|
||||||
val version = versionDataSource.getVersionInfo(baseURL).version
|
val version = versionDataSource.getVersionInfo(baseURL).version
|
||||||
serverInfoRepo.storeBaseURL(baseURL, version)
|
serverInfoRepo.storeBaseURL(baseURL, version)
|
||||||
|
authRepo.logout()
|
||||||
|
recipeRepo.clearLocalData()
|
||||||
}
|
}
|
||||||
logger.i { "checkBaseURL: result is $result" }
|
logger.i { "checkBaseURL: result is $result" }
|
||||||
_uiState.value = OperationUiState.fromResult(result)
|
_uiState.value = OperationUiState.fromResult(result)
|
||||||
|
|||||||
@@ -7,4 +7,8 @@
|
|||||||
<item
|
<item
|
||||||
android:id="@+id/add_recipe"
|
android:id="@+id/add_recipe"
|
||||||
android:title="@string/menu_bottom_navigation_add_recipe" />
|
android:title="@string/menu_bottom_navigation_add_recipe" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/change_url"
|
||||||
|
android:title="@string/menu_bottom_navigation_change_url" />
|
||||||
</menu>
|
</menu>
|
||||||
@@ -72,4 +72,8 @@
|
|||||||
<action
|
<action
|
||||||
android:id="@+id/action_global_addRecipeFragment"
|
android:id="@+id/action_global_addRecipeFragment"
|
||||||
app:destination="@id/addRecipeFragment" />
|
app:destination="@id/addRecipeFragment" />
|
||||||
|
|
||||||
|
<action
|
||||||
|
android:id="@+id/action_global_baseURLFragment"
|
||||||
|
app:destination="@id/baseURLFragment" />
|
||||||
</navigation>
|
</navigation>
|
||||||
@@ -47,4 +47,5 @@
|
|||||||
<string name="fragment_recipes_load_failure_toast_unexpected_response">неожиданный ответ</string>
|
<string name="fragment_recipes_load_failure_toast_unexpected_response">неожиданный ответ</string>
|
||||||
<string name="fragment_recipes_load_failure_toast_no_connection">нет соединения</string>
|
<string name="fragment_recipes_load_failure_toast_no_connection">нет соединения</string>
|
||||||
<string name="fragment_recipes_load_failure_toast_no_reason">Ошибка загрузки.</string>
|
<string name="fragment_recipes_load_failure_toast_no_reason">Ошибка загрузки.</string>
|
||||||
|
<string name="menu_bottom_navigation_change_url">Сменить URL</string>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -51,4 +51,5 @@
|
|||||||
<string name="fragment_recipes_load_failure_toast_unauthorized">unauthorized</string>
|
<string name="fragment_recipes_load_failure_toast_unauthorized">unauthorized</string>
|
||||||
<string name="fragment_recipes_load_failure_toast_unexpected_response">unexpected response</string>
|
<string name="fragment_recipes_load_failure_toast_unexpected_response">unexpected response</string>
|
||||||
<string name="fragment_recipes_load_failure_toast_no_connection">no connection</string>
|
<string name="fragment_recipes_load_failure_toast_no_connection">no connection</string>
|
||||||
|
<string name="menu_bottom_navigation_change_url">Change URL</string>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user