Skip to content

Commit

Permalink
feat: cache articles and positions in page
Browse files Browse the repository at this point in the history
  • Loading branch information
Crissium committed Nov 9, 2023
1 parent d1160a4 commit 927a047
Show file tree
Hide file tree
Showing 13 changed files with 365 additions and 266 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ android {
applicationId "com.gmail.blandilyte.silverdict"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 5
versionName "1.0.7"
versionCode 6
versionName "1.1.0"
}
signingConfigs {
debug {
Expand Down
4 changes: 2 additions & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gmail.blandilyte.silverdict"
android:versionCode="5"
android:versionName="1.0.7">
android:versionCode="6"
android:versionName="1.1.0">

<uses-permission android:name="android.permission.INTERNET" />

Expand Down
4 changes: 2 additions & 2 deletions android/app/src/main/play/release-notes/en-US/default.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Fix:
Retain the old search term when a suggestion is selected. This is the behaviour of GoldenDict.
New Feature:
Caching the search results in memory.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SilverDict",
"version": "1.0.7",
"version": "1.1.0",
"private": true,
"scripts": {
"android": "react-native run-android",
Expand Down
167 changes: 11 additions & 156 deletions src/components/QueryScreen.js
Original file line number Diff line number Diff line change
@@ -1,164 +1,19 @@
import React, { useState, useEffect, useRef } from 'react';
import React from 'react';
import { View } from 'react-native';
import { loadDataFromJsonResponse } from '../utils';
import { QueryProvider } from './QueryScreen/QueryContext';
import QueryBar from './QueryScreen/QueryBar';
import QueryContent from './QueryScreen/QueryContent';
import { useAppContext } from '../AppContext';
import { localisedStrings } from '../translations/l10n';

export default function QueryScreen({ navigation }) {
const { serverAddress, dictionaries, groups, groupings, history, setHistory, sizeSuggestion } = useAppContext();
const apiPrefix = `${serverAddress}/api`;
const [query, setQuery] = useState('');
const textInputRef = useRef(null);

const [localHistory, setLocalHistory] = useState([]);
const [locationInLocalHistory, setLocationInLocalHistory] = useState(0);

const [latestSuggestionsTimestamp, setLatestSuggestionsTimestamp] = useState(0);
const [suggestions, setSuggestions] = useState([]);

const [nameActiveGroup, setNameActiveGroup] = useState('Default Group');

const [article, setArticle] = useState('');
const [namesActiveDictionaries, setNamesActiveDictionaries] = useState([]);
const [nameDictionaryToJumpTo, setNameDictionaryToJumpTo] = useState('');

useEffect(function() {
setNameActiveGroup('Default Group');
}, [groups.length]);

useEffect(function () {
if (query.length === 0) {
setLatestSuggestionsTimestamp(Date.now());
setSuggestions(['']);
resetNamesActiveDictionaries();
} else {
fetch(`${apiPrefix}/suggestions/${nameActiveGroup}/${encodeURIComponent(query)}?timestamp=${Date.now()}`)
.then(loadDataFromJsonResponse)
.then((data) => {
if (data['timestamp'] > latestSuggestionsTimestamp) {
setLatestSuggestionsTimestamp(data['timestamp']);
// Filter out empty suggestions
const newSuggestions = data['suggestions'].filter((suggestion) => suggestion.length > 0);
if (newSuggestions.length > 0) {
setSuggestions(newSuggestions);
} else {
setSuggestions(['']);
}
}
})
.catch((error) => {
alert(localisedStrings['query-screen-failure-fetch-suggestions']);
});
}
}, [dictionaries, groupings, nameActiveGroup, query, sizeSuggestion]);

useEffect(function() {
search(query);
}, [nameActiveGroup]);

function search(newQuery) {
if (newQuery.length === 0) {
return;
}

try {
newQuery = decodeURIComponent(newQuery);
// setQuery(newQuery);
newQuery = encodeURIComponent(newQuery);
}
catch (error) {
// setQuery(newQuery);
newQuery = encodeURIComponent(newQuery);
}

fetch(`${apiPrefix}/query/${nameActiveGroup}/${newQuery}?dicts=True`)
.then(loadDataFromJsonResponse)
.then((data) => {
setArticle(data['articles']);
setNamesActiveDictionaries(data['dictionaries']);

fetch(`${apiPrefix}/management/history`)
.then(loadDataFromJsonResponse)
.then((data) => {
setHistory(data);
});
})
.catch((error) => {
resetNamesActiveDictionaries();
alert(localisedStrings['query-screen-failure-fetch-articles']);
});
}

function setQueryAndFocusOnInput(newQuery) {
setQuery(newQuery);
textInputRef.current.focus();
}

function resetNamesActiveDictionaries() {
if (groupings[nameActiveGroup]) {
const dictionariesInGroup = [];
for (let dictionary of dictionaries) {
if (groupings[nameActiveGroup].includes(dictionary.name)) {
dictionariesInGroup.push(dictionary.name);
}
}
setNamesActiveDictionaries(dictionariesInGroup);
}
}

function searchBranching(newQuery) {
search(newQuery);
// If the current location is not the end of the local history, pop to the current location
setLocationInLocalHistory(localHistory.length);
setLocalHistory([...localHistory.slice(0, locationInLocalHistory + 1), newQuery]);
}

function searchInLocalHistory(direction) {
search(localHistory[locationInLocalHistory + direction]);
setLocationInLocalHistory(locationInLocalHistory + direction);
}

function handleInputSubmit(e) {
const firstSuggetion = suggestions[0];
if (firstSuggetion && firstSuggetion.length > 0) {
searchBranching(firstSuggetion);
} else {
searchBranching(query);
}
}

return (
<View style={{ flex: 1 }}>
<QueryBar
openDrawer={navigation.openDrawer}
query={query}
setQuery={setQuery}
textInputRef={textInputRef}
handleInputSubmit={handleInputSubmit}
setArticle={setArticle}
dictionaries={dictionaries}
groups={groups}
groupings={groupings}
nameActiveGroup={nameActiveGroup}
setNameActiveGroup={setNameActiveGroup}
namesActiveDictionaries={namesActiveDictionaries}
setNameDictionaryToJumpTo={setNameDictionaryToJumpTo}
/>
<QueryContent
serverAddress={serverAddress}
queryIsEmpty={query.length === 0}
history={history}
suggestions={suggestions}
search={searchBranching}
article={article}
nameDictionaryToJumpTo={nameDictionaryToJumpTo}
setQuery={setQueryAndFocusOnInput}
searchInLocalHistory={searchInLocalHistory}
ableToGoBack={locationInLocalHistory > 0}
ableToGoForward={locationInLocalHistory < localHistory.length - 1}
/>
</View>
<QueryProvider>
<View style={{ flex: 1 }}>
<QueryBar
openDrawer={navigation.openDrawer}
/>
<QueryContent />
</View>
</QueryProvider>
);
}
}
12 changes: 7 additions & 5 deletions src/components/QueryScreen/ArticleBottomBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import React, { useState } from 'react';
import { View } from 'react-native';
import { Appbar, TextInput, useTheme } from 'react-native-paper';
import { TEXT_ZOOM_MAX, TEXT_ZOOM_MIN } from '../../config';
import { useQueryContext } from './QueryContext';
import { localisedStrings } from '../../translations/l10n';

const TEXT_ZOOM_STEP = 10;

export default function ArticleBottomBar(props) {
export default function ArticleBottomBar() {
const onSurfaceColour = useTheme().colors.onSurface;
const { searchInLocalHistory, ableToGoBack, ableToGoForward, textZoom, setTextZoom, findInPageRef } = props;
const { ableToGoBackInHistory, ableToGoForwardInHistory, searchInLocalHistory, textZoom, setTextZoom, findInPageRef } = useQueryContext();

const [findBarActive, setFindBarActive] = useState(false);
const [wordToFind, setWordToFind] = useState('');

Expand Down Expand Up @@ -69,7 +71,7 @@ export default function ArticleBottomBar(props) {
<Appbar>
<View style={{ flex: 1, flexDirection: 'row', justifyContent: 'space-between' }}>
{
ableToGoBack ?
ableToGoBackInHistory ?
<Appbar.Action
icon='arrow-left'
color={onSurfaceColour}
Expand All @@ -79,7 +81,7 @@ export default function ArticleBottomBar(props) {
color={onSurfaceColour} />
}
{
ableToGoForward ?
ableToGoForwardInHistory ?
<Appbar.Action
icon='arrow-right'
color={onSurfaceColour}
Expand All @@ -103,4 +105,4 @@ export default function ArticleBottomBar(props) {
</View>
</Appbar>
);
}
}
Loading

0 comments on commit 927a047

Please sign in to comment.