Skip to content

Commit

Permalink
✨ 子窗口操作同步主窗口,帖子排序
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Mar 23, 2024
1 parent 55671ab commit 9e030c9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/components/post/tb-collect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ToPostCollect v-model="showEdit" :post="props.data" @submit="refresh()" />
</template>
<script lang="ts" setup>
import { event } from "@tauri-apps/api";
import { onBeforeMount, ref, watch } from "vue";
import TSUserCollection from "../../plugins/Sqlite/modules/userCollect";
Expand All @@ -34,6 +35,7 @@ const props = defineProps<TbCollectProps>();
onBeforeMount(async () => await refresh());
async function refresh(): Promise<void> {
await event.emit("refreshCollect");
const check = await TSUserCollection.getPostCollect(props.modelValue.toString());
if (typeof check === "boolean") {
isCollected.value = check;
Expand All @@ -57,6 +59,7 @@ watch(
if (val === undefined) return;
if (isCollected.value === false) return;
const res = await TSUserCollection.updatePostInfo(props.modelValue.toString(), val);
await event.emit("refreshCollect");
if (!res) {
showSnackbar({
text: "更新帖子信息失败,数据库中不存在帖子信息!",
Expand All @@ -82,6 +85,7 @@ async function switchCollect(): Promise<void> {
return;
}
await TSUserCollection.addCollect(props.modelValue.toString(), props.data);
await event.emit("refreshCollect");
isCollected.value = true;
showSnackbar({
text: "收藏成功",
Expand All @@ -99,6 +103,7 @@ async function switchCollect(): Promise<void> {
}
}
await TSUserCollection.deletePostCollect(props.modelValue.toString(), true);
await event.emit("refreshCollect");
isCollected.value = false;
showSnackbar({
text: "取消收藏成功",
Expand Down
49 changes: 41 additions & 8 deletions src/pages/common/PostCollect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
<v-list-item v-bind="props" :title="item.raw.title" :subtitle="item.raw.desc" />
</template>
</v-select>
<v-btn
size="small"
class="pc-btn"
icon="mdi-sort"
@click="sortPost(!sortId)"
:title="sortId ? '按更新时间排序' : '按帖子ID排序'"
/>
<v-btn
:disabled="selectedMode"
size="small"
Expand Down Expand Up @@ -83,8 +90,10 @@
</div>
</template>
<script lang="ts" setup>
import { event } from "@tauri-apps/api";
import { UnlistenFn } from "@tauri-apps/api/helpers/event";
import { storeToRefs } from "pinia";
import { computed, onBeforeMount, onMounted, ref, watch } from "vue";
import { computed, onBeforeMount, onMounted, onUnmounted, ref, watch } from "vue";
import showConfirm from "../../components/func/confirm";
import showSnackbar from "../../components/func/snackbar";
Expand Down Expand Up @@ -115,6 +124,9 @@ const view = computed(() => {
const selectedMode = ref<boolean>(false);
const selectedPost = ref<Array<string>>([]);
const showOverlay = ref(false);
const sortId = ref<boolean>(false);
let collectListener: UnlistenFn | undefined = undefined;
onBeforeMount(async () => {
if (!(await TGSqlite.checkTableExist("UFPost"))) {
Expand All @@ -124,34 +136,55 @@ onBeforeMount(async () => {
color: "success",
});
}
collectListener = await event.listen("refreshCollect", async () => await load());
});
onMounted(async () => await load());
onUnmounted(() => {
if (collectListener) collectListener();
});
function updateSelected(v: string[]) {
selectedPost.value = v;
}
function sortPost(value: boolean) {
let ori = sortId.value;
sortId.value = value;
selected.value = selected.value.sort((a, b) => {
if (sortId.value) {
return Number(b.id) - Number(a.id);
} else {
return Number(b.updated) - Number(a.updated);
}
});
if (ori !== sortId.value) {
showSnackbar({
text: `已${sortId.value ? "按帖子ID排序" : "按更新时间排序"}`,
color: "success",
});
}
}
async function load(): Promise<void> {
loadingTitle.value = "获取收藏帖子...";
loading.value = true;
loadingTitle.value = "获取收藏合集...";
collections.value = await TSUserCollection.getCollectList();
loadingTitle.value = "获取未分类帖子...";
const postUnCollect = await TSUserCollection.getUnCollectPostList();
if (postUnCollect.length > 0) {
if (curSelect.value === "未分类" || collections.value.length === 0) {
selected.value = postUnCollect;
curSelect.value = "未分类";
} else if (collections.value.length > 0) {
selected.value = await TSUserCollection.getCollectPostList(collections.value[0].title);
curSelect.value = collections.value[0].title;
} else if (collections.value.find((c) => c.title === curSelect.value)) {
selected.value = await TSUserCollection.getCollectPostList(curSelect.value);
} else {
selected.value = [];
selected.value = postUnCollect;
curSelect.value = "未分类";
}
sortPost(sortId.value);
selectedMode.value = false;
selectedPost.value = [];
page.value = 1;
if (page.value > length.value) page.value = 1;
loading.value = false;
}
Expand Down

0 comments on commit 9e030c9

Please sign in to comment.