diff --git a/android/tinySSB/app/src/main/assets/web/board_ui.js b/android/tinySSB/app/src/main/assets/web/board_ui.js
index b59bdff..79022d4 100644
--- a/android/tinySSB/app/src/main/assets/web/board_ui.js
+++ b/android/tinySSB/app/src/main/assets/web/board_ui.js
@@ -88,7 +88,7 @@ function load_board_list() {
var board = tremola.board[bid]
var date = new Date(bidTimestamp[i][1])
date = date.toDateString() + ' ' + date.toTimeString().substring(0, 5);
- if (board.forgotten && tremola.settings.hide_forgotten_boards)
+ if (board.forgotten && tremola.settings.hide_forgotten_kanbans)
continue
var cl, mem, item, bg, row, badge, badgeId, cnt;
cl = document.getElementById('lst:kanban');
diff --git a/android/tinySSB/app/src/main/assets/web/tremola.html b/android/tinySSB/app/src/main/assets/web/tremola.html
index 2df3e25..e9f79f2 100644
--- a/android/tinySSB/app/src/main/assets/web/tremola.html
+++ b/android/tinySSB/app/src/main/assets/web/tremola.html
@@ -433,7 +433,7 @@
@@ -463,7 +463,7 @@
@@ -479,7 +479,7 @@
@@ -503,7 +503,7 @@
diff --git a/android/tinySSB/app/src/main/assets/web/tremola.js b/android/tinySSB/app/src/main/assets/web/tremola.js
index e2b93f4..3af8948 100644
--- a/android/tinySSB/app/src/main/assets/web/tremola.js
+++ b/android/tinySSB/app/src/main/assets/web/tremola.js
@@ -16,6 +16,7 @@ var colors = ["#d9ceb2", "#99b2b7", "#e6cba5", "#ede3b4", "#8b9e9b", "#bd7578",
var curr_img_candidate = null;
var pubs = []
var wants = {}
+var loaded_settings = {} // the settings provided bz the backend, will overwrite tremola.settings after initialization
var restream = false // whether the backend is currently restreaming all posts
@@ -877,7 +878,7 @@ function resetTremola() { // wipes browser-side content
"contacts": {},
"profile": {},
"id": myId,
- "settings": get_default_settings(),
+ "settings": {},
"board": {}
}
var n = recps2nm([myId])
@@ -942,63 +943,6 @@ function b2f_ble_disabled() {
//ble_status = "disabled"
}
-/*
-var want = {} // all received want vectors, id: [[want vector], timestamp], want vectors older than 90 seconds are discarded
-var max_want = [] // current max vector
-var old_curr = [] // own want vector at the time when the maximum want vector was last updated
-
-function b2f_want_update(identifier, wantVector) {
-
- console.log("b2f received want:", wantVector, "from: ", identifier)
-
- // remove old want vectors
- var deleted = false;
- for (var id in want) {
- var ts = want[id][1]
- if(Date.now() - ts > 90000) {
- console.log("removed want of", id)
- delete want[id]
- deleted = true
- }
-
- }
-
- // if the want vector didn't change, no further updates are required
- if(identifier in want) {
- if( equalArrays(want[identifier][0], wantVector)) {
- console.log("update only")
- want[identifier][1] = Date.now()
- if(!deleted) //if a want vector was previously removed, the max_want needs to be recalculated otherwise it is just an update without an effect
- return
- }
- }
-
- want[identifier] = [wantVector, Date.now()]
-
- // calculate new max want vector
- var all_vectors = Object.values(want).map(val => val[0])
- var new_max_want = all_vectors.reduce((accumulator, curr) => accumulator.len >= curr.len ? accumulator : curr) //return want vector with most entries
-
- for (var vec of all_vectors) {
- for(var i in vec) {
- if (vec[i] > new_max_want[i])
- new_max_want[i] = vec[i]
- }
- }
-
- // update
- if (!equalArrays(max_want,new_max_want)) {
- old_curr = want['me'][0]
- max_want = new_max_want
- console.log("new max")
- }
-
- refresh_connection_progressbar()
-
- console.log("max:", max_want)
-}
-*/
-
function b2f_local_peer_remaining_updates(identifier, remaining) {
//TODO
}
@@ -1253,7 +1197,7 @@ function b2f_new_image_blob(ref) {
overlayIsActive = true;
}
-function b2f_initialize(id) {
+function b2f_initialize(id, settings) {
myId = id
if (window.localStorage.tremola) {
tremola = JSON.parse(window.localStorage.getItem('tremola'));
@@ -1265,11 +1209,13 @@ function b2f_initialize(id) {
if (tremola == null) {
resetTremola();
console.log("reset tremola")
+ if (typeof Android == 'undefined')
+ tremola.settings = BrowserOnlySettings // browser-only testing
}
if (typeof Android == 'undefined')
console.log("loaded ", JSON.stringify(tremola))
- if (!('settings' in tremola))
- tremola.settings = {}
+ else
+ tremola.settings = JSON.parse(settings)
var nm, ref;
for (nm in tremola.settings)
setSetting(nm, tremola.settings[nm])
@@ -1282,4 +1228,7 @@ function b2f_initialize(id) {
// load_chat("ALL");
}
+
+
+
// --- eof
diff --git a/android/tinySSB/app/src/main/assets/web/tremola_settings.js b/android/tinySSB/app/src/main/assets/web/tremola_settings.js
index 91fc78f..c92b7b9 100644
--- a/android/tinySSB/app/src/main/assets/web/tremola_settings.js
+++ b/android/tinySSB/app/src/main/assets/web/tremola_settings.js
@@ -2,20 +2,23 @@
"use strict";
-function get_default_settings() {
- return {
- 'enable_preview': false,
- 'background_map': true,
- 'websocket': true,
- 'show_shortnames': true,
- 'hide_forgotten_conv': true,
- 'hide_forgotten_contacts': true,
- 'udp_multicast': true,
- 'ble': true,
- 'websocket_url': "ws://meet.dmi.unibas.ch:8989"
- }
+
+// These default settings are only used for browser-only testing
+// Normally, these settings below WILL BE IGNORED and loaded via the provided backend.
+const BrowserOnlySettings = {
+ 'show_chat_preview': false,
+ 'show_background_map': true,
+ 'websocket_enabled': true,
+ 'show_shortnames': true,
+ 'hide_forgotten_conv': true,
+ 'hide_forgotten_contacts': true,
+ 'hide_forgotten_kanbans': true,
+ 'udp_multicast_enabled': true,
+ 'ble_enabled': true,
+ 'websocket_url': "ws://meet.dmi.unibas.ch:8989"
}
+// button/toggle handler for boolean settings; settingID is determined by the id of the html object that emitted the event (e.id)
function toggle_changed(e) {
// console.log("toggle ", e.id);
tremola.settings[e.id] = e.checked;
@@ -24,12 +27,14 @@ function toggle_changed(e) {
applySetting(e.id, e.checked);
}
-function getSetting(nm) {
- return document.getElementById(nm).checked
+// getter
+function getSetting(settingID) {
+ return tremola.settings[settingID]
}
+// frontend handler when settings have changed
function applySetting(nm, val) {
- if (nm == 'background_map') {
+ if (nm == 'show_background_map') {
if (val)
document.body.style.backgroundImage = "url('img/splash-as-background.jpg')";
else
@@ -38,7 +43,7 @@ function applySetting(nm, val) {
load_chat_list();
} else if (nm == 'hide_forgotten_contacts') {
load_contact_list();
- } else if (nm == 'websocket') {
+ } else if (nm == 'websocket_enabled') {
if (val)
document.getElementById("container:settings_ws_url").style.display = 'flex'
else
@@ -46,8 +51,9 @@ function applySetting(nm, val) {
}
}
+// setter, this will also save the given settingID and its value in the backend
function setSetting(nm, val) {
- // console.log("setting", nm, val)
+ console.log("setting", nm, val)
if (nm == "websocket_url") {
document.getElementById("settings_urlInput").value = val
return
@@ -56,20 +62,13 @@ function setSetting(nm, val) {
document.getElementById(nm).checked = val;
}
-/* async */
+// calls the backend to wipe everything, including the ID
function settings_wipe() {
closeOverlay();
- backend("wipe"); // will not return
- /*
- window.localStorage.setItem("tremola", "null");
- backend("ready"); // will call initialize()
- await new Promise(resolve => setTimeout(resolve, 500));
- // resetTremola();
- menu_redraw();
- setScenario('chats');
- */
+ backend("wipe"); // will not return, because of app restart
}
+// button handler for websocket url textfield
function btn_setWebsocketUrl() {
var new_url = document.getElementById("settings_urlInput").value
diff --git a/android/tinySSB/app/src/main/assets/web/tremola_ui.js b/android/tinySSB/app/src/main/assets/web/tremola_ui.js
index cb1b38b..83b53a8 100644
--- a/android/tinySSB/app/src/main/assets/web/tremola_ui.js
+++ b/android/tinySSB/app/src/main/assets/web/tremola_ui.js
@@ -267,7 +267,7 @@ function closeOverlay() {
function showPreview() {
var draft = escapeHTML(document.getElementById('draft').value);
if (draft.length == 0) return;
- if (!getSetting("enable_preview")) {
+ if (!getSetting("show_chat_preview")) {
new_text_post(draft);
return;
}
diff --git a/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/Settings.kt b/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/Settings.kt
index 5c87dd2..4042e67 100644
--- a/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/Settings.kt
+++ b/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/Settings.kt
@@ -1,132 +1,106 @@
package nz.scuttlebutt.tremolavossbol
import android.content.Context
-import androidx.core.content.withStyledAttributes
-import nz.scuttlebutt.tremolavossbol.utils.Constants.Companion.TINYSSB_DIR
import nz.scuttlebutt.tremolavossbol.utils.Constants.Companion.TINYSSB_SIMPLEPUB_URL
-import java.io.File
+import org.json.JSONObject
class Settings(val context: MainActivity) {
private val sharedPreferences = context.getSharedPreferences("tinyssbSettings", Context.MODE_PRIVATE)
- private val WEBSOCKET_ENABLED_BY_DEFAULT = true
- private val BLE_ENABLED_BY_DEFAULT = true
- private val UDP_MULTICAST_ENABLED_BY_DEFAULT = true
-
- private val PREVIEW_ENABLED_BY_DEFAULT = false
- private val BACKGROUD_MAP_ENABLED_BY_DEFAULT = false
- private val HIDE_FORGOTTEN_CONVERSATIONS_BY_DEFAULT = true
- private val HIDE_FORGOTTEN_CONTACTS_BY_DEFAULT = true
- private val HIDE_FORGOTTEN_KANBAN_BY_DEFAULT = true
- private val SHOW_SHORTNAME_BY_DEFAULT = true
-
- private val WEBSOCKET_URL_DEFAULT = "ws://meet.dmi.unibas.ch:8989"
-
-
-
- // put for different data types
- private fun put(key: String, value: Boolean) {
- sharedPreferences.edit().putBoolean(key, value).apply()
- }
-
- private fun put(key: String, value: Float) {
- sharedPreferences.edit().putFloat(key, value).apply()
- }
-
- private fun put(key: String, value:Int) {
- sharedPreferences.edit().putInt(key, value).apply()
- }
-
- private fun put(key: String, value: Long) {
- sharedPreferences.edit().putLong(key, value).apply()
- }
-
- private fun put(key: String, value: String) {
- sharedPreferences.edit().putString(key, value).apply()
- }
-
- private fun put(key: String, value: Set
) {
- sharedPreferences.edit().putStringSet(key, value).apply()
- }
-
- private fun delete() {
- sharedPreferences.edit().clear().apply()
- }
-
- fun getAll(): MutableMap? {
- return sharedPreferences.all
- }
-
- fun resetToDefault() {
- delete()
+ // These settings are only used in the frontend and will not affect the backend
+ // The settingID must match the one used in the frontend (tremola.settings).
+ private var defaultSettings = mapOf(
+ "show_chat_preview" to "false",
+ "show_background_map" to "true",
+ "show_shortnames" to "true",
+ "hide_forgotten_conv" to "true",
+ "hide_forgotten_contacts" to "true",
+ "hide_forgotten_kanbans" to "true",
+ // backend settings
+ "ble_enabled" to "true",
+ "udp_multicast_enabled" to "true",
+ "websocket_enabled" to "true",
+ "websocket_url" to TINYSSB_SIMPLEPUB_URL
+ )
+ fun getSettings(): String {
+ val currentSettings = mutableMapOf()
+ for ( (settingID, default) in defaultSettings) {
+ val value = sharedPreferences.getString(settingID, default)!!
+ // convert string back to boolean
+ if (value.lowercase() == "true") {
+ currentSettings[settingID] = true
+ } else if (value.lowercase() == "false"){
+ currentSettings[settingID] = false
+ } else {
+ currentSettings[settingID] = value
+ }
+ }
+ return JSONObject(currentSettings.toMap()).toString()
+ }
+
+ // Sets a value for the provided settingID and executes necessary backend actions to align with the updated setting.
+ fun set(settingID: String, value: String): Boolean {
+ if (!defaultSettings.keys.contains(settingID)) {
+ return false // default of setting id must be defined
+ }
+ val success = sharedPreferences.edit().putString(settingID, value).commit()
+ if (!success)
+ return false
+
+ when (settingID) {
+ "websocket_enabled" -> handleWebsocketEnabled(value.toBoolean())
+ "ble_enabled" -> handleBleEnabled(value.toBoolean())
+ "udp_multicast_enabled" -> handleUdpMulticastEnabled(value.toBoolean())
+ "websocket_url" -> handleWebsocketUrl(value)
+ }
+ return true
+ }
+
+ fun resetToDefault(): Boolean {
+ return sharedPreferences.edit().clear().commit()
}
+ // ------------------------------------------------------------------------------------//
+ // Getter (backend settings) //
// ------------------------------------------------------------------------------------//
fun isWebsocketEnabled(): Boolean {
- return sharedPreferences.getBoolean("websocket", WEBSOCKET_ENABLED_BY_DEFAULT)
+ return sharedPreferences.getString("websocket_enabled", defaultSettings["websocket_enabled"]).toBoolean()
}
fun isBleEnabled(): Boolean {
- return sharedPreferences.getBoolean("ble", BLE_ENABLED_BY_DEFAULT)
+ return sharedPreferences.getString("ble_enabled", defaultSettings["ble_enabled"]).toBoolean()
}
fun isUdpMulticastEnabled(): Boolean {
- return sharedPreferences.getBoolean("udp_multicast", UDP_MULTICAST_ENABLED_BY_DEFAULT)
- }
-
- fun isHideForgottenConversationsEnabled(): Boolean {
- return sharedPreferences.getBoolean("hide_forgotten_conv", HIDE_FORGOTTEN_CONVERSATIONS_BY_DEFAULT)
- }
-
- fun isHideForgottenContactsEnabled(): Boolean {
- return sharedPreferences.getBoolean("hide_forgotten_contacts", HIDE_FORGOTTEN_CONTACTS_BY_DEFAULT)
- }
-
- fun isHideForgottenKanbanEnabled(): Boolean {
- return sharedPreferences.getBoolean("hide_forgotten_boards", HIDE_FORGOTTEN_KANBAN_BY_DEFAULT)
- }
-
- fun isPreviewBeforeSendingEnabled(): Boolean {
- return sharedPreferences.getBoolean("enable_preview", PREVIEW_ENABLED_BY_DEFAULT)
- }
-
- fun isBackgroundMapEnabled(): Boolean {
- return sharedPreferences.getBoolean("background_map", BACKGROUD_MAP_ENABLED_BY_DEFAULT)
- }
-
- fun isShowShortnameEnabled(): Boolean {
- return sharedPreferences.getBoolean("show_shortnames", SHOW_SHORTNAME_BY_DEFAULT)
+ return sharedPreferences.getString("udp_multicast_enabled", defaultSettings["udp_multicast_enabled"]).toBoolean()
}
fun getWebsocketUrl(): String {
- return sharedPreferences.getString("websocket_url", WEBSOCKET_URL_DEFAULT)!!
+ return sharedPreferences.getString("websocket_url", defaultSettings["websocket_url"])!!
}
// ------------------------------------------------------------------------------------------//
-
- fun setWebsocketEnabled(value: Boolean) {
- put("websocket", value)
+ // Backend settings handler (called from set()) //
+ // ------------------------------------------------------------------------------------------//
+ fun handleWebsocketEnabled(value: Boolean) {
if (!value)
context.websocket?.stop()
context.websocket?.start()
}
- fun setBleEnabled(value: Boolean) {
- put("ble", value)
+ fun handleBleEnabled(value: Boolean) {
if(!value)
context.ble?.stopBluetooth()
context.ble?.startBluetooth()
}
- fun setUdpMulticastEnabled(value: Boolean) {
- put("udp_multicast", value)
+ fun handleUdpMulticastEnabled(value: Boolean) {
if (!value)
context.rmSockets()
context.mkSockets()
}
- fun setWebsocketUrl(value: String) {
- put("websocket_url", value)
+ fun handleWebsocketUrl(value: String) {
context.websocket?.updateUrl(value)
}
diff --git a/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/WebAppInterface.kt b/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/WebAppInterface.kt
index 09f950a..b8da373 100644
--- a/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/WebAppInterface.kt
+++ b/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/WebAppInterface.kt
@@ -43,7 +43,7 @@ class WebAppInterface(val act: MainActivity, val webView: WebView) {
(act as MainActivity)._onBackPressed()
}
"ready" -> {
- eval("b2f_initialize(\"${act.idStore.identity.toRef()}\")")
+ eval("b2f_initialize('${act.idStore.identity.toRef()}', '${act.settings!!.getSettings()}')")
frontend_ready = true
act.tinyRepo.addNumberOfPendingChunks(0) // initialize chunk progress bar
act.tinyNode.beacon()
@@ -240,12 +240,7 @@ class WebAppInterface(val act: MainActivity, val webView: WebView) {
}
}
"settings:set" -> {
- when(args[1]) {
- "ble" -> {act.settings!!.setBleEnabled(args[2].toBooleanStrict())}
- "udp_multicast" -> {act.settings!!.setUdpMulticastEnabled(args[2].toBooleanStrict())}
- "websocket" -> {act.settings!!.setWebsocketEnabled(args[2].toBooleanStrict())}
- "websocket_url" -> {act.settings!!.setWebsocketUrl(args[2])}
- }
+ act.settings!!.set(args[1], args[2])
}
else -> {
Log.d("onFrontendRequest", "unknown")
diff --git a/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/utils/Constants.kt b/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/utils/Constants.kt
index b9959a6..840413a 100644
--- a/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/utils/Constants.kt
+++ b/android/tinySSB/app/src/main/java/nz/scuttlebutt/tremolavossbol/utils/Constants.kt
@@ -40,7 +40,7 @@ class Constants{
val TINYSSB_BLE_RX_NAME_DESCRIPTOR = UUID.fromString("6e400002-7646-4b5b-9a50-71becce51559")
val TINYSSB_BLE_TX_CHARACTERISTIC = UUID.fromString("6e400003-7646-4b5b-9a50-71becce51558") // for receiving from the remote device
- val TINYSSB_SIMPLEPUB_URL = "ws://meet.dmi.unibas.ch:8080"
+ val TINYSSB_SIMPLEPUB_URL = "ws://meet.dmi.unibas.ch:8989"
val TINYSSB_DIR = "tinyssb"
}