Add API call to welcome

pull/653/merge
Varun Patil 2023-05-16 00:25:34 -07:00
parent 14bbea4c9f
commit 0a47baf16e
4 changed files with 116 additions and 17 deletions

View File

@ -25,6 +25,14 @@ p {
margin-bottom: 30px;
}
p.error {
display: none;
color: red;
font-weight: 500;
word-wrap: break-word;
user-select: text;
}
.logo {
margin-bottom: 30px;
width: 60vw;
@ -62,6 +70,11 @@ input.m-input:focus {
transition: background-color 0.3s ease-in-out;
}
.m-button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
.m-button.link {
background-color: unset;
color: var(--theme-color);

View File

@ -13,16 +13,93 @@
To begin, enter the URL of your Nextcloud server below.
</p>
<input type="url" id="server-name" class="m-input"
placeholder="e.g. https://nextcloud.home.server">
<input type="url" id="server-url" class="m-input"
placeholder="e.g. https://nextcloud.home.server"
value="http://10.0.2.2:8035/"
/>
<button class="m-button login-button">
<button class="m-button login-button" id="login">
Continue to Login
</button>
<p id="conn-error" class="error"></p>
<a class="m-button link" href="https://memories.gallery/install/">
I don't have a server
</a>
</div>
<script>
const urlBox = document.getElementById('server-url');
const loginButton = document.getElementById('login');
const connError = document.getElementById('conn-error');
function validateUrl(url) {
try {
url = new URL(url);
const protoOk = url.protocol === 'http:' || url.protocol === 'https:';
const hostOk = url.hostname.length > 0;
return protoOk && hostOk;
} catch (e) {
return false;
}
}
function updateLoginEnabled() {
loginButton.disabled = !validateUrl(urlBox.value);
}
function getMemoriesUrl() {
const url = new URL(urlBox.value);
// Add index.php to the path if it's not there already
if (!url.pathname.includes('/index.php')) {
url.pathname += '/index.php/';
}
// Add path to memories
url.pathname += 'apps/memories/';
return url;
}
// Update login button enabled state when the URL changes
urlBox.addEventListener('input', updateLoginEnabled);
updateLoginEnabled();
// Login button click handler
loginButton.addEventListener('click', async () => {
const url = getMemoriesUrl();
url.pathname += 'api/describe';
const urlStr = url.toString();
try {
urlBox.disabled = true;
loginButton.disabled = true;
// Abort request after 5 seconds
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 5000)
// Get API description
const res = await fetch(urlStr, {
method: 'GET',
signal: controller.signal,
});
const data = await res.json();
// API is fine, redirect to login page
clearTimeout(timeoutId);
window.nativex.login(data?.baseUrl || getMemoriesUrl().toString());
} catch (e) {
// CORS request, we know nothing about the server
connError.innerText = `Failed to fetch server info.\nIs Memories installed and enabled?\n${urlStr}`;
connError.style.display = 'block';
} finally {
urlBox.disabled = false;
loginButton.disabled = false;
}
});
</script>
</body>
</html>

View File

@ -138,8 +138,7 @@ import gallery.memories.databinding.ActivityMainBinding
webSettings.userAgentString = "memories-native-android/0.0"
binding.webview.clearCache(true)
binding.webview.addJavascriptInterface(mNativeX, "nativex")
// binding.webview.loadUrl("http://10.0.2.2:8035/index.php/apps/memories/")
binding.webview.loadUrl("https://uncanny-subdue.loca.lt/index.php/apps/memories/")
binding.webview.loadUrl("file:///android_asset/welcome.html");
binding.webview.setBackgroundColor(Color.TRANSPARENT)
WebView.setWebContentsDebuggingEnabled(true);
}

View File

@ -6,6 +6,7 @@ import android.view.SoundEffectConstants
import android.webkit.JavascriptInterface
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.widget.Toast
import androidx.media3.common.util.UnstableApi
import gallery.memories.mapper.SystemImage
import gallery.memories.service.DownloadService
@ -88,6 +89,15 @@ import java.net.URLDecoder
val isNative: Boolean
get() = true
@JavascriptInterface
fun login(server: String?) {
if (server == null) return;
mActivity.runOnUiThread {
Toast.makeText(mActivity, server, Toast.LENGTH_LONG).show()
}
}
@JavascriptInterface
fun setThemeColor(color: String?, isDark: Boolean) {
// Save for getting it back on next start