Skip to content

Commit

Permalink
Fix the way "higher is better" is set and allow changing this parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dmint789 committed Dec 30, 2022
1 parent 11dd5ae commit 94ed774
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 56 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ android {
applicationId "com.stat_tracker"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 2
versionName "0.4.0"
versionCode 3
versionName "0.4.1"
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

if (isNewArchitectureEnabled()) {
Expand Down
16 changes: 8 additions & 8 deletions android/app/src/main/assets/index.android.bundle

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion app/components/Entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import { useSelector } from 'react-redux';
import { RootState } from '../redux/store';
import GS from '../shared/GlobalStyles';
import { IEntry, IStatType, IMultiValueStat, IStat } from '../shared/DataStructure';
import { IEntry, IStatType, IStat } from '../shared/DataStructure';

import IconButton from './IconButton';

Expand All @@ -28,6 +28,11 @@ const Entry: React.FC<{
const getMultiStatTextElement = (stat: IStat, statType: IStatType, key: 'best' | 'avg' | 'sum') => {
const isPB = statType?.trackPBs && statType.pbs?.allTime.entryId[key] === entry.id;

if (key === 'best') {
// @ts-ignore
key = statType?.higherIsBetter ? 'high' : 'low';
}

return <Text style={isPB ? styles.pbStyle : {}}>{stat.multiValueStats[key]}</Text>;
};

Expand Down
5 changes: 3 additions & 2 deletions app/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ type Props = {
options: ISelectOption[];
selected: number; // corresponds to the value field of ISelectOption
onSelect: (value: number) => void;
horizontal?: boolean;
};

const Select: React.FC<Props> = ({ options, selected, onSelect }) => {
const Select: React.FC<Props> = ({ options, selected, onSelect, horizontal = false }) => {
return (
<View>
<View style={horizontal ? { flexDirection: 'row', justifyContent: 'space-between' } : {}}>
{options.map((option) => (
<TouchableOpacity
key={option.value}
Expand Down
9 changes: 6 additions & 3 deletions app/components/WorkingEntryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ const WorkingEntryList: React.FC<{
</Text>
{stat.values.length > 1 && (statType?.showBest || statType?.showAvg || statType?.showSum) && (
<Text style={{ ...GS.text, ...GS.grayText }}>
{statType?.showBest && `Best: ${stat.multiValueStats.best} `}
{statType?.showAvg && `Avg: ${stat.multiValueStats.avg} `}
{statType?.showSum && `Sum: ${stat.multiValueStats.sum}`}
{statType.showBest &&
`Best: ${
statType.higherIsBetter ? stat.multiValueStats.high : stat.multiValueStats.low
} `}
{statType.showAvg && `Avg: ${stat.multiValueStats.avg} `}
{statType.showSum && `Sum: ${stat.multiValueStats.sum}`}
</Text>
)}
</TouchableOpacity>
Expand Down
30 changes: 22 additions & 8 deletions app/redux/mainSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import * as SM from '../shared/StorageManager';
import { isNewerOrSameDate } from '../shared/GlobalFunctions';
import {
IMultiValueStat,
IMultiValuePB,
IStatCategory,
IEntry,
IStatType,
IStat,
StatTypeVariant,
} from '../shared/DataStructure';

const verbose = true;

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -58,23 +60,29 @@ const updateStatTypePB = (state: any, statType: IStatType, entry: IEntry): boole
avg: entry.id,
sum: entry.id,
},
result: { ...stat.multiValueStats },
result: {
best: statType.higherIsBetter ? stat.multiValueStats.high : stat.multiValueStats.low,
avg: stat.multiValueStats.avg,
sum: stat.multiValueStats.sum,
},
},
};

pbUpdated = true;
} else {
const pbs = statType.pbs.allTime.result as IMultiValueStat;
const pbs = statType.pbs.allTime.result as IMultiValuePB;
const convertKey = (key) => (key === 'best' ? (statType.higherIsBetter ? 'high' : 'low') : key);

['best', 'avg', 'sum'].forEach((key) => {
if (
pbs[key] === null ||
(stat.multiValueStats[key] > pbs[key] && statType.higherIsBetter) ||
(stat.multiValueStats[key] < pbs[key] && !statType.higherIsBetter) ||
(stat.multiValueStats[key] === pbs[key] && !isNewerOrSameDate(entry.date, state.entries[0].date))
(stat.multiValueStats[convertKey(key)] > pbs[key] && statType.higherIsBetter) ||
(stat.multiValueStats[convertKey(key)] < pbs[key] && !statType.higherIsBetter) ||
(stat.multiValueStats[convertKey(key)] === pbs[key] &&
!isNewerOrSameDate(entry.date, state.entries[0].date))
) {
statType.pbs.allTime.entryId[key] = entry.id;
statType.pbs.allTime.result[key] = stat.multiValueStats[key];
statType.pbs.allTime.result[key] = stat.multiValueStats[convertKey(key)];

pbUpdated = true;
}
Expand All @@ -87,6 +95,8 @@ const updateStatTypePB = (state: any, statType: IStatType, entry: IEntry): boole
};

const checkPBFromScratch = (state: any, statType: IStatType) => {
if (verbose) console.log(`Checking PB from scratch for stat type ${statType.name}`);

for (let i = state.entries.length - 1; i >= 0; i--) {
updateStatTypePB(state, statType, state.entries[i]);
}
Expand Down Expand Up @@ -294,9 +304,13 @@ const mainSlice = createSlice({
editStatType: (state, action: PayloadAction<IStatType>) => {
state.statTypes = state.statTypes.map((el) => {
if (el.id === action.payload.id) {
console.log('ST', JSON.stringify(action.payload), JSON.stringify(el));
if (el.trackPBs && !action.payload.trackPBs) {
delete action.payload.pbs;
} else if (!el.trackPBs && action.payload.trackPBs) {
} else if (
action.payload.trackPBs &&
(!el.trackPBs || action.payload.higherIsBetter !== el.higherIsBetter)
) {
checkPBFromScratch(state, action.payload);
}

Expand Down
3 changes: 2 additions & 1 deletion app/screens/AddEditEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ const AddEditEntry = ({ navigation, route }) => {

if (selectedStatType.multipleValues) {
mvs.sum = formatted.reduce((acc, val) => acc + val, 0);
mvs.best = selectedStatType.higherIsBetter ? Math.max(...formatted) : Math.min(...formatted);
mvs.low = Math.min(...formatted);
mvs.high = Math.max(...formatted);
mvs.avg = Math.round((mvs.sum / formatted.length + Number.EPSILON) * 100) / 100;
}
} else {
Expand Down
48 changes: 32 additions & 16 deletions app/screens/AddEditStatType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@ const AddEditStatType = ({ navigation, route }) => {
const [name, setName] = useState<string>('');
const [unit, setUnit] = useState<string>('');
const [variant, setVariant] = useState<StatTypeVariant>(StatTypeVariant.TEXT);
const [higherIsBetter, setHigherIsBetter] = useState<boolean>(true);
// const [choices, setChoices] = useState([]);
// const [formula, setFormula] = useState<string>('');
const [multipleValues, setMultipleValues] = useState<boolean>(false);
const [showBest, setShowBest] = useState<boolean>(true);
const [showAvg, setShowAvg] = useState<boolean>(false);
const [showSum, setShowSum] = useState<boolean>(false);
const [trackPBs, setTrackPBs] = useState<boolean>(false);
const [higherIsBetter, setHigherIsBetter] = useState<boolean>(true);

// If statType is null, we're adding a new stat type
const passedData: {
statType?: IStatType;
} = route.params;

const options: ISelectOption[] = [
const variantOptions: ISelectOption[] = [
{ label: 'Text', value: StatTypeVariant.TEXT },
{ label: 'Number', value: StatTypeVariant.NUMBER },
];

const higherLowerIsBetterOptions: ISelectOption[] = [
{ label: 'Higher is better', value: 1 },
{ label: 'Lower is better', value: 0 },
];

useEffect(() => {
if (passedData?.statType) {
navigation.setOptions({ title: 'Edit Stat Type' });
Expand All @@ -43,12 +48,12 @@ const AddEditStatType = ({ navigation, route }) => {
setName(statType.name);
if (statType.unit) setUnit(statType.unit);
setVariant(statType.variant);
if (statType.higherIsBetter !== undefined) setHigherIsBetter(statType.higherIsBetter);
if (statType.multipleValues !== undefined) setMultipleValues(statType.multipleValues);
if (statType.showBest !== undefined) setShowBest(statType.showBest);
if (statType.showAvg !== undefined) setShowAvg(statType.showAvg);
if (statType.showSum !== undefined) setShowSum(statType.showSum);
if (statType.trackPBs !== undefined) setTrackPBs(statType.trackPBs);
if (statType.higherIsBetter !== undefined) setTrackPBs(statType.higherIsBetter);
} else {
navigation.setOptions({ title: 'Add Stat Type' });
}
Expand All @@ -58,6 +63,10 @@ const AddEditStatType = ({ navigation, route }) => {
setVariant(value);
};

const changeHigherLowerIsBetter = (value: number) => {
setHigherIsBetter(!!value); // value will be either 1 or 0
};

const getShowMultiNumericOptions = (): boolean => {
return multipleValues && variant === StatTypeVariant.NUMBER;
};
Expand Down Expand Up @@ -86,16 +95,16 @@ const AddEditStatType = ({ navigation, route }) => {
};

if (unit) statType.unit = unit;
if (variant === StatTypeVariant.NUMBER) {
statType.higherIsBetter = higherIsBetter;
statType.trackPBs = trackPBs;
}
if (getCanHaveMultipleValues()) statType.multipleValues = multipleValues;
if (getShowMultiNumericOptions()) {
statType.showBest = showBest;
statType.showAvg = showAvg;
statType.showSum = showSum;
}
if (variant === StatTypeVariant.NUMBER) {
statType.trackPBs = trackPBs;
if (trackPBs) statType.higherIsBetter = higherIsBetter;
}

if (passedData?.statType) {
if (passedData.statType.pbs) statType.pbs = passedData.statType.pbs;
Expand Down Expand Up @@ -126,12 +135,24 @@ const AddEditStatType = ({ navigation, route }) => {
placeholderTextColor="grey"
onChangeText={(value) => setUnit(value)}
/>
<Text style={styles.label}>Variant:</Text>
<Text style={styles.label}>Variant</Text>
<Select
options={passedData?.statType ? [options.find((el) => el.value === variant)] : options}
options={
passedData?.statType ? [variantOptions.find((el) => el.value === variant)] : variantOptions
}
selected={variant}
onSelect={changeVariant}
/>
{variant === StatTypeVariant.NUMBER && (
<View style={{ marginHorizontal: 18 }}>
<Select
options={higherLowerIsBetterOptions}
selected={Number(higherIsBetter)}
onSelect={changeHigherLowerIsBetter}
horizontal
/>
</View>
)}
{getCanHaveMultipleValues() && (
// Checkbox disabled if editing stat type
<Checkbox checked={multipleValues} disabled={!!passedData?.statType} onChange={setMultipleValues}>
Expand All @@ -157,13 +178,6 @@ const AddEditStatType = ({ navigation, route }) => {
Track PBs
</Checkbox>
)}
{trackPBs && (
<View style={{ marginLeft: 24 }}>
<Checkbox checked={higherIsBetter} onChange={setHigherIsBetter}>
Higher is better
</Checkbox>
</View>
)}
<View style={{ marginVertical: 20 }}>
<Button
onPress={addEditStatType}
Expand All @@ -180,7 +194,9 @@ const styles = StyleSheet.create({
label: {
marginTop: 10,
marginBottom: 20,
textAlign: 'center',
fontSize: 22,
fontWeight: 'bold',
color: 'black',
},
});
Expand Down
23 changes: 15 additions & 8 deletions app/shared/DataStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export enum StatTypeVariant {
}

export interface IMultiValueStat {
low: number; // lowest value
high: number; // highest value
avg: number;
sum: number;
}

export interface IMultiValuePB {
best: number;
avg: number;
sum: number;
Expand All @@ -41,20 +48,20 @@ export interface IStatType {
unit?: string;
order: number; // goes from 1 to statTypes.length with no gaps
variant: StatTypeVariant;
higherIsBetter?: boolean; // NUMBER only
// choices?: string[]; // MULTIPLE_CHOICE only
// formula?: string; // FORMULA only
multipleValues?: boolean; // TEXT and NUMBER only
showBest?: boolean;
showAvg?: boolean;
showSum?: boolean;
trackPBs?: boolean;
// trackWorst?: boolean;
higherIsBetter?: boolean; // only set if trackPBs or trackWorst is on
showBest?: boolean; // NUMBER only
showAvg?: boolean; // NUMBER only
showSum?: boolean; // NUMBER only
trackPBs?: boolean; // TEXT (manual) and NUMBER only
// trackWorst?: boolean; // NUMBER only
// If this is unset and trackPBs is on, that means there aren't any pbs yet for this stat type
pbs?: {
allTime: {
entryId: number | IMultiValueStat;
result: number | IMultiValueStat;
entryId: number | IMultiValuePB;
result: number | IMultiValuePB;
};
};
}
Expand Down
4 changes: 2 additions & 2 deletions ios/Stat_Tracker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Stat_Tracker/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -511,7 +511,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
INFOPLIST_FILE = Stat_Tracker/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
4 changes: 2 additions & 2 deletions ios/Stat_Tracker/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.4.0</string>
<string>0.4.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>3</string>
<key>LSRequiresIPhoneOS</key>
<true />
<key>NSAppTransportSecurity</key>
Expand Down
4 changes: 2 additions & 2 deletions ios/Stat_TrackerTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>0.4.0</string>
<string>0.4.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>3</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Stat_Tracker",
"version": "0.4.0",
"version": "0.4.1",
"private": true,
"scripts": {
"dev": "alacritty -e yarn emulator & alacritty -e yarn start && sleep 2 & alacritty -e yarn android &",
Expand Down

0 comments on commit 94ed774

Please sign in to comment.