Накоплено
- 0 ₽ -Текущий баланс
+ 0 ₽ +Движение денег
История
Новая запись
diff --git a/static/app.js b/static/app.js index 2da58d0..cd091a7 100644 --- a/static/app.js +++ b/static/app.js @@ -12,6 +12,8 @@ const state = { kind: "income", board: "all", + currentView: "dashboardView", + previousView: "dashboardView", categoryFilter: "", summary: null, config: null, @@ -24,17 +26,19 @@ const $ = (selector) => document.querySelector(selector); const money = new Intl.NumberFormat("ru-RU", { style: "currency", currency: "RUB", maximumFractionDigits: 2 }); + function safeTelegram(action) { + try { action(); } catch (error) { console.warn("Telegram WebApp method failed", error); } + } + function initTelegram() { if (!tg) return; - const safely = (action) => { - try { action(); } catch (error) { console.warn("Telegram WebApp method failed", error); } - }; - safely(() => tg.ready()); - safely(() => tg.expand()); - safely(() => tg.enableClosingConfirmation?.()); - if (tg.isVersionAtLeast?.("6.1")) safely(() => tg.setBackgroundColor("#111111")); - if (tg.isVersionAtLeast?.("6.9")) safely(() => tg.setHeaderColor("#111111")); - if (tg.isVersionAtLeast?.("7.10")) safely(() => tg.setBottomBarColor("#111111")); + safeTelegram(() => tg.ready()); + safeTelegram(() => tg.expand()); + safeTelegram(() => tg.enableClosingConfirmation?.()); + if (tg.isVersionAtLeast?.("6.1")) safeTelegram(() => tg.setBackgroundColor("#111111")); + if (tg.isVersionAtLeast?.("6.9")) safeTelegram(() => tg.setHeaderColor("#111111")); + if (tg.isVersionAtLeast?.("7.7")) safeTelegram(() => tg.disableVerticalSwipes()); + if (tg.isVersionAtLeast?.("7.10")) safeTelegram(() => tg.setBottomBarColor("#111111")); } function showToast(message) { @@ -54,15 +58,59 @@ function bindNavigation() { document.querySelectorAll(".bottom-nav button").forEach((button) => { - button.addEventListener("click", () => { - document.querySelectorAll(".bottom-nav button, .view").forEach((item) => item.classList.remove("active")); - button.classList.add("active"); - $(`#${button.dataset.view}`).classList.add("active"); - if (button.dataset.view === "leaderboardView") loadLeaderboard(); - }); + button.addEventListener("click", () => showView(button.dataset.view)); }); } + function showView(viewId) { + document.querySelectorAll(".bottom-nav button, .view").forEach((item) => item.classList.remove("active")); + $(`#${viewId}`).classList.add("active"); + document.querySelector(`.bottom-nav button[data-view="${viewId}"]`)?.classList.add("active"); + state.currentView = viewId; + document.body.classList.toggle("form-open", viewId === "addView"); + updateNativeControls(); + window.scrollTo({ top: 0, behavior: "auto" }); + if (viewId === "leaderboardView") loadLeaderboard(); + } + + function openAddView() { + if (state.currentView !== "addView") state.previousView = state.currentView; + showView("addView"); + window.setTimeout(() => $("#amount").focus(), 0); + } + + function closeAddView() { + showView(state.previousView || "dashboardView"); + } + + function setupNativeControls() { + const isTelegramClient = Boolean(tg?.platform && tg.platform !== "unknown"); + if (!isTelegramClient || !tg?.MainButton) return; + safeTelegram(() => tg.MainButton.onClick(() => { + if (state.currentView === "addView") { + $("#transactionForm").requestSubmit(); + } else { + openAddView(); + } + })); + safeTelegram(() => tg.BackButton?.onClick(closeAddView)); + updateNativeControls(); + } + + function updateNativeControls() { + const isTelegramClient = Boolean(tg?.platform && tg.platform !== "unknown"); + if (!isTelegramClient || !tg?.MainButton) return; + const isForm = state.currentView === "addView"; + safeTelegram(() => tg.MainButton.setParams({ + text: isForm ? "Сохранить операцию" : "Добавить запись", + color: "#f0d28a", + text_color: "#17130b", + is_active: true, + is_visible: true, + })); + safeTelegram(() => isForm ? tg.BackButton?.show() : tg.BackButton?.hide()); + } + function fillCategories() { const select = $("#category"); const items = [...new Set([...categories[state.kind], ...state.customCategories[state.kind]])]; @@ -143,10 +191,12 @@ } if (!canSendViaTelegram()) return false; try { + safeTelegram(() => tg.MainButton?.showProgress()); tg.HapticFeedback?.notificationOccurred("success"); tg.sendData(encoded); return true; } catch (error) { + safeTelegram(() => tg.MainButton?.hideProgress()); console.error("Telegram sendData failed", error); showToast("Telegram не принял запись. Обновите приложение и попробуйте снова"); return false; @@ -190,9 +240,6 @@ $("#balanceValue").textContent = money.format(summary.balance_cents / 100); $("#incomeValue").textContent = money.format(summary.income_cents / 100); $("#expenseValue").textContent = money.format(summary.expense_cents / 100); - const percent = Number(summary.saved_percent); - $("#savedPercent").textContent = `${percent.toLocaleString("ru-RU", { maximumFractionDigits: 1 })}%`; - $("#savingProgress").style.width = `${Math.max(0, Math.min(100, percent))}%`; renderCategories(summary.categories || []); } @@ -369,6 +416,7 @@ initTelegram(); setIdentity(); bindNavigation(); + setupNativeControls(); bindForm(); bindLeaderboard(); fillCategories(); diff --git a/static/index.html b/static/index.html index a338741..949c570 100644 --- a/static/index.html +++ b/static/index.html @@ -6,9 +6,9 @@
Накоплено
- 0 ₽ -Текущий баланс
+ 0 ₽ +Движение денег
Новая запись