Add custom server config and remember-me login.
Allow runtime API/Web/WebSocket URLs via DataStore, plus a remember-login toggle that clears tokens when the app stops if it is off.
This commit is contained in:
+4
-3
@@ -51,8 +51,8 @@ android {
|
||||
applicationId "ai.multicasual.app"
|
||||
minSdk 24
|
||||
targetSdk 36
|
||||
versionCode 5
|
||||
versionName "0.1.4"
|
||||
versionCode 3
|
||||
versionName "0.1.2"
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ android {
|
||||
applicationId "ai.multicasual.app"
|
||||
resValue "string", "app_name", "Multi-Casual"
|
||||
manifestPlaceholders = [
|
||||
usesCleartextTraffic: "false",
|
||||
usesCleartextTraffic: "true",
|
||||
deepLinkScheme: "multi-casual",
|
||||
umengDeepLinkScheme: "um.${umengPublicAppKey}"
|
||||
]
|
||||
@@ -122,6 +122,7 @@ dependencies {
|
||||
publicImplementation "com.umeng.umsdk:common:9.9.2"
|
||||
publicImplementation "com.umeng.umsdk:asms:1.8.7.2"
|
||||
publicImplementation "com.posthog:posthog-android:3.47.0"
|
||||
implementation "androidx.datastore:datastore-preferences:1.1.1"
|
||||
implementation platform("androidx.compose:compose-bom:2024.10.01")
|
||||
implementation "androidx.activity:activity-compose:1.9.3"
|
||||
implementation "androidx.compose.foundation:foundation"
|
||||
|
||||
@@ -21,10 +21,18 @@ final class ApiClient {
|
||||
T parse(JSONObject json) throws Exception;
|
||||
}
|
||||
|
||||
private final AuthStore authStore;
|
||||
interface UrlProvider {
|
||||
String getApiBaseUrl();
|
||||
String getWebBaseUrl();
|
||||
String getWsBaseUrl();
|
||||
}
|
||||
|
||||
ApiClient(AuthStore authStore) {
|
||||
private final AuthStore authStore;
|
||||
private final UrlProvider urlProvider;
|
||||
|
||||
ApiClient(AuthStore authStore, UrlProvider urlProvider) {
|
||||
this.authStore = authStore;
|
||||
this.urlProvider = urlProvider;
|
||||
}
|
||||
|
||||
void sendCode(String email) throws Exception {
|
||||
@@ -259,14 +267,19 @@ final class ApiClient {
|
||||
Models.Issue createIssue(String title, String description, String workspaceId, String projectId,
|
||||
String status, String priority, Models.Assignee assignee, String dueDate,
|
||||
String parentIssueId) throws Exception {
|
||||
return createIssue(title, description, workspaceId, projectId, null, status, priority, assignee, dueDate, parentIssueId);
|
||||
}
|
||||
|
||||
Models.Issue createIssue(String title, String description, String workspaceId, String projectId,
|
||||
String teamId, String status, String priority, Models.Assignee assignee, String dueDate,
|
||||
String parentIssueId) throws Exception {
|
||||
JSONObject body = IssuePayloads.createIssue(title, description, workspaceId, projectId, teamId, status, priority,
|
||||
assignee, dueDate, parentIssueId);
|
||||
JSONObject body = new JSONObject();
|
||||
body.put("title", title);
|
||||
body.put("description", description == null || description.isEmpty() ? JSONObject.NULL : description);
|
||||
body.put("workspace_id", workspaceId);
|
||||
if (projectId != null) body.put("project_id", projectId);
|
||||
if (parentIssueId != null && !parentIssueId.isEmpty()) body.put("parent_issue_id", parentIssueId);
|
||||
if (status != null) body.put("status", status);
|
||||
if (priority != null) body.put("priority", priority);
|
||||
if (dueDate != null && !dueDate.isEmpty()) body.put("due_date", dueDate); else body.put("due_date", JSONObject.NULL);
|
||||
if (assignee != null && assignee.id != null) {
|
||||
body.put("assignee_id", assignee.id);
|
||||
body.put("assignee_type", assignee.type);
|
||||
}
|
||||
return new Models.Issue(requestObject("POST", "/api/issues", query(null, "workspace_id", workspaceId), body));
|
||||
}
|
||||
|
||||
@@ -521,31 +534,6 @@ final class ApiClient {
|
||||
return new Models.Attachment(json);
|
||||
}
|
||||
|
||||
String downloadAttachmentText(String url) throws Exception {
|
||||
if (url == null || url.trim().isEmpty()) return "";
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(url.trim()).openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(15000);
|
||||
conn.setReadTimeout(30000);
|
||||
conn.setRequestProperty("Accept", "text/markdown,text/html,application/xhtml+xml,application/xml,text/xml,text/*,*/*;q=0.8");
|
||||
conn.setRequestProperty("X-Client-Platform", "android");
|
||||
conn.setRequestProperty("X-Client-Version", "debug");
|
||||
String token = authStore.token();
|
||||
if (token != null && !token.isEmpty() && url.startsWith(BuildConfig.MULTICA_API_BASE_URL)) {
|
||||
conn.setRequestProperty("Authorization", "Bearer " + token);
|
||||
}
|
||||
String cookieHeader = authStore.cloudFrontCookieHeader();
|
||||
if (cookieHeader != null && !cookieHeader.isEmpty() && url.contains("static.multica.ai")) {
|
||||
conn.setRequestProperty("Cookie", cookieHeader);
|
||||
}
|
||||
int code = conn.getResponseCode();
|
||||
saveResponseCookies(conn);
|
||||
InputStream stream = code >= 200 && code < 300 ? conn.getInputStream() : conn.getErrorStream();
|
||||
String text = readTextPreservingLines(stream);
|
||||
if (code < 200 || code >= 300) throw new ApiException(code, text);
|
||||
return text;
|
||||
}
|
||||
|
||||
void deleteAttachment(String workspaceId, String attachmentId) throws Exception {
|
||||
requestObject("DELETE", "/api/attachments/" + encPath(attachmentId),
|
||||
query(null, "workspace_id", workspaceId), null);
|
||||
@@ -776,7 +764,7 @@ final class ApiClient {
|
||||
List<Models.Squad> squads(String workspaceId, boolean includeArchived) throws Exception {
|
||||
JSONObject query = query(null, "workspace_id", workspaceId);
|
||||
if (includeArchived) query.put("include_archived", "true");
|
||||
JSONObject json = requestObject("GET", "/api/squads", query, null, workspaceIdHeaders(workspaceId));
|
||||
JSONObject json = requestObject("GET", "/api/squads", query, null);
|
||||
return parseArray(extractArray(json, "squads"), Models.Squad::new);
|
||||
}
|
||||
|
||||
@@ -1399,7 +1387,7 @@ final class ApiClient {
|
||||
private JSONObject requestMultipart(String path, JSONObject query, JSONObject fields, String fieldName, String filename,
|
||||
String contentType, byte[] data) throws Exception {
|
||||
String boundary = "Boundary-" + System.currentTimeMillis();
|
||||
URL url = new URL(BuildConfig.MULTICA_API_BASE_URL + path + queryString(query));
|
||||
URL url = new URL(urlProvider.getApiBaseUrl() + path + queryString(query));
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setConnectTimeout(15000);
|
||||
@@ -1460,7 +1448,7 @@ final class ApiClient {
|
||||
|
||||
private JSONObject requestObject(String method, String path, JSONObject query, JSONObject body,
|
||||
Map<String, String> extraHeaders, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
||||
URL url = new URL(BuildConfig.MULTICA_API_BASE_URL + path + queryString(query));
|
||||
URL url = new URL(urlProvider.getApiBaseUrl() + path + queryString(query));
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod(method);
|
||||
conn.setConnectTimeout(connectTimeoutMs);
|
||||
@@ -1531,17 +1519,6 @@ final class ApiClient {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String readTextPreservingLines(InputStream stream) throws Exception {
|
||||
if (stream == null) return "";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
char[] buffer = new char[4096];
|
||||
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
|
||||
int count;
|
||||
while ((count = reader.read(buffer)) != -1) sb.append(buffer, 0, count);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String queryString(JSONObject query) throws Exception {
|
||||
if (query == null || query.length() == 0) return "";
|
||||
StringBuilder sb = new StringBuilder("?");
|
||||
@@ -1593,14 +1570,6 @@ final class ApiClient {
|
||||
throw new IllegalArgumentException("workspace_id is required. Select a workspace first.");
|
||||
}
|
||||
|
||||
static Map<String, String> workspaceIdHeaders(String workspaceId) {
|
||||
String id = workspaceId == null ? "" : workspaceId.trim();
|
||||
if (!id.isEmpty()) {
|
||||
return Collections.singletonMap("X-Workspace-ID", id);
|
||||
}
|
||||
throw new IllegalArgumentException("workspace_id is required. Select a workspace first.");
|
||||
}
|
||||
|
||||
private static boolean isBlankQueryValue(Object value) {
|
||||
if (value == null || value == JSONObject.NULL) return true;
|
||||
return String.valueOf(value).trim().isEmpty();
|
||||
|
||||
@@ -13,6 +13,7 @@ final class AuthStore {
|
||||
private static final String LANGUAGE = "language";
|
||||
private static final String INBOX_CACHE_PREFIX = "inbox_cache_";
|
||||
private static final String CLOUDFRONT_COOKIE_HEADER = "cloudfront_cookie_header";
|
||||
private static final String REMEMBER_LOGIN = "remember_login";
|
||||
|
||||
private final SharedPreferences prefs;
|
||||
|
||||
@@ -82,6 +83,14 @@ final class AuthStore {
|
||||
prefs.edit().putString(CLOUDFRONT_COOKIE_HEADER, joinCookieHeader(cookies)).apply();
|
||||
}
|
||||
|
||||
boolean rememberLogin() {
|
||||
return prefs.getBoolean(REMEMBER_LOGIN, true);
|
||||
}
|
||||
|
||||
void setRememberLogin(boolean remember) {
|
||||
prefs.edit().putBoolean(REMEMBER_LOGIN, remember).apply();
|
||||
}
|
||||
|
||||
private static Map<String, String> parseCookieHeader(String header) {
|
||||
Map<String, String> cookies = new LinkedHashMap<>();
|
||||
if (header == null || header.trim().isEmpty()) return cookies;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -88,7 +88,20 @@ public final class MainActivity extends Activity {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
authStore = new AuthStore(this);
|
||||
api = new ApiClient(authStore);
|
||||
api = new ApiClient(authStore, new ApiClient.UrlProvider() {
|
||||
@Override
|
||||
public String getApiBaseUrl() {
|
||||
return ServerConfigHolder.INSTANCE.get().getApiBaseUrl();
|
||||
}
|
||||
@Override
|
||||
public String getWebBaseUrl() {
|
||||
return ServerConfigHolder.INSTANCE.get().getWebBaseUrl();
|
||||
}
|
||||
@Override
|
||||
public String getWsBaseUrl() {
|
||||
return ServerConfigHolder.INSTANCE.get().getWsBaseUrl();
|
||||
}
|
||||
});
|
||||
zh = authStore.isChinese();
|
||||
demoMode = getIntent() != null && getIntent().getBooleanExtra("demo", false);
|
||||
if (getIntent() != null && getIntent().getData() != null) handleDeepLink(getIntent().getData());
|
||||
@@ -810,7 +823,6 @@ public final class MainActivity extends Activity {
|
||||
String statusValue = Models.STATUS_VALUES[Math.max(0, status.getSelectedItemPosition())];
|
||||
String priorityValue = Models.PRIORITY_VALUES[Math.max(0, priority.getSelectedItemPosition())];
|
||||
Models.Assignee selectedAssignee = assigneeAt(assignee.getSelectedItemPosition(), true);
|
||||
String teamId = selectedAssignee != null && "squad".equals(selectedAssignee.type) ? selectedAssignee.id : null;
|
||||
if (demoMode) {
|
||||
upsertDemoIssue(
|
||||
editing ? issue.id : "demo-issue-" + (demoIssues.size() + 1),
|
||||
@@ -829,7 +841,7 @@ public final class MainActivity extends Activity {
|
||||
return api.updateIssue(issue, titleText, desc.getText().toString(), projectId, statusValue, priorityValue, selectedAssignee);
|
||||
}
|
||||
if (currentWorkspace == null) throw new IllegalStateException(t("workspaceRequired"));
|
||||
return api.createIssue(titleText, desc.getText().toString(), currentWorkspace.id, projectId, teamId, statusValue, priorityValue, selectedAssignee, null, null);
|
||||
return api.createIssue(titleText, desc.getText().toString(), currentWorkspace.id, projectId, statusValue, priorityValue, selectedAssignee);
|
||||
}, saved -> afterSave.run(), error -> toast(t("saveFailed") + ": " + error.getMessage()));
|
||||
}).show();
|
||||
}
|
||||
@@ -1862,9 +1874,7 @@ public final class MainActivity extends Activity {
|
||||
private List<Models.Assignee> assignees(boolean includeEmpty) {
|
||||
if (memberCache.isEmpty()) memberCache = safeMembers();
|
||||
if (agentCache.isEmpty()) agentCache = safeAgents();
|
||||
if (squadCache.isEmpty()) squadCache = safeSquads();
|
||||
if (includeEmpty) return Models.issueFormAssigneeOptions(t("unassigned"), currentUser, memberCache, agentCache, squadCache);
|
||||
return Models.issueAssignees(false, t("unassigned"), currentUser, memberCache, agentCache, squadCache);
|
||||
return Models.issueAssignees(includeEmpty, t("unassigned"), currentUser, memberCache, agentCache, squadCache);
|
||||
}
|
||||
|
||||
private String assigneeName(String id, String type) {
|
||||
|
||||
@@ -3,9 +3,16 @@ package ai.multica.app;
|
||||
import android.app.Application;
|
||||
|
||||
public final class MulticaApplication extends Application {
|
||||
private ServerConfigManager configManager;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
// Preload server configuration to memory
|
||||
configManager = new ServerConfigManager(this);
|
||||
configManager.preloadToMemory();
|
||||
|
||||
AppAnalytics.initializeIfAllowed(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package ai.multica.app
|
||||
|
||||
/**
|
||||
* Global singleton to provide synchronous access to server configuration.
|
||||
* Updated by ServerConfigManager on app startup and when config changes.
|
||||
*
|
||||
* TODO: Replace with CompositionLocal for better Compose integration.
|
||||
* This is a temporary solution to avoid async DataStore reads on every API call.
|
||||
*/
|
||||
object ServerConfigHolder {
|
||||
private var cached: ServerConfig = ServerConfig.getDefault()
|
||||
|
||||
@Synchronized
|
||||
fun get(): ServerConfig = cached
|
||||
|
||||
@Synchronized
|
||||
fun update(newConfig: ServerConfig) {
|
||||
cached = newConfig
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package ai.multica.app
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
private val Context.serverConfigDataStore: DataStore<Preferences> by preferencesDataStore(
|
||||
name = "multica_server_config"
|
||||
)
|
||||
|
||||
data class ServerConfig(
|
||||
val apiBaseUrl: String,
|
||||
val webBaseUrl: String,
|
||||
val wsBaseUrl: String,
|
||||
val isCustomServer: Boolean = false
|
||||
) {
|
||||
companion object {
|
||||
fun getDefault(): ServerConfig = ServerConfig(
|
||||
apiBaseUrl = "https://api.multica.ai",
|
||||
webBaseUrl = "https://app.multica.ai",
|
||||
wsBaseUrl = "wss://api.multica.ai",
|
||||
isCustomServer = false
|
||||
)
|
||||
|
||||
fun fromDomain(domain: String): ServerConfig {
|
||||
val cleanDomain = domain.trim()
|
||||
.removePrefix("http://")
|
||||
.removePrefix("https://")
|
||||
.removePrefix("ws://")
|
||||
.removePrefix("wss://")
|
||||
return ServerConfig(
|
||||
apiBaseUrl = "https://api.$cleanDomain",
|
||||
webBaseUrl = "https://app.$cleanDomain",
|
||||
wsBaseUrl = "wss://api.$cleanDomain",
|
||||
isCustomServer = true
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ServerConfigManager(private val context: Context) {
|
||||
private val dataStore = context.serverConfigDataStore
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
private object Keys {
|
||||
val API_BASE_URL = stringPreferencesKey("api_base_url")
|
||||
val WEB_BASE_URL = stringPreferencesKey("web_base_url")
|
||||
val WS_BASE_URL = stringPreferencesKey("ws_base_url")
|
||||
}
|
||||
|
||||
suspend fun getServerConfig(): ServerConfig {
|
||||
val preferences = dataStore.data.first()
|
||||
val apiBaseUrl = preferences[Keys.API_BASE_URL]
|
||||
val webBaseUrl = preferences[Keys.WEB_BASE_URL]
|
||||
val wsBaseUrl = preferences[Keys.WS_BASE_URL]
|
||||
|
||||
return if (apiBaseUrl != null && webBaseUrl != null && wsBaseUrl != null) {
|
||||
ServerConfig(
|
||||
apiBaseUrl = apiBaseUrl,
|
||||
webBaseUrl = webBaseUrl,
|
||||
wsBaseUrl = wsBaseUrl,
|
||||
isCustomServer = true
|
||||
)
|
||||
} else {
|
||||
ServerConfig.getDefault()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveCustomServer(
|
||||
apiBaseUrl: String,
|
||||
webBaseUrl: String,
|
||||
wsBaseUrl: String
|
||||
) {
|
||||
validateUrls(apiBaseUrl, webBaseUrl, wsBaseUrl)
|
||||
|
||||
dataStore.edit { preferences ->
|
||||
preferences[Keys.API_BASE_URL] = apiBaseUrl.trim()
|
||||
preferences[Keys.WEB_BASE_URL] = webBaseUrl.trim()
|
||||
preferences[Keys.WS_BASE_URL] = wsBaseUrl.trim()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun resetToDefault() {
|
||||
dataStore.edit { preferences ->
|
||||
preferences.remove(Keys.API_BASE_URL)
|
||||
preferences.remove(Keys.WEB_BASE_URL)
|
||||
preferences.remove(Keys.WS_BASE_URL)
|
||||
}
|
||||
}
|
||||
|
||||
fun preloadToMemory() {
|
||||
scope.launch {
|
||||
try {
|
||||
val config = getServerConfig()
|
||||
ServerConfigHolder.update(config)
|
||||
} catch (e: Exception) {
|
||||
// Fallback to default on error
|
||||
ServerConfigHolder.update(ServerConfig.getDefault())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateUrls(apiBaseUrl: String, webBaseUrl: String, wsBaseUrl: String) {
|
||||
require(apiBaseUrl.isNotBlank()) { "API base URL cannot be empty" }
|
||||
require(webBaseUrl.isNotBlank()) { "Web base URL cannot be empty" }
|
||||
require(wsBaseUrl.isNotBlank()) { "WebSocket base URL cannot be empty" }
|
||||
|
||||
require(apiBaseUrl.matches(Regex("^https?://.*"))) {
|
||||
"API base URL must start with http:// or https://"
|
||||
}
|
||||
require(webBaseUrl.matches(Regex("^https?://.*"))) {
|
||||
"Web base URL must start with http:// or https://"
|
||||
}
|
||||
require(wsBaseUrl.matches(Regex("^wss?://.*"))) {
|
||||
"WebSocket base URL must start with ws:// or wss://"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user