Skip to content

Commit

Permalink
fix: multiple bars bug (#554)
Browse files Browse the repository at this point in the history
* fix: multiple bars bug

* fix

* fix

* fix
  • Loading branch information
anton-gustafsson authored Sep 24, 2024
1 parent 9a196a9 commit a5914ec
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 96 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.moimob.drinkable"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 15400
versionName "1.54.0"
versionCode 15401
versionName "1.54.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
aaptOptions {
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
Expand Down
16 changes: 11 additions & 5 deletions cypress/e2e/ingredients.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ describe('Ingredients', () => {

const highestIngredientId = 300;
window.localStorage.setItem(
'CapacitorStorage.saved-ingredients',
JSON.stringify(Array.from(Array(highestIngredientId + 1).keys()).slice(1))
'CapacitorStorage.ingredient-lists',
JSON.stringify([
{
name: 'My Bar',
ingredients: Array.from(Array(highestIngredientId + 1).keys()).map(i => i.toString()),
id: 0
}
])
);

cy.visit('#/ingredients');
Expand All @@ -56,8 +62,8 @@ describe('Ingredients', () => {
window.localStorage.setItem(
'CapacitorStorage.ingredient-lists',
JSON.stringify([
{ name: 'My Bar', ingredients: [] },
{ name: 'Test', ingredients: [] }
{ name: 'My Bar', ingredients: [], id: 0 },
{ name: 'Test', ingredients: [], id: 1 }
])
);

Expand All @@ -80,7 +86,7 @@ describe('Ingredients', () => {
window.localStorage.setItem(
'CapacitorStorage.ingredient-lists',
JSON.stringify([
{ name: 'My Bar', ingredients: [] },
{ name: 'My Bar', ingredients: [], id: 0 },
{ name: 'Test', ingredients: [] }
])
);
Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/15401.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
• Fixed bug with data storage when user had more than two bars
44 changes: 13 additions & 31 deletions src/services/local-storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,24 @@ export class LocalStorageService {
private _activeIngredientListId = 0;

public async initialize(): Promise<void> {
await this.migrateSavedIngredients();

const ingredientLists = await this.getFromLocalStorage(StorageKey.IngredientLists);
this._ingredientLists = ingredientLists !== null ? ingredientLists : [];

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

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

this.updateIngredientLists(this._ingredientLists);
uniqueIds.forEach(element => {
const list = this._ingredientLists.find(x => x.id === element);
if (list) {
newLists.push(list);
}
});
await this.updateIngredientLists(newLists);
}

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

/**
* Migration made 2023-09-06. Remove after 6 months?
*/
private async migrateSavedIngredients() {
const savedIngredientsStorageKey: StorageKey = StorageKey.SavedIngredients;

const keyExists = await this.keyExists(savedIngredientsStorageKey);

if (keyExists) {
const savedIngredientsResponse = await this.getFromLocalStorage(savedIngredientsStorageKey);
const savedIngredients: string[] =
savedIngredientsResponse !== null ? savedIngredientsResponse.map(String) : [];

await this.addDefaultIngredientList(savedIngredients);
await Preferences.remove({ key: savedIngredientsStorageKey });
}
}

private async addDefaultIngredientList(savedIngredients: string[]) {
const ingredientList: IngredientList = {
id: 0,
Expand Down Expand Up @@ -138,10 +122,12 @@ export class LocalStorageService {
}

public async createIngredientList(name: string) {
const newId = this._ingredientLists.map(x => x.id).sort((a, b) => b - a)[0] + 1;

const list: IngredientList = {
ingredients: [],
name: name,
id: this._ingredientLists.map(x => x.id).sort((a, b) => a + b)[0] + 1
id: newId
};

this._ingredientLists.push(list);
Expand Down Expand Up @@ -426,10 +412,6 @@ export class LocalStorageService {
}

export enum StorageKey {
/**
* @deprecated SavedIngredients have been replaced with IngredientLists
*/
SavedIngredients = 'saved-ingredients',
MessuarementSystem = 'messuarement-system',
WidgetOrder = 'widget-order',
Cocktails = 'cocktails',
Expand Down
58 changes: 0 additions & 58 deletions tests/services/local-storage-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,62 +35,4 @@ describe('LocalStorageService', () => {
const result = await sut.keyExists(StorageKey.MessuarementSystem);
expect(result).toBe(true);
});

test('Initialize - Map from old numbers array', async () => {
window.localStorage.setItem('CapacitorStorage.saved-ingredients', JSON.stringify([1, 2, 3]));
await sut.initialize();

const result = sut.getIngredientIds();

expect(result).toHaveLength(3);
expect(result).toStrictEqual(['1', '2', '3']);
});

test('Initialize - Map new array', async () => {
window.localStorage.setItem('CapacitorStorage.saved-ingredients', JSON.stringify(['1', '2', '3']));
await sut.initialize();

const result = sut.getIngredientIds();

expect(result).toHaveLength(3);
expect(result).toStrictEqual(['1', '2', '3']);
});

test('Initialize - Migrate from Saved Ingredients', async () => {
const key = 'CapacitorStorage.saved-ingredients';

window.localStorage.setItem(key, JSON.stringify(['1', '2', '3']));

await sut.initialize();

const ingredientLists = JSON.parse(
window.localStorage.getItem('CapacitorStorage.ingredient-lists')
) as IngredientList[];

expect(window.localStorage.getItem(key)).toBeNull();

expect(ingredientLists.length).toBe(1);
expect(ingredientLists[0].ingredients).toEqual(['1', '2', '3']);
expect(ingredientLists[0].id).toBe(0);
expect(ingredientLists[0].name).toBe('My Bar');
});

test('Initialize - Remove Ingredient with id 150', async () => {
const key = 'CapacitorStorage.' + StorageKey.IngredientLists;

window.localStorage.setItem(
key,
JSON.stringify([
{ name: 'My Bar', ingredients: ['1', '150'] },
{ name: 'Test', ingredients: ['150', '2'] }
])
);

await sut.initialize();

const ingredientLists = JSON.parse(window.localStorage.getItem(key)) as IngredientList[];
expect(ingredientLists.length).toBe(2);
expect(ingredientLists[0].ingredients).toEqual(['1']);
expect(ingredientLists[1].ingredients).toEqual(['2']);
});
});

0 comments on commit a5914ec

Please sign in to comment.