Skip to content

Commit

Permalink
ship protein feature
Browse files Browse the repository at this point in the history
  • Loading branch information
SarcevicAntonio committed Mar 20, 2024
1 parent 4ef5a35 commit e86c1aa
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 48 deletions.
8 changes: 5 additions & 3 deletions src/lib/components/DayEditor/Bucket.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ItemDrawer from '$lib/views/ItemDrawer/ItemDrawer.svelte'
import { createEventDispatcher } from 'svelte'
import Expandable from '../Expandable.svelte'
import { calculateProteinFromItems } from '$lib/protein'
const dispatch = createEventDispatcher()
export let label: string
Expand Down Expand Up @@ -35,13 +36,14 @@
<span class="title-l">{label}</span>
<div class="bucket-info">
{#if items.length}
<span class="body-m">
{items.length} Item{items.length !== 1 ? 's' : ''}
</span>
<span class="label-l">
{kcalDisplay(calculateKcalFromItems(items))}
kcal
</span>
<span class="label-l">
{kcalDisplay(calculateProteinFromItems(items))}
protein
</span>
{:else}
<ItemDrawer
triggerTestId="track-item-{label}-{date}"
Expand Down
4 changes: 4 additions & 0 deletions src/lib/components/InstanceCreator/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Item, ItemInstance } from '$lib/data/items'
import { calculateKcalPer100FromItems } from '$lib/kcal'
import { calculateProteinPer100FromItems } from '$lib/protein'
import { writable } from 'svelte/store'
import { v4 as uuid } from 'uuid'

Expand All @@ -13,6 +14,9 @@ export function transformItemToInstance(item: Item): ItemInstance {
brand: item.brand || '',
kcalPer100:
item.kcalPer100 || calculateKcalPer100FromItems(item.items, item.amount),
proteinPer100:
item.proteinPer100 ||
calculateProteinPer100FromItems(item.items, item.amount),
amount: 100,
portions: item.portions || [],
}
Expand Down
8 changes: 5 additions & 3 deletions src/lib/components/ItemInstanceEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import IcDelete from '~icons/ic/round-delete-forever'
import InstanceForm from './InstanceForm.svelte'
import PortionSelector from './PortionSelector.svelte'
import { calculateProtein } from '$lib/protein'
const dispatch = createEventDispatcher()
export let item: ItemInstance
export let amountInputElement: HTMLInputElement = null
let editing = false
$: kcalLabel = kcalDisplay(calculateKcal(item))
function dispatchUpdate() {
dispatch('update')
}
Expand All @@ -41,7 +40,10 @@
</div>
<div class="col end">
<span class="label-l">
{kcalLabel} kcal
{kcalDisplay(calculateKcal(item))} kcal
</span>
<span class="label-l">
{kcalDisplay(calculateProtein(item))} protein
</span>
</div>
</button>
Expand Down
21 changes: 7 additions & 14 deletions src/lib/components/KcalLimitBar.svelte
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
<script>
import { userSettings } from '$lib/data/user'
<script lang="ts">
export let amount: number
export let limit = 9999
export let kcalInDay
$: overLimit = kcalInDay > ($userSettings?.kcalLimit || 9999)
$: overLimit = amount > limit
</script>

