Skip to content

Commit a5914ec

Browse files
fix: multiple bars bug (#554)
* fix: multiple bars bug * fix * fix * fix
1 parent 9a196a9 commit a5914ec

File tree

5 files changed

+27
-96
lines changed

5 files changed

+27
-96
lines changed

android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.moimob.drinkable"
88
minSdkVersion rootProject.ext.minSdkVersion
99
targetSdkVersion rootProject.ext.targetSdkVersion
10-
versionCode 15400
11-
versionName "1.54.0"
10+
versionCode 15401
11+
versionName "1.54.1"
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
aaptOptions {
1414
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.

cypress/e2e/ingredients.cy.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ describe('Ingredients', () => {
3535

3636
const highestIngredientId = 300;
3737
window.localStorage.setItem(
38-
'CapacitorStorage.saved-ingredients',
39-
JSON.stringify(Array.from(Array(highestIngredientId + 1).keys()).slice(1))
38+
'CapacitorStorage.ingredient-lists',
39+
JSON.stringify([
40+
{
41+
name: 'My Bar',
42+
ingredients: Array.from(Array(highestIngredientId + 1).keys()).map(i => i.toString()),
43+
id: 0
44+
}
45+
])
4046
);
4147

4248
cy.visit('#/ingredients');
@@ -56,8 +62,8 @@ describe('Ingredients', () => {
5662
window.localStorage.setItem(
5763
'CapacitorStorage.ingredient-lists',
5864
JSON.stringify([
59-
{ name: 'My Bar', ingredients: [] },
60-
{ name: 'Test', ingredients: [] }
65+
{ name: 'My Bar', ingredients: [], id: 0 },
66+
{ name: 'Test', ingredients: [], id: 1 }
6167
])
6268
);
6369

@@ -80,7 +86,7 @@ describe('Ingredients', () => {
8086
window.localStorage.setItem(
8187
'CapacitorStorage.ingredient-lists',
8288
JSON.stringify([
83-
{ name: 'My Bar', ingredients: [] },
89+
{ name: 'My Bar', ingredients: [], id: 0 },
8490
{ name: 'Test', ingredients: [] }
8591
])
8692
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
• Fixed bug with data storage when user had more than two bars

src/services/local-storage-service.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,24 @@ export class LocalStorageService {
2727
private _activeIngredientListId = 0;
2828

2929
public async initialize(): Promise<void> {
30-
await this.migrateSavedIngredients();
31-
3230
const ingredientLists = await this.getFromLocalStorage(StorageKey.IngredientLists);
3331
this._ingredientLists = ingredientLists !== null ? ingredientLists : [];
3432

3533
if (this._ingredientLists.length === 0) {
3634
await this.addDefaultIngredientList([]);
3735
}
3836

39-
// 2023-11-27 - Remove Ingredient 150 due to duplication
40-
if (this._ingredientLists.flatMap(x => x.ingredients).find(x => x === '150') !== undefined) {
41-
this._ingredientLists.forEach(x => {
42-
x.ingredients = x.ingredients.filter(y => y !== '150');
43-
});
37+
const uniqueIds = [...new Set(this._ingredientLists.map(x => x.id))];
38+
if (this._ingredientLists.map(x => x.id).length !== uniqueIds.length) {
39+
const newLists = [];
4440

45-
this.updateIngredientLists(this._ingredientLists);
41+
uniqueIds.forEach(element => {
42+
const list = this._ingredientLists.find(x => x.id === element);
43+
if (list) {
44+
newLists.push(list);
45+
}
46+
});
47+
await this.updateIngredientLists(newLists);
4648
}
4749

4850
const messuarementSystem = await this.getFromLocalStorage(StorageKey.MessuarementSystem, false);
@@ -72,24 +74,6 @@ export class LocalStorageService {
7274
this._shoppingLists = shoppingLists !== null ? shoppingLists : [];
7375
}
7476

75-
/**
76-
* Migration made 2023-09-06. Remove after 6 months?
77-
*/
78-
private async migrateSavedIngredients() {
79-
const savedIngredientsStorageKey: StorageKey = StorageKey.SavedIngredients;
80-
81-
const keyExists = await this.keyExists(savedIngredientsStorageKey);
82-
83-
if (keyExists) {
84-
const savedIngredientsResponse = await this.getFromLocalStorage(savedIngredientsStorageKey);
85-
const savedIngredients: string[] =
86-
savedIngredientsResponse !== null ? savedIngredientsResponse.map(String) : [];
87-
88-
await this.addDefaultIngredientList(savedIngredients);
89-
await Preferences.remove({ key: savedIngredientsStorageKey });
90-
}
91-
}
92-
9377
private async addDefaultIngredientList(savedIngredients: string[]) {
9478
const ingredientList: IngredientList = {
9579
id: 0,
@@ -138,10 +122,12 @@ export class LocalStorageService {
138122
}
139123

140124
public async createIngredientList(name: string) {
125+
const newId = this._ingredientLists.map(x => x.id).sort((a, b) => b - a)[0] + 1;
126+
141127
const list: IngredientList = {
142128
ingredients: [],
143129
name: name,
144-
id: this._ingredientLists.map(x => x.id).sort((a, b) => a + b)[0] + 1
130+
id: newId
145131
};
146132

147133
this._ingredientLists.push(list);
@@ -426,10 +412,6 @@ export class LocalStorageService {
426412
}
427413

428414
export enum StorageKey {
429-
/**
430-
* @deprecated SavedIngredients have been replaced with IngredientLists
431-
*/
432-
SavedIngredients = 'saved-ingredients',
433415
MessuarementSystem = 'messuarement-system',
434416
WidgetOrder = 'widget-order',
435417
Cocktails = 'cocktails',

tests/services/local-storage-service.test.ts

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -35,62 +35,4 @@ describe('LocalStorageService', () => {
3535
const result = await sut.keyExists(StorageKey.MessuarementSystem);
3636
expect(result).toBe(true);
3737
});
38-
39-
test('Initialize - Map from old numbers array', async () => {
40-
window.localStorage.setItem('CapacitorStorage.saved-ingredients', JSON.stringify([1, 2, 3]));
41-
await sut.initialize();
42-
43-
const result = sut.getIngredientIds();
44-
45-
expect(result).toHaveLength(3);
46-
expect(result).toStrictEqual(['1', '2', '3']);
47-
});
48-
49-
test('Initialize - Map new array', async () => {
50-
window.localStorage.setItem('CapacitorStorage.saved-ingredients', JSON.stringify(['1', '2', '3']));
51-
await sut.initialize();
52-
53-
const result = sut.getIngredientIds();
54-
55-
expect(result).toHaveLength(3);
56-
expect(result).toStrictEqual(['1', '2', '3']);
57-
});
58-
59-
test('Initialize - Migrate from Saved Ingredients', async () => {
60-
const key = 'CapacitorStorage.saved-ingredients';
61-
62-
window.localStorage.setItem(key, JSON.stringify(['1', '2', '3']));
63-
64-
await sut.initialize();
65-
66-
const ingredientLists = JSON.parse(
67-
window.localStorage.getItem('CapacitorStorage.ingredient-lists')
68-
) as IngredientList[];
69-
70-
expect(window.localStorage.getItem(key)).toBeNull();
71-
72-
expect(ingredientLists.length).toBe(1);
73-
expect(ingredientLists[0].ingredients).toEqual(['1', '2', '3']);
74-
expect(ingredientLists[0].id).toBe(0);
75-
expect(ingredientLists[0].name).toBe('My Bar');
76-
});
77-
78-
test('Initialize - Remove Ingredient with id 150', async () => {
79-
const key = 'CapacitorStorage.' + StorageKey.IngredientLists;
80-
81-
window.localStorage.setItem(
82-
key,
83-
JSON.stringify([
84-
{ name: 'My Bar', ingredients: ['1', '150'] },
85-
{ name: 'Test', ingredients: ['150', '2'] }
86-
])
87-
);
88-
89-
await sut.initialize();
90-
91-
const ingredientLists = JSON.parse(window.localStorage.getItem(key)) as IngredientList[];
92-
expect(ingredientLists.length).toBe(2);
93-
expect(ingredientLists[0].ingredients).toEqual(['1']);
94-
expect(ingredientLists[1].ingredients).toEqual(['2']);
95-
});
9638
});

0 commit comments

Comments
 (0)