Exemplo n.º 1
0
        private SettingsService()
        {
            _helper = new Template10.Services.SettingsService.SettingsHelper();

            //Initialize GroceryItemTypes
            GroceryItemType[] savedGroceryTypes = _helper.Read(nameof(GroceryTypes), new GroceryItemType[]
            {
                new GroceryItemType("Milk, 1L"),
                new GroceryItemType("Bread"),
                new GroceryItemType("Cheese"),
                new GroceryItemType("Ketchup"),
                new GroceryItemType("Weiner schnitzel"),
                new GroceryItemType("Juice"),
                new GroceryItemType("Herb tomato pureé")
            });
            _groceryTypes.AddRange(savedGroceryTypes);
            GroceryTypes = _groceryTypes.Connect();

            //Initialize GroceryItems
            InventoryEntry[] savedInventory = _helper.Read(nameof(InventoryItems), new InventoryEntry[0]);
            _inventoryItems.AddRange(savedInventory);
            InventoryItems = _inventoryItems.Connect();

            //Initialize ShoppingListItems
            ShoppingListEntry[] savedShoppingList = _helper.Read(nameof(ShoppingListItems), new ShoppingListEntry[0]);
            _shoppingListItems.AddRange(savedShoppingList);
            ShoppingListItems = _shoppingListItems.Connect();

            //Write changes to Grocery Types 30 seconds after changes stop coming in
            GroceryTypes.Throttle(TimeSpan.FromSeconds(30))
            .Subscribe(
                onNext: _ => _helper.Write(nameof(GroceryTypes), _groceryTypes.Items)
                );

            //Write changes to Inventory Items to disk 30 seconds after changes stop coming in
            InventoryItems.Throttle(TimeSpan.FromSeconds(30))
            .Subscribe(
                onNext: _ => _helper.Write(nameof(InventoryItems), _inventoryItems.Items)
                );

            //Write changed to the Shopping List items to disk 30 seconds after changes stop coming in
            ShoppingListItems.Throttle(TimeSpan.FromSeconds(30))
            .Subscribe(
                onNext: _ => _helper.Write(nameof(ShoppingListItems), _shoppingListItems.Items)
                );
        }