<div class="bar" class:over-limit={overLimit}>
<div
class="inner"
style={!isNaN(kcalInDay)
style={!isNaN(amount)
? overLimit
? `width: ${Math.min(
($userSettings?.kcalLimit / kcalInDay) * 100,
100
)}%;`
: `width: ${Math.min(
(kcalInDay / $userSettings?.kcalLimit) * 100,
100
)}%;`
? `width: ${Math.min((limit / amount) * 100, 100)}%;`
: `width: ${Math.min((amount / limit) * 100, 100)}%;`
: ''}
/>
</div>
Expand Down
7 changes: 3 additions & 4 deletions src/lib/components/WeekSwitcher.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script>
import { toISODateString } from '$lib/dateHelpers'
import { calculateKcalFromItems } from '$lib/kcal'
import { kcalDisplay } from '$lib/kcal'
import { curDay, curWeek, curYear, weekData } from '$lib/data/intake'
import { userSettings } from '$lib/data/user'
import { toISODateString } from '$lib/dateHelpers'
import { calculateKcalFromItems, kcalDisplay } from '$lib/kcal'
import { addDays, getYear } from 'date-fns'
import Switcher from './Switcher.svelte'
Expand All @@ -22,7 +21,7 @@
acc +
day.meals.reduce(
(acc, meal) => acc + calculateKcalFromItems(meal.intake),
0
0,
) || 0
)
}, 0)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/data/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export const defaultItem = {
label: '',
brand: '',
kcalPer100: 100, // 0 means no override
proteinPer100: 100, // 0 means no override
amount: 0, // 0 means no override
items: [],
portions: [],
Expand Down Expand Up @@ -183,6 +184,7 @@ export interface Item {
brand?: string
label: string
kcalPer100: number
proteinPer100?: number
amount?: number
items?: ItemInstance[]
portions?: Portion[]
Expand All @@ -197,6 +199,7 @@ export interface ItemInstance {
label: string
brand?: string
kcalPer100: number
proteinPer100?: number
amount: number
portions?: Portion[]
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/data/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ export interface User {

export interface UserSettings {
kcalLimit?: number
proteinLimit?: number
}
18 changes: 18 additions & 0 deletions src/lib/protein.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { ItemInstance } from './data/items'
import { calculateAmountSum } from './kcal'

export const calculateProtein = (item: ItemInstance) =>
(item.proteinPer100 / 100) * item.amount

export const calculateProteinFromItems = (items: ItemInstance[]) => {
return items.reduce((acc, item) => acc + calculateProtein(item), 0)
}

export const calculateProteinPer100FromItems = (
items: ItemInstance[],
amountSum = 0
) => {
if (!amountSum) amountSum = calculateAmountSum(items)
const protein = calculateProteinFromItems(items)
return (protein / amountSum) * 100
}
19 changes: 17 additions & 2 deletions src/lib/views/CurDayView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { userSettings } from '$lib/data/user'
import { toISODateString } from '$lib/dateHelpers'
import { calculateKcalFromItems, kcalDisplay } from '$lib/kcal'
import { calculateProteinFromItems } from '$lib/protein'
import { addDays } from 'date-fns'
let data = null as DayType
Expand All @@ -21,7 +22,12 @@
$: kcalInDay = data?.meals.reduce(
(acc, meal) => acc + calculateKcalFromItems(meal.intake),
0
0,
)
$: proteinInDay = data?.meals.reduce(
(acc, meal) => acc + calculateProteinFromItems(meal.intake),
0,
)
$: dateObj = new Date($curDay)
Expand Down Expand Up @@ -52,12 +58,21 @@
>
{kcalDisplay(kcalInDay)} kcal
</span>
<span
class="label-l"
class:over-limit={proteinInDay > ($userSettings?.proteinLimit || 9999)}
class:stale
>
{kcalDisplay(proteinInDay)} protein
</span>
</Switcher>

{#await dayData.load()}
<KcalLimitBarSkeleton />
<KcalLimitBarSkeleton />
{:then _}
<KcalLimitBar {kcalInDay} />
<KcalLimitBar amount={kcalInDay} limit={$userSettings?.kcalLimit} />
<KcalLimitBar amount={proteinInDay} limit={$userSettings?.proteinLimit} />
{/await}

<DayEditor bind:data date={$curDay} />
Expand Down
30 changes: 21 additions & 9 deletions src/lib/views/ItemDrawer/EditItem.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<script lang="ts">
import Input from '$lib/Input.svelte'
import BarCodeScanDialog from '$lib/components/BarCodeScanDialog.svelte'
import ItemInstance from '$lib/components/ItemInstanceEditor.svelte'
import TrackNow from '$lib/components/TrackNow.svelte'
import {
createItemStore,
defaultPortion,
deleteItem,
items,
type Item,
type ItemInstance as ItemInstanceType,
} from '$lib/data/items'
import {
addQuickSnack,
quickSnacks,
removeQuickSnack,
} from '$lib/data/quickSnacks'
import { toISODateString } from '$lib/dateHelpers'
import Input from '$lib/Input.svelte'
import {
calculateAmountSum,
calculateKcalPer100FromItems,
Expand All @@ -29,15 +33,11 @@
import IcAdd from '~icons/ic/round-plus'
import IcRoundShare from '~icons/ic/round-share'
import IcRoundToday from '~icons/ic/round-today'
import ItemDrawer from './ItemDrawer.svelte'
import PortionCreator from './PortionCreator.svelte'
import MaterialSymbolsOfflineBolt from '~icons/material-symbols/offline-bolt'
import MaterialSymbolsOfflineBoltOutline from '~icons/material-symbols/offline-bolt-outline'
import {
addQuickSnack,
quickSnacks,
removeQuickSnack,
} from '$lib/data/quickSnacks'
import ItemDrawer from './ItemDrawer.svelte'
import PortionCreator from './PortionCreator.svelte'
import { calculateProteinPer100FromItems } from '$lib/protein'
const dispatch = createEventDispatcher()
Expand Down Expand Up @@ -95,6 +95,9 @@
<Input type="calc" bind:value={$dataStore.kcalPer100}>
kcal per 100 g || ml
</Input>
<Input type="calc" bind:value={$dataStore.proteinPer100}>
protein per 100 g || ml
</Input>
{:else}
<Input
type="calc"
Expand All @@ -105,6 +108,15 @@
>
kcal per 100 g || ml
</Input>
<Input
type="calc"
disabled
value={kcalDisplay(
calculateProteinPer100FromItems($dataStore.items, $dataStore.amount),
)}
>
protein per 100 g || ml
</Input>
{/if}
<Input type="number" bind:value={$dataStore.ean}>
EAN (Barcode)
Expand Down
2 changes: 0 additions & 2 deletions src/lib/views/ItemDrawer/ItemDrawer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
export let isOpen = false
export let editItem: Item = null
export let triggerTestId = ''
let showQuickSnacks = false
</script>

<Dialog
let:toggle
bind:isOpen
on:dismiss={() => {
editItem = null
showQuickSnacks = false
}}
triggerProps={{
'class': `margin-left-auto ${selector ? 'btn text' : ''}`,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/views/UnifiedView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
{kcalDisplay(kcalInDay)} kcal
</span>
</div>
<KcalLimitBar {kcalInDay} />
<KcalLimitBar amount={kcalInDay} />
<DayEditor bind:data {date} />
</div>
{:else}
Expand Down
4 changes: 2 additions & 2 deletions src/routes/+error.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
<p>Maybe try reloading or logging in.</p>

<nav class="fab-bar">
<a data-sveltekit-prefetch href="/auth">
<a href="/auth">
<IconLogin /> Login
</a>
</nav>
{:else}
<p>Maybe try reloading or going home.</p>
<nav class="fab-bar">
<a data-sveltekit-prefetch href="/">
<a href="/">
<IcHome />
</a>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<IcRoundCloudOff color="var(--md-error)" />
{/if}
{#if $user}
<a data-sveltekit-prefetch class="btn text" href="/profile">
<a class="btn text" href="/profile">
{#if $user.photoURL && !imgError}
<img
src={$user.photoURL}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { page } from '$app/stores'
import { decodeUriComponentToObj } from '$lib/base64'
import InstanceCreator from '$lib/components/InstanceCreator/InstanceCreator.svelte'
import { curDay, dateIsToday, weekData } from '$lib/data/intake'
import { curDay, dateIsToday } from '$lib/data/intake'
import { saveExternalItem, type Item } from '$lib/data/items'
import { toISODateString } from '$lib/dateHelpers'
import CurDayView from '$lib/views/CurDayView.svelte'
Expand Down
2 changes: 1 addition & 1 deletion src/routes/Credits.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<hr />
<h3 class="headline-5">Made possible thanks to:</h3>
<ul>
{#each deps as dep, i}
{#each deps as dep}
{@const link = dep.link.replace('git://', 'https://').replace('git+', '')}
<li>
<a target="_blank" rel="noreferrer" href={link}>{dep.name}</a>
Expand Down
12 changes: 8 additions & 4 deletions src/routes/profile/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@
</ul>
<hr />
{#await userSettings.load()}
<Input type="calc" disabled>Kcal Day Limit / Warning</Input>
<Input type="calc" disabled>Kcal Goal</Input>
<Input type="calc" disabled>Protein Goal</Input>
{:then}
<Input type="calc" bind:value={$userSettings.kcalLimit}>
Kcal Day Limit / Warning
Kcal Goal
</Input>
<Input type="calc" bind:value={$userSettings.proteinLimit}>
Protein Goal
</Input>
{/await}
<a
href="https://www.tk.de/service/app/2004134/kalorienrechner/einstieg.app?tkcm=ab"
target="_blank"
rel="noreferrer"
>
Kalo­ri­en­be­darfs­rechner von TK
Kalorienbedarfsrechner von TK
</a>
</div>
<button class="btn tonal" on:click={logout}>
Expand All @@ -57,7 +61,7 @@

{#if $user}
<nav class="fab-bar">
<a data-sveltekit-prefetch href="/">
<a href="/">
<IcHome /> Go Home
</a>
</nav>
Expand Down

0 comments on commit e86c1aa

Please sign in to comment.