Skip to content

Commit

Permalink
fix: 修复bug和体验优化(暂存)
Browse files Browse the repository at this point in the history
  • Loading branch information
simply-none committed Jan 13, 2024
1 parent ccb5435 commit 20521a3
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 132 deletions.
119 changes: 40 additions & 79 deletions src/hooks/useVoca.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import {

import { useBookStore } from "../stores/books";
import useTableStore from "../stores/table";
import useDBStore from '../stores/db'
import { storeToRefs } from "pinia";

import moment from "moment";

import { clearNotify, setNotify } from "../utils/element-plus";
import { funDownloadByJson } from "../utils/generateFile";
import { delBFromA, filterBFromA } from "../utils/common";

function isRequiredField(obj) {
obj = unref(obj);
Expand All @@ -37,6 +39,7 @@ function getTodayDate() {

export function useVoca() {
let useBook = useBookStore();
let useDB = useDBStore()
let { getDBTable, getDBTableData } = useTableStore();

// 今日学习数据
Expand Down Expand Up @@ -64,6 +67,7 @@ export function useVoca() {
let isMorethanTodayPlan = ref(false)

let { basicData, basicDataOrigin } = storeToRefs(useBook);
let { dbChanged, ad } = storeToRefs(useDB)

const drawer = ref(false);

Expand All @@ -73,21 +77,36 @@ export function useVoca() {

let isWordNotInDict = ref(false)

console.log(basicData, dbChanged.value, 'basicData')

// watchEffect(() => {
// if (JSON.stringify(basicData.value) === JSON.stringify(basicDataOrigin.value)) {
// console.log('初始化')
// initDataInFirstLoad()
// }
// })
initDataInFirstLoad();
// initDataInFirstLoad();
watch(
() => basicData.value.name, () => {
console.log('hhhhh')
initDataInFirstLoad();
}
);

watch(
[() => basicData.value.currentBook, () => basicData.value.currentRange, () => basicData.value.studyMode, () => basicData.value.studyCount],
() => {
console.log('hhhhh')
([a, b, c, d], [x, y, z, o]) => {
console.log('hhhhh', [a, b, c, d], [x, y, z, o])
initDataInFirstLoad();
}
);
watch(() => dbChanged.value, (n, o) => {
console.log(n, o, 'n o')
})
watch(() => dbChanged.value, (n, o) => {
console.log(n, o, '很疯狂的是')
initDataInFirstLoad()
})

watchEffect(async () => {
// 非首次加载,就不进行下列操作
Expand Down Expand Up @@ -134,7 +153,7 @@ export function useVoca() {

// 今日数据表
getDataFromDB(getDBTable, ["today-studied-voca", "++id, n"], todayStudyWordsTable).then(d => {
console.log('今日数据表' , d)
console.log('今日数据表', d)
todayStudyWordsTable.value = d
todayStudyWordsTable.value.orderBy('n').keys().then(dd => {
todayStudyWords.value = (dd || []).filter(w => w).map(w => (w || '').toLowerCase())
Expand All @@ -154,7 +173,7 @@ export function useVoca() {
table.value.orderBy('n').keys().then(dd => {
dictWords.value = (dd || []).filter(w => w).map(w => (w || '').toLowerCase())
dictWords.value.sort((a, b) => a > b ? 1 : -1)
console.log(dictWords.value, '总表')
console.log(dictWords.value.length, '总表')

})
})
Expand All @@ -166,7 +185,7 @@ export function useVoca() {
rangeTable.value.orderBy('n').keys().then(dd => {
rangeWords.value = (dd || []).filter(w => w).map(w => (w || '').toLowerCase())
rangeWords.value.sort((a, b) => a > b ? 1 : -1)
console.log(rangeWords.value, '范围表')
console.log(rangeWords.value.length, '范围表')

})
// getDataFromDB(getDBTableData, [rangeTable, ["n"]], rangeWords, 'setValue')
Expand Down Expand Up @@ -209,7 +228,7 @@ export function useVoca() {

function moreThanPlanFn() {
let moreThanPlan = todayStudyWords.value.length >= basicData.value.studyCount
console.log(todayStudyWords.value.length , basicData.value.studyCount, '比较,切换count')
console.log(todayStudyWords.value.length, basicData.value.studyCount, '比较,切换count')

if (basicData.value.studyMode === 'study' && moreThanPlan) {
if (!isWordNotInDict.value) {
Expand All @@ -235,61 +254,12 @@ export function useVoca() {
return moreThanPlan
}

function delAFromB (nums1, nums2) {
nums1.sort((x, y) => x - y);
nums2.sort((x, y) => x - y);
const length1 = nums1.length, length2 = nums2.length;
let index1 = 0, index2 = 0;
const intersection = [];
while (index1 < length1 && index2 < length2) {
const num1 = nums1[index1], num2 = nums2[index2];
if (num1 === num2) {
// 保证加入元素的唯一性
if (!intersection.length || num1 !== intersection[intersection.length - 1]) {
intersection.push(num1);
}
index1++;
index2++;
} else if (num1 < num2) {
index1++;
} else {
index2++;
}
}
return intersection;
}

function filterAFromB (nums1, nums2) {
nums1.sort((x, y) => x - y);
nums2.sort((x, y) => x - y);
const length1 = nums1.length, length2 = nums2.length;
let index1 = 0, index2 = 0;
const intersection = [];
while (index1 < length1 && index2 < length2) {
const num1 = nums1[index1], num2 = nums2[index2];
if (num1 === num2) {
// 保证加入元素的唯一性
if (!intersection.length || num1 !== intersection[intersection.length - 1]) {
intersection.push(num1);
}
index1++;
index2++;
} else if (num1 < num2) {
index1++;
} else {
index2++;
}
}
return intersection;
}


// 获取能够展示单词卡片的索引
async function getCouldStudyWords() {
let studyWordsData = [];
let moreThanPlan = moreThanPlanFn()
console.log(basicData.value.studyCount, todayStudyWords.value.length, '总,今')


// 查看是否是复习过去的单词模式
if (basicData.value.studyMode === "review-past") {
Expand All @@ -306,28 +276,19 @@ export function useVoca() {
// 仅学习模式
if (basicData.value.studyMode === "study" && !moreThanPlan) {
console.time('xuex1')
if (dictWords.value.length > rangeWords.value.length) {
studyWordsData = filterAFromB(dictWords.value, rangeWords.value)
// studyWordsData = rangeWords.value.filter(n => dictWords.value.includes(n))
} else {
// studyWordsData = dictWords.value.filter(n => rangeWords.value.includes(n))
studyWordsData = filterAFromB(rangeWords.value, dictWords.value)
}
console.log(studyWordsData, '能够学习的单词n')
studyWordsData = filterBFromA(rangeWords.value, dictWords.value)
console.log(studyWordsData.length, '能够学习的单词n')
console.timeEnd('xuex1')
console.time('xuex2')
studyWordsData = studyWordsData.filter(n => !studyWords.value.includes(n))
console.log(studyWordsData, '能够学习的单词n')

console.timeEnd('xuex2')
console.time('xuex3')

studyWordsData = studyWordsData.filter(
(word) => !todayStudyWords.value.includes(word)
);
console.log('仅学习模式')
console.timeEnd('xuex3')

console.time('xuex22')
studyWordsData = delBFromA(studyWordsData, studyWords.value)
console.log(studyWordsData.length, '能够学习的单词n22')
console.timeEnd('xuex22')

console.time('xuex33')
studyWordsData = delBFromA(studyWordsData, todayStudyWords.value)
console.log('仅学习模式33', studyWordsData.length)
console.timeEnd('xuex33')
}
console.log(studyWordsData.length, '能够学习的单词')
// 获取过滤后的可学习/复习的单词索引
Expand All @@ -344,7 +305,7 @@ export function useVoca() {
});
if (!vocabularycard) {
// 当前单词在词典中未找到,将过滤掉所有找不到的单词,重新获取下一个
setNotify('由于单词【' + couldStudyWordNameList.value[random]+ '】在词典中找不到,将跳转到下一个单词...', 'warning')
setNotify('由于单词【' + couldStudyWordNameList.value[random] + '】在词典中找不到,将跳转到下一个单词...', 'warning')
let d = await table.value.toArray()
d = d.map(dn => dn.n)
couldStudyWordNameList.value = couldStudyWordNameList.value.filter(c => {
Expand Down Expand Up @@ -423,7 +384,7 @@ export function useVoca() {

async function getDataTest(isForward = true) {
putStudiedVocabulary(toRaw(bookItem.value));

showVocabularyCard(isForward).then(d => bookItem.value = d);
fullscreenLoading.value = false;
}
Expand Down
72 changes: 55 additions & 17 deletions src/stores/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,31 @@ import { ref, computed, watch, reactive, toRaw, toRefs } from "vue";
import { defineStore, storeToRefs } from "pinia";
import useDBStore from "./db";

function getBasicInfo () {
let basicInfoInStorage = localStorage.getItem('basic-info')
if (!basicInfoInStorage) {
basicInfoInStorage = {name: '基础数据'}
} else {
basicInfoInStorage = JSON.parse(basicInfoInStorage)
}
return basicInfoInStorage
}

function setBasicInfo (key, data) {
let basicInfo = getBasicInfo()
basicInfo = {
...basicInfo,
id: 1,
name: '基础数据',
[key]: toRaw(data)
}
localStorage.setItem('basic-info', JSON.stringify(basicInfo))
}

export const useBookStore = defineStore("book", () => {

let basicData = reactive({
name: '',
currentBook: "",
currentRange: "",
studyMode: "",
Expand All @@ -24,24 +46,38 @@ export const useBookStore = defineStore("book", () => {

let useDB = useDBStore();
let { schema, dbChanged } = storeToRefs(useDB);
let { getTable } = useDB;
let { getTable, isExistSchema } = useDB;

watch(
() => dbChanged.value,
async (newV, oldV) => {

console.log('改变')

let a = await isExistSchema()
console.log(a, 'a')

if (!a) {
basicData.name = '基础数据'
localStorage.setItem('basic-info', JSON.stringify({}))
console.log('books')
return false
}


let tableList = Object.keys(schema.value);
let basic = tableList.find((l) => l === "basic-info");
let basicTable = getTable(basic);
// let basicTable = getTable(basic);

if (!basicTable) {
return false;
}
// if (!basicTable) {
// return false;
// }

// let basicInfo = (await basicTable.get(1));

let basicInfo = (await basicTable.get(1));
let basicInfo = getBasicInfo()

console.log(basicInfo, 'info')

if (!basicInfo) {
basicData.name = '基础数据'
Expand All @@ -52,24 +88,26 @@ export const useBookStore = defineStore("book", () => {
Object.keys(basicInfo).forEach((field) => {
basicData[field] = toRaw(basicInfo[field]);
});

console.log(basicData, 'console.log')
},
{ deep: true, immediate: true }
);

async function updateBasicInfo(field, newData) {
basicData[field] = newData;

let tableList = Object.keys(schema.value);
let basic = tableList.find((l) => l === "basic-info");
let basicTable = getTable(basic);
let basicInfo = await basicTable.get(1);
basicTable.put({
...basicInfo,
id: 1,
name: "基础数据",
[field]: toRaw(newData),
});
// let tableList = Object.keys(schema.value);
// let basic = tableList.find((l) => l === "basic-info");
// let basicTable = getTable(basic);
// let basicInfo = await basicTable.get(1);
// basicTable.put({
// ...basicInfo,
// id: 1,
// name: "基础数据",
// [field]: toRaw(newData),
// });

setBasicInfo(field, newData)
}

return {
Expand Down
Loading

0 comments on commit 20521a3

Please sign in to comment.