Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to always try the internal URL first #1788

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
Log.d("SettingsFragment", "Unable to set the icon tint", e)
}
}

findPreference<SwitchPreference>("prioritize_internal")?.isEnabled = false
}

override fun enableInternalConnection() {
Expand All @@ -298,6 +300,8 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
Log.d("SettingsFragment", "Unable to set the icon tint", e)
}
}

findPreference<SwitchPreference>("prioritize_internal")?.isEnabled = true
}

override fun onLangSettingsChanged() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SettingsPresenterImpl @Inject constructor(
"keep_screen_on" -> integrationUseCase.isKeepScreenOnEnabled()
"app_lock" -> authenticationUseCase.isLockEnabled()
"crash_reporting" -> prefsRepository.isCrashReporting()
"prioritize_internal" -> urlUseCase.isPrioritizeInternal()
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
}
Expand All @@ -54,6 +55,7 @@ class SettingsPresenterImpl @Inject constructor(
"keep_screen_on" -> integrationUseCase.setKeepScreenOnEnabled(value)
"app_lock" -> authenticationUseCase.setLockEnabled(value)
"crash_reporting" -> prefsRepository.setCrashReporting(value)
"prioritize_internal" -> urlUseCase.setPrioritizeInternal(value)
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class WebViewPresenterImpl @Inject constructor(
override fun onViewReady(path: String?) {
mainScope.launch {
val oldUrl = url
url = urlUseCase.getUrl()
url = urlUseCase.getUrl(urlUseCase.isInternal() || urlUseCase.isPrioritizeInternal())

if (path != null && !path.startsWith("entityId:")) {
url = UrlHandler.handle(url, path)
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_priority.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/colorAccent"
android:pathData="M12,19m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0"/>
<path
android:fillColor="@color/colorAccent"
android:pathData="M10,3h4v12h-4z"/>
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -619,4 +619,6 @@ like to connect to:</string>
<string name="webview_error_PROXY_AUTHENTICATION">User authentication failed on proxy, please check proxy settings.</string>
<string name="webview_error_AUTH_SCHEME">Unsupported authentication scheme (not basic or digest), please check network settings.</string>
<string name="webview_error_HOST_LOOKUP">Server or proxy hostname lookup failed, please check that the URL is a valid Home Assistant URL.</string>
<string name="prioritize_internal">Prioritize Internal URL</string>
<string name="prioritize_internal_summary">Always try the internal URL first before the external URL. Enable this setting if you typically leave location off.</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
android:key="connection_internal"
android:title="@string/pref_connection_internal"
app:useSimpleSummaryProvider="true"/>
<SwitchPreference
android:key="prioritize_internal"
android:title="@string/prioritize_internal"
android:icon="@drawable/ic_priority"
android:summary="@string/prioritize_internal_summary" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/device_registration">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ interface UrlRepository {
suspend fun saveHomeWifiSsids(ssid: Set<String>)

suspend fun isInternal(): Boolean

suspend fun isPrioritizeInternal(): Boolean

suspend fun setPrioritizeInternal(enabled: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class UrlRepositoryImpl @Inject constructor(
private const val PREF_WEBHOOK_ID = "webhook_id"
private const val PREF_LOCAL_URL = "local_url"
private const val PREF_WIFI_SSIDS = "wifi_ssids"
private const val PREF_PRIORITIZE_INTERNAL = "prioritize_internal"
private const val TAG = "UrlRepository"
}

Expand All @@ -35,7 +36,7 @@ class UrlRepositoryImpl @Inject constructor(
}

// If we are local then add the local URL in the first position, otherwise no reason to try
if (isInternal()) {
if (isInternal() || isPrioritizeInternal()) {
localStorage.getString(PREF_LOCAL_URL)?.let {
retVal.add(
it.toHttpUrl().newBuilder()
Expand Down Expand Up @@ -111,6 +112,14 @@ class UrlRepositoryImpl @Inject constructor(
localStorage.putStringSet(PREF_WIFI_SSIDS, ssid)
}

override suspend fun setPrioritizeInternal(enabled: Boolean) {
localStorage.putBoolean(PREF_PRIORITIZE_INTERNAL, enabled)
}

override suspend fun isPrioritizeInternal(): Boolean {
return localStorage.getBoolean(PREF_PRIORITIZE_INTERNAL)
}

override suspend fun isInternal(): Boolean {
val formattedSsid = wifiHelper.getWifiSsid().removeSurrounding("\"")
val wifiSsids = getHomeWifiSsids()
Expand Down