Skip to content

Commit

Permalink
feat(settings): review cards in random order (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
akdasa committed Jun 4, 2023
1 parent 5bec330 commit 45e782a
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/app/decks/review/pages/ReviewDeckPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import { StackedFlipCardsDeck , useIndexedList, useLibraryCache } from '@/app/de
import { ReviewFlipCard, ReviewDeckEmpty, ReviewCardSwipeOverlay, GradeCardButtons } from '@/app/decks/review'
import { useSettingsStore } from '@/app/settings'
import { TutorialSteps, useTutorialStore } from '@/app/tutorial'
import { useApplication , BackgroundTasks } from '@/app/shared'
import { useApplication , BackgroundTasks, useArrayShuffler } from '@/app/shared'
/* -------------------------------------------------------------------------- */
Expand All @@ -85,6 +85,7 @@ const libraryCache = useLibraryCache(application.instance())
const indexedList = useIndexedList()
const settings = useSettingsStore()
const tutorial = useTutorialStore()
const shuffler = useArrayShuffler()
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -159,12 +160,21 @@ async function onOpened() {
reviewCards = await app.reviewDeck.dueToCards(app.timeMachine.now)
await libraryCache.load(reviewCards.map(x => x.verseId))
// Create cards order array and shuffle if needed
const orderArray = [...Array(reviewCards.length).keys()]
const order = settings.reviewCardsInRandomOrder
? shuffler.shuffle(orderArray)
: orderArray
// Create view cards
const result = []
for (const [index, card] of reviewCards.entries()) {
result.push({
id: card.id.value, index, flipped: false, verseId: card.verseId
id: card.id.value, index: order[index], flipped: false, verseId: card.verseId
})
}
// Return result
cardsToShow.value = result
}
Expand Down
1 change: 1 addition & 0 deletions src/app/library/pages/LibraryPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ async function onRefresherPullDown(
event: IonRefresherCustomEvent<RefresherEventDetail>
) {
await syncLibrary(true)
await onSearchQueryChanged(searchQuery.value)
event.target.complete()
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/settings/pages/SettingsPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
</ion-toggle>
</ion-item>

<ion-item>
<ion-toggle
v-model="settings.reviewCardsInRandomOrder"
>
{{ $t('settings.reviewCardsInRandomOrder') }}
</ion-toggle>
</ion-item>

<ion-item>
<ion-label
@click="onSendEmail"
Expand Down
5 changes: 3 additions & 2 deletions src/app/settings/stores/useSettingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const settingsKeys = [
'language', 'showGradeButtons', 'colorfulCards',
'welcomeDone', 'authToken', 'authSessionId', 'authStrategy',
'authRefreshedAt', 'authExpiresAt', 'syncCollectionId', 'syncAt',
'syncLibraryAt'
'syncLibraryAt', 'reviewCardsInRandomOrder'
]


Expand All @@ -18,6 +18,7 @@ export const useSettingsStore = defineStore('settings', () => {
const language = ref('') // language will be set in initLocale at app initialization
const showGradeButtons = ref(true)
const colorfulCards = ref(true)
const reviewCardsInRandomOrder = ref(true)

// welcome
const welcomeDone = ref(false)
Expand Down Expand Up @@ -45,6 +46,6 @@ export const useSettingsStore = defineStore('settings', () => {
return {
language, showGradeButtons, colorfulCards, syncLibraryAt, welcomeDone,
authToken, authSessionId, authStrategy, authRefreshedAt, authExpiresAt, syncCollectionId, syncAt,
showAccountControls, autoSyncOnLogin,
showAccountControls, autoSyncOnLogin, reviewCardsInRandomOrder
}
})
22 changes: 22 additions & 0 deletions src/app/shared/composables/useArrayShuffler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

export function useArrayShuffler() {

function shuffle<T>(array: Array<T>) {
let currentIndex = array.length, randomIndex

// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex--;

// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]]
}

return array
}

return { shuffle }
}
1 change: 1 addition & 0 deletions src/app/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './composables/useEmitter'
export * from './composables/useTestId'
export * from './composables/useClearCache'
export * from './composables/useAppVersion'
export * from './composables/useArrayShuffler'

// tasks:
export * from './tasks/runSyncTask'
Expand Down
3 changes: 1 addition & 2 deletions src/app/welcome/pages/WelcomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ async function onOpened() {
await syncLibraryTask.sync()
await loadLibrary.sync()
settingsStore.syncLibraryAt = new Date().getTime()
settingsStore.welcomeDone = true
}
async function onSignIn(strategy: string) {
try {
await auth.authenticate(strategy)
emitter.emit('signedIn')
settingsStore.welcomeDone = true
router.replace(go('library'))
} catch (e) {
const alert = await alertController.create({
Expand All @@ -143,7 +143,6 @@ async function onEmailSignIn() {
}
function onSigInAsGuest() {
settingsStore.welcomeDone = true
router.replace(go('library'))
}
</script>
Expand Down
6 changes: 6 additions & 0 deletions src/init/app/initParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function initParams(
const tutorialEnabled = params.get('tutorialEnabled')
const libraryLastSyncDate = params.get('libraryLastSyncDate')
const autoSyncOnLogin = params.get('autoSyncOnLogin')
const reviewCardsInRandomOrder = params.get('reviewCardsInRandomOrder')
const date = params.get('date')

const tutorialStore = useTutorialStore()
Expand All @@ -32,6 +33,11 @@ export async function initParams(
settingsStore.autoSyncOnLogin = ['true', '1'].includes(autoSyncOnLogin)
}

if (reviewCardsInRandomOrder) {
console.debug('[params] reviewCardsInRandomOrder', reviewCardsInRandomOrder)
settingsStore.reviewCardsInRandomOrder = ['true', '1'].includes(reviewCardsInRandomOrder)
}


if (date) {
console.debug('[params] date', date)
Expand Down
3 changes: 2 additions & 1 deletion src/locale/en/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"colorfulCards": "Colorful cards",
"contactUs": "Contact us",
"update": "Update",
"clearCache": "Clear cache"
"clearCache": "Clear cache",
"reviewCardsInRandomOrder": "Randomize review cards"
},
"account": {
"welcomeBack": "Welcome back!",
Expand Down
3 changes: 2 additions & 1 deletion src/locale/ru/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"colorfulCards": "Цветные карточки",
"contactUs": "Напишите нам",
"update": "Обновить",
"clearCache": "Очистить кэш"
"clearCache": "Очистить кэш",
"reviewCardsInRandomOrder": "Обзор в случайном порядке"
},
"account": {
"welcomeBack": "Добро пожаловать!",
Expand Down
3 changes: 2 additions & 1 deletion src/locale/uk/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"gradeButtons": "Показувати кнопки оцінок",
"colorfulCards": "Кольорові картки",
"contactUs": "Зв'язатися з нами",
"update": "Update"
"update": "Update",
"reviewCardsInRandomOrder": "Випадковий порядок"
},
"account": {
"welcomeBack": "З Поверненням!",
Expand Down

0 comments on commit 45e782a

Please sign in to comment.