Skip to content

Commit

Permalink
Allow editing multiple choice stat type options, dont rearrange entri…
Browse files Browse the repository at this point in the history
…es when the date wasnt changed, fix bug with importing when there are overlaps between stat categories
  • Loading branch information
dmint789 committed Jan 3, 2023
1 parent 2779327 commit 56f3ce2
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 39 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 12
versionName "0.5"
versionCode 13
versionName "0.5.1"
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

if (isNewArchitectureEnabled()) {
Expand Down
82 changes: 63 additions & 19 deletions app/redux/mainSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,74 @@ import {
StatTypeVariant,
} from '../shared/DataStructure';

const verbose = true;

const addEditEntry = (entries: IEntry[], entry: IEntry): IEntry[] => {
if (verbose) {
console.log('Adding or editing entry:');
const addEditEntry = (entries: IEntry[], entry: IEntry, newEntry = false): IEntry[] => {
if (__DEV__) {
console.log(`${newEntry ? 'Adding' : 'Editing'} entry:`);
console.log(JSON.stringify(entry, null, 2));
}

const newEntries: IEntry[] = [];
let newEntries: IEntry[] = [];
let inserted = false;

if (entries.length > 0) {
for (let e of entries) {
if (!inserted && isNewerOrSameDate(entry.date, e.date)) {
newEntries.push(entry);
inserted = true;
if (newEntry) {
if (!isNewerOrSameDate(entry.date, entries[0].date)) {
for (let e of entries) {
if (!inserted && isNewerOrSameDate(entry.date, e.date)) {
if (__DEV__) console.log('Adding entry in the middle');
newEntries.push(entry);
inserted = true;
}
newEntries.push(e);
}

// If it's to become the very last element in the array of entries
if (!inserted) {
if (__DEV__) console.log('Adding entry at the bottom');
newEntries.push(entry);
}
} else {
if (__DEV__) console.log('Adding entry at the top');
entries.unshift(entry);
newEntries = entries;
}
if (e.id !== entry.id) {
newEntries.push(e);
} else {
let differentDate = true;
const uneditedEntry = entries.find((el) => el.id === entry.id);

if (!!uneditedEntry.date === !!entry.date) {
differentDate =
!!uneditedEntry.date &&
(uneditedEntry.date.day !== entry.date.day ||
uneditedEntry.date.month !== entry.date.month ||
uneditedEntry.date.year !== entry.date.year);
}
}

if (!inserted) newEntries.push(entry);
if (differentDate) {
for (let e of entries) {
if (!inserted && isNewerOrSameDate(entry.date, e.date)) {
if (__DEV__) console.log('Reordering the entry');
newEntries.push(entry);
inserted = true;
}
if (e.id !== entry.id) {
newEntries.push(e);
}
}

// If it's to become the very last element in the array of entries
if (!inserted) {
if (__DEV__) console.log('Reordering it to the bottom');
newEntries.push(entry);
}
} else {
if (__DEV__) console.log('Date is the same, not reordering');
newEntries = entries.map((el) => (el.id === entry.id ? entry : el));
}
}
} else {
if (__DEV__) console.log('Adding first entry');
// When there are no entries in the array
newEntries.push(entry);
}

Expand All @@ -42,7 +86,7 @@ const addEditEntry = (entries: IEntry[], entry: IEntry): IEntry[] => {

// The return value is whether or not the PB got updated
const updateStatTypePB = (state: any, statType: IStatType, entry: IEntry): boolean => {
if (verbose) console.log(`Checking if entry ${entry.id} has the PB for stat type ${statType.name}`);
if (__DEV__) console.log(`Checking if entry ${entry.id} has the PB for stat type ${statType.name}`);

let pbUpdated = false;
const stat = entry.stats.find((el: IStat) => el.type === statType.id);
Expand Down Expand Up @@ -98,7 +142,7 @@ 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}`);
if (__DEV__) 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 All @@ -110,7 +154,7 @@ const updatePBs = (state: any, entry: IEntry, mode: 'add' | 'edit' | 'delete') =

for (let statType of state.statTypes) {
if (statType.trackPBs && statType.variant === StatTypeVariant.NUMBER) {
if (verbose) console.log(`Updating PB for stat type ${statType.name}`);
if (__DEV__) console.log(`Updating PB for stat type ${statType.name}`);

let pbUpdated = false;

Expand Down Expand Up @@ -170,7 +214,7 @@ const updatePBs = (state: any, entry: IEntry, mode: 'add' | 'edit' | 'delete') =

PBsUpdated = pbUpdated || PBsUpdated;

if (verbose) {
if (__DEV__) {
if (pbUpdated) console.log('PB updated to: ', JSON.stringify(statType.pbs, null, 2));
else console.log('PB not updated');
}
Expand Down Expand Up @@ -241,7 +285,7 @@ const mainSlice = createSlice({
* Entries
*/
addEntry: (state, action: PayloadAction<IEntry>) => {
state.entries = addEditEntry(state.entries, action.payload);
state.entries = addEditEntry(state.entries, action.payload, true);
SM.setData(state.statCategory.id, 'entries', state.entries);

// Update PBs if needed
Expand Down
3 changes: 2 additions & 1 deletion app/screens/AddEditEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ const AddEditEntry = ({ navigation, route }) => {
Alert.alert('Error', error, [{ text: 'Ok' }]);
}
return false;
} else return true;
}
return true;
};

// Assumes the stat type has a default value
Expand Down
26 changes: 19 additions & 7 deletions app/screens/AddEditStatType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ const AddEditStatType = ({ navigation, route }) => {
} else if (variant === StatTypeVariant.NUMBER && isNaN(Number(defaultValue))) {
Alert.alert('Error', 'The default value for a numeric stat type must be a number', [{ text: 'Ok' }]);
return false;
} else if (variant === StatTypeVariant.MULTIPLE_CHOICE && !choicesAccepted) {
Alert.alert('Error', 'You must accept your options first', [{ text: 'Ok' }]);
return false;
} else return true;
} else if (variant === StatTypeVariant.MULTIPLE_CHOICE) {
if (!choicesAccepted) {
Alert.alert('Error', 'You must accept your options first', [{ text: 'Ok' }]);
return false;
} else if (!!passedData?.statType && choices.length < passedData.statType.choices.length) {
Alert.alert('Error', 'You cannot have fewer options than before', [{ text: 'Ok' }]);
return false;
}
}
return true;
};

const isValidChoices = (): boolean => {
Expand All @@ -129,11 +135,19 @@ const AddEditStatType = ({ navigation, route }) => {
if (nonEmptyChoices.length >= 2) {
setChoices(nonEmptyChoices);
setChoicesAccepted(true);
if (defaultValue > nonEmptyChoices.length) {
setDefaultValue(0);
}
} else {
Alert.alert('Error', 'Please enter at least two choices', [{ text: 'Ok' }]);
}
};

const editChoices = () => {
setChoicesAccepted(false);
setChoices((prevChoices) => [...prevChoices, '']);
};

const getChoicesOptions = (): ISelectOption[] => {
return [
{
Expand Down Expand Up @@ -258,9 +272,7 @@ const AddEditStatType = ({ navigation, route }) => {
onSelect={(value: number) => setDefaultValue(value)}
/>
</View>
{!passedData?.statType && (
<Button color="blue" title="Edit Options" onPress={() => setChoicesAccepted(false)} />
)}
<Button color="blue" title="Edit Options" onPress={editChoices} />
</>
))}
{getCanHaveMultipleValues() && (
Expand Down
8 changes: 5 additions & 3 deletions app/shared/StorageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ScopedStorage from 'react-native-scoped-storage';
import { formatDate } from './GlobalFunctions';
import { IStatCategory, IBackupData, dataPoints } from './DataStructure';

const verbose = true;
const verbose = true && __DEV__;

export const getData = async (categoryId: number, request: string) => {
const key: string = categoryId + '_' + request;
Expand Down Expand Up @@ -141,6 +141,8 @@ export const importData = async (
const data: IBackupData = JSON.parse(backupFile.data);

if (verbose) {
console.log('Stat categories before import:');
console.log(JSON.stringify(statCategories, null, 2));
console.log('Backup file data:');
console.log(JSON.stringify(data, null, 2));
}
Expand Down Expand Up @@ -173,14 +175,14 @@ export const importData = async (
successMessage = successMessage.slice(0, -2);
}

setStatCategories(data.statCategories);

for (let i of dataPoints) {
for (let statCategory of data.statCategories) {
setData(statCategory.id, i, data[i][statCategory.id]);
}
}

setStatCategories([...statCategories, ...data.statCategories]);

return { message: successMessage, error: '' };
} else {
return { error: 'Backup file is invalid', message: '' };
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 = 12;
CURRENT_PROJECT_VERSION = 13;
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 = 12;
CURRENT_PROJECT_VERSION = 13;
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.5</string>
<string>0.5.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>12</string>
<string>13</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.5</string>
<string>0.5.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>12</string>
<string>13</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.5",
"version": "0.5.1",
"private": true,
"scripts": {
"dev": "alacritty -e yarn emulator & alacritty -e yarn start && sleep 3 & alacritty -e yarn android &",
Expand Down

0 comments on commit 56f3ce2

Please sign in to comment.