Skip to content

Commit

Permalink
feat: add mastered button on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Aug 10, 2024
1 parent 030e934 commit eb45842
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 37 deletions.
27 changes: 27 additions & 0 deletions apps/client/components/main/QuestionInput/QuestionInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,27 @@
播放声音
</button>
</div>
<button
class="btn btn-outline btn-sm"
@click="handleMastered"
>
掌握
</button>
</div>
</div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref, watch } from "vue";
import Message from "~/components/main/Message/useMessage";
import { courseTimer } from "~/composables/courses/courseTimer";
import { useAnswerTip } from "~/composables/main/answerTip";
import { useCurrentStatementEnglishSound } from "~/composables/main/englishSound";
import { isWord } from "~/composables/main/question";
import { useMastered } from "~/composables/main/useMastered";
import { useShowWordsWidth } from "~/composables/user/words";
import { isAuthenticated } from "~/services/auth";
import { useCourseStore } from "~/store/course";
import { isWindows } from "~/utils/platform";
import { getWordWidth, useQuestionInput } from "./questionInputHelper";
Expand All @@ -89,6 +98,7 @@ const {
const { isShowWordsWidth } = useShowWordsWidth();
const { toggleAnswerTip, isAnswerTip } = useAnswerTip();
const { resetCloseTip } = useAnswerError();
const { handleMastered } = useMasteredShortcut();
initializeQuestionInput();
focusInputWhenWIndowFocus();
Expand Down Expand Up @@ -127,6 +137,23 @@ function focusInputWhenWIndowFocus() {
});
}
function useMasteredShortcut() {
const { markStatementAsMastered } = useMastered();
function handleMastered() {
if (!isAuthenticated()) {
Message.warning("需要登录哦");
return;
}
markStatementAsMastered();
}
return {
handleMastered,
};
}
const { playSound } = useCurrentStatementEnglishSound();
function handlePlaySound(e: MouseEvent) {
e.preventDefault();
Expand Down
44 changes: 7 additions & 37 deletions apps/client/components/main/Tips.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,20 @@ import { useAnswerTip } from "~/composables/main/answerTip";
import { useCurrentStatementEnglishSound } from "~/composables/main/englishSound";
import { useGameMode } from "~/composables/main/game";
import { useSummary } from "~/composables/main/summary";
import { useMastered } from "~/composables/main/useMastered";
import { useShortcutKeyMode } from "~/composables/user/shortcutKey";
import { isAuthenticated } from "~/services/auth";
import { useCourseStore } from "~/store/course";
import { useMasteredElementsStore } from "~/store/masteredElements";
import { cancelShortcut, parseShortcutKeys, registerShortcut } from "~/utils/keyboardShortcuts";
import { useAnswer } from "./QuestionInput/useAnswer";
import { useWrapperQuestionInput } from "./QuestionInput/useWrapperQuestionInput";
const { toggleAnswerTip, isAnswerTip } = useAnswerTip();
const { shortcutKeys } = useShortcutKeyMode();
const { playSound } = usePlaySound(shortcutKeys.value.sound);
const { handleMastered } = useMastered();
const { goToNextQuestion } = useAnswer();
const { showQuestion, isQuestion } = useGameMode();
const { submitAnswer } = useWrapperQuestionInput();
const { handleMastered } = useMasteredShortcut();
useShowAnswer(shortcutKeys.value.answer);
const keybindings = computed(() => {
Expand Down Expand Up @@ -90,7 +89,7 @@ const keybindings = computed(() => {
},
{
keys: shortcutKeys.value.mastered,
text: "掌握啦",
text: "掌握",
eventFn: handleMastered,
},
];
Expand All @@ -106,45 +105,16 @@ const keybindings = computed(() => {
return resultItems;
});
function useMastered() {
const { shortcutKeys } = useShortcutKeyMode();
const courseStore = useCourseStore();
const masteredElements = useMasteredElementsStore();
const { showQuestion } = useGameMode();
const { showSummary } = useSummary();
function useMasteredShortcut() {
const { markStatementAsMastered } = useMastered();
const addLoading = ref(false);
async function handleMastered() {
function handleMastered() {
if (!isAuthenticated()) {
Message.warning("需要登录哦");
return;
}
// updateMarketedStatements 会影响 isLastStatement 返回的结果
// 所以我们提前调用 isLastStatement 来记录好值
if (addLoading.value) return;
const isLastStatement = courseStore.isLastStatement();
addLoading.value = true;
await masteredElements.addElement({
english: courseStore.currentStatement?.english!,
});
addLoading.value = false;
courseStore.updateMarketedStatements();
if (isLastStatement) {
showSummary();
} else {
// 看看消完之后 是否全部都没有了
// 这个是在 updatemarketedStatements 之后
// 处理的 case 比如只剩下2个 good ,那么消除一个 good 之后 那么列表就应该为0了
if (courseStore.isAllMastered()) {
showSummary();
return;
}
courseStore.toNextStatement();
showQuestion();
}
markStatementAsMastered();
}
onMounted(() => {
Expand Down
46 changes: 46 additions & 0 deletions apps/client/composables/main/useMastered.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ref } from "vue";

import { useGameMode } from "~/composables/main/game";
import { useSummary } from "~/composables/main/summary";
import { useCourseStore } from "~/store/course";
import { useMasteredElementsStore } from "~/store/masteredElements";

export function useMastered() {
const courseStore = useCourseStore();
const masteredElements = useMasteredElementsStore();
const { showQuestion } = useGameMode();
const { showSummary } = useSummary();

const addLoading = ref(false);
async function markStatementAsMastered() {
// updateMarketedStatements 会影响 isLastStatement 返回的结果
// 所以我们提前调用 isLastStatement 来记录好值
if (addLoading.value) return;
const isLastStatement = courseStore.isLastStatement();
addLoading.value = true;
await masteredElements.addElement({
english: courseStore.currentStatement?.english!,
});
addLoading.value = false;

courseStore.updateMarketedStatements();

if (isLastStatement) {
showSummary();
} else {
// 看看消完之后 是否全部都没有了
// 这个是在 updatemarketedStatements 之后
// 处理的 case 比如只剩下2个 good ,那么消除一个 good 之后 那么列表就应该为0了
if (courseStore.isAllMastered()) {
showSummary();
return;
}
courseStore.toNextStatement();
showQuestion();
}
}

return {
markStatementAsMastered,
};
}

0 comments on commit eb45842

Please sign in to comment.