Skip to content

Commit

Permalink
Added API settings to settings screen
Browse files Browse the repository at this point in the history
  • Loading branch information
f-r00t committed Feb 27, 2024
1 parent 2b6cc92 commit d3d72ce
Show file tree
Hide file tree
Showing 6 changed files with 754 additions and 341 deletions.
5 changes: 4 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {

import {
SettingsScreen, SwapCurrencyScreen, ExportKeysScreen, LoggingScreen, FaqScreen,
DisableDozeScreen, SwapNodeScreen, OptimizeScreen, SwapLanguageScreen
DisableDozeScreen, SwapNodeScreen, OptimizeScreen, SwapLanguageScreen, SwapAPIScreen
} from './SettingsScreen';

import {
Expand Down Expand Up @@ -152,6 +152,7 @@ const SettingsNavigator = createStackNavigator(
ChooseAuthMethod: ChooseAuthMethodScreen,
RequestHardwareAuth: RequestHardwareAuthScreen,
Optimize: OptimizeScreen,
SwapAPI: SwapAPIScreen
},
{
initialRouteName: 'Settings',
Expand Down Expand Up @@ -421,6 +422,8 @@ export default class App extends React.Component {
if (prefs !== undefined) {
Globals.preferences = prefs;
}

console.log(Globals.preferences);

this.setState({
screenProps: {
Expand Down
42 changes: 35 additions & 7 deletions src/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ async function createTables(DB) {
theme TEXT,
pinconfirmation BOOLEAN,
language TEXT,
nickname TEXT
nickname TEXT,
cache TEXT,
cacheenabled TEXT
)`
);

Expand Down Expand Up @@ -334,6 +336,21 @@ async function createTables(DB) {

}

if (dbVersion === 7) {
tx.executeSql(
`ALTER TABLE
preferences
ADD
cache TEXT default '${Config.defaultCache}'`
);
tx.executeSql(
`ALTER TABLE
preferences
ADD
cacheenabled text default "true"`
);
}

/* Setup default preference values */
tx.executeSql(
`INSERT OR IGNORE INTO preferences (
Expand Down Expand Up @@ -399,7 +416,7 @@ async function createTables(DB) {


tx.executeSql(
`PRAGMA user_version = 7`
`PRAGMA user_version = 8`
);
});

Expand Down Expand Up @@ -434,7 +451,9 @@ export async function savePreferencesToDatabase(preferences) {
authmethod = ?,
node = ?,
language = ?,
nickname = ?
nickname = ?,
cache = ?,
cacheenabled = ?
WHERE
id = 0`,
[
Expand All @@ -448,7 +467,9 @@ export async function savePreferencesToDatabase(preferences) {
preferences.authenticationMethod,
preferences.node,
preferences.language,
preferences.nickname
preferences.nickname,
preferences.cache,
preferences.cacheEnabled
]
);
});
Expand All @@ -467,7 +488,9 @@ export async function loadPreferencesFromDatabase() {
authmethod,
node,
language,
nickname
nickname,
cache,
cacheenabled
FROM
preferences
WHERE
Expand All @@ -488,7 +511,9 @@ export async function loadPreferencesFromDatabase() {
authenticationMethod: item.authmethod,
node: item.node,
language: item.language,
nickname: item.nickname
nickname: item.nickname,
cache: item.cache,
cacheEnabled: item.cacheenabled
}
}

Expand Down Expand Up @@ -731,6 +756,9 @@ export async function markGroupConversationAsRead(group) {

});

Globals.unreadMessages = await getUnreadMessages();
Globals.updateGroupsFunction();


}

Expand Down Expand Up @@ -1171,7 +1199,7 @@ LIMIT ${limit}`
let count_raw = 0;

if (count && count.rows && count.rows.length) {
console.log(count, rand);

const res = [];

for (let i = 0; i < count.rows.length; i++) {
Expand Down
1 change: 1 addition & 0 deletions src/Globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class globals {
node: Config.defaultDaemon.getConnectionString(),
language: 'en',
cache: Config.defaultCache,
cacheEnabled: 'true',
nickname: 'Anonymous'
};

Expand Down
20 changes: 16 additions & 4 deletions src/HuginUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ if (recommended_node == undefined) {

export async function getBestCache() {

console.log('Getting best cache..')

let recommended_cache = undefined;

await Globals.updateCacheList();
Expand All @@ -116,12 +118,18 @@ export async function getBestCache() {
for (cache in caches) {
let this_cache = caches[cache];
let cacheURL = `${this_cache.url}/api/v1/info`;
console.log('Trying ', this_cache)
try {
const resp = await fetch(cacheURL, {
method: 'GET'
}, 1000);
if (resp.ok) {
recommended_cache = this_cache;
}, 3000);
if (!resp.ok) {continue}
recommended_cache = this_cache;
const json = await resp.json();
console.log(json);
if (json.status == "online") {

console.log(this_cache);
return(this_cache);
}
} catch (e) {
Expand Down Expand Up @@ -376,7 +384,7 @@ export async function optimizeMessages(nbrOfTxs, force=false) {

}

if (optimizing === true) return
if (optimizing === true) return "";

const [walletHeight, localHeight, networkHeight] = Globals.wallet.getSyncStatus();

Expand Down Expand Up @@ -430,13 +438,17 @@ export async function optimizeMessages(nbrOfTxs, force=false) {
export async function sendMessageWithHuginAPI(payload_hex) {

let cacheURL = Globals.preferences.cache ? Globals.preferences.cache : Config.defaultCache;

console.log('Sending messag with', cacheURL);

const response = await fetch(`${cacheURL}/api/v1/posts`, {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({payload: payload_hex}),
});
console.log(response.json());
return response.json();

}
Expand Down
Loading

0 comments on commit d3d72ce

Please sign in to comment.