diff --git a/android/app/build.gradle b/android/app/build.gradle index 427fb07f..74f1b6ba 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -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. diff --git a/cypress/e2e/ingredients.cy.ts b/cypress/e2e/ingredients.cy.ts index ffcae8fa..bf169ce0 100644 --- a/cypress/e2e/ingredients.cy.ts +++ b/cypress/e2e/ingredients.cy.ts @@ -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'); @@ -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 } ]) ); @@ -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: [] } ]) ); diff --git a/fastlane/metadata/android/en-US/changelogs/15401.txt b/fastlane/metadata/android/en-US/changelogs/15401.txt new file mode 100644 index 00000000..9990d107 --- /dev/null +++ b/fastlane/metadata/android/en-US/changelogs/15401.txt @@ -0,0 +1 @@ +• Fixed bug with data storage when user had more than two bars diff --git a/src/services/local-storage-service.ts b/src/services/local-storage-service.ts index ce9c9a36..98b172ea 100644 --- a/src/services/local-storage-service.ts +++ b/src/services/local-storage-service.ts @@ -27,8 +27,6 @@ export class LocalStorageService { private _activeIngredientListId = 0; public async initialize(): Promise { - await this.migrateSavedIngredients(); - const ingredientLists = await this.getFromLocalStorage(StorageKey.IngredientLists); this._ingredientLists = ingredientLists !== null ? ingredientLists : []; @@ -36,13 +34,17 @@ export class LocalStorageService { 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); @@ -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, @@ -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); @@ -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', diff --git a/tests/services/local-storage-service.test.ts b/tests/services/local-storage-service.test.ts index 01be6e2b..9ea9d803 100644 --- a/tests/services/local-storage-service.test.ts +++ b/tests/services/local-storage-service.test.ts @@ -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']); - }); });