Skip to content

Commit

Permalink
moving the storage of settings from frontend to the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jannickheisch authored and tschudin committed Jul 29, 2024
1 parent e445c8c commit 8bee9a8
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 194 deletions.
2 changes: 1 addition & 1 deletion android/tinySSB/app/src/main/assets/web/board_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 6 additions & 6 deletions android/tinySSB/app/src/main/assets/web/tremola.html
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,15 @@
<div class="settings">
<div class="settingsText">Connect to Peers via BLE</div>
<div style="float: right;"><label class="switch">
<input id="ble" type="checkbox" onchange="toggle_changed(this);">
<input id="ble_enabled" type="checkbox" onchange="toggle_changed(this);">
<span class="slider round"></span></label></div>
</div>
<hr>

<div class="settings">
<div class="settingsText">Connect to Peers via UDP Multicast</div>
<div><label class="switch">
<input id="udp_multicast" type="checkbox" onchange="toggle_changed(this);">
<input id="udp_multicast_enabled" type="checkbox" onchange="toggle_changed(this);">
<span class="slider round"></span>
</label></div>
</div>
Expand All @@ -450,7 +450,7 @@
<div class="settings">
<div class="settingsText">Connect to Pub via Websocket</div>
<div style="float: right;"><label class="switch">
<input id="websocket" type="checkbox" onchange="toggle_changed(this);">
<input id="websocket_enabled" type="checkbox" onchange="toggle_changed(this);">
<span class="slider round"></span></label></div>
</div>
<div id="container:settings_ws_url" class="websocket_url_settings" style="display:none;">
Expand All @@ -463,7 +463,7 @@
<div class="settings">
<div class="settingsText">Preview before sending</div>
<div style="float: right;"><label class="switch">
<input id="enable_preview" type="checkbox" onchange="toggle_changed(this);">
<input id="show_chat_preview" type="checkbox" onchange="toggle_changed(this);">
<span class="slider round"></span></label></div>
</div>
<hr>
Expand All @@ -479,7 +479,7 @@
<div class="settings">
<div class="settingsText">Hide forgotten Kanban Boards</div>
<div style="float: right;"><label class="switch">
<input id="hide_forgotten_boards" type="checkbox" onchange="toggle_changed(this);">
<input id="hide_forgotten_kanbans" type="checkbox" onchange="toggle_changed(this);">
<span class="slider round"></span></label></div>
</div>
<hr>
Expand All @@ -503,7 +503,7 @@
<div class="settings">
<div class="settingsText">Background map</div>
<div style="float: right;"><label class="switch">
<input id="background_map" type="checkbox" onchange="toggle_changed(this);">
<input id="show_background_map" type="checkbox" onchange="toggle_changed(this);">
<span class="slider round"></span></label></div>
</div>
<hr>
Expand Down
71 changes: 10 additions & 61 deletions android/tinySSB/app/src/main/assets/web/tremola.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -877,7 +878,7 @@ function resetTremola() { // wipes browser-side content
"contacts": {},
"profile": {},
"id": myId,
"settings": get_default_settings(),
"settings": {},
"board": {}
}
var n = recps2nm([myId])
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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'));
Expand All @@ -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])
Expand All @@ -1282,4 +1228,7 @@ function b2f_initialize(id) {
// load_chat("ALL");
}




// --- eof
53 changes: 26 additions & 27 deletions android/tinySSB/app/src/main/assets/web/tremola_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -38,16 +43,17 @@ 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
document.getElementById("container:settings_ws_url").style.display = 'none'
}
}

// 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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion android/tinySSB/app/src/main/assets/web/tremola_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 8bee9a8

Please sign in to comment.