Skip to content

Commit

Permalink
Added IsChecked to item model
Browse files Browse the repository at this point in the history
  • Loading branch information
Programming-With-Chris committed Jul 18, 2022
1 parent f41185a commit 1fb1905
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 41 deletions.
3 changes: 3 additions & 0 deletions ShoppingList/Model/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ public class Item
[ForeignKey(typeof(UserList))]
[Column("parentid")]
public int ParentId { get; set; }

[Column("iscompleted")]
public bool IsCompleted { get; set; }
}
12 changes: 10 additions & 2 deletions ShoppingList/Services/DatabaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace ShoppingList.Services;
public class DatabaseHandler
{
private SQLiteConnection _db;
private string _pathToDb;
private readonly SQLiteConnection _db;
private readonly string _pathToDb;


/// <summary>
Expand Down Expand Up @@ -130,4 +130,12 @@ public DatabaseHandler(string newDBName)
if (numOfRowsFound == 0)
throw new Exception("No Rows Found To Delete in Table " + typeof(T).Name + "s.");
}

public void Update<T>(T updateTarget) where T : new()
{
var numOfRowsFound = _db.Update(updateTarget);

if (numOfRowsFound == 0)
throw new Exception("No Rows Found To Update in Table " + typeof(T).Name + "s.");
}
}
5 changes: 5 additions & 0 deletions ShoppingList/Services/ItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public void DeleteItem(Item deletedItem)
{
_db.Delete(deletedItem);
}

public void UpdateItem(Item updatedItem)
{
_db.Update(updatedItem);
}
}


14 changes: 14 additions & 0 deletions ShoppingList/Services/ListSorter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingList.Services
{
public class ListSorter
{


}
}
4 changes: 4 additions & 0 deletions ShoppingList/View/UserListDetails.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model="clr-namespace:ShoppingList.Model"
xmlns:viewmodel="clr-namespace:ShoppingList.ViewModels"
xmlns:view="clr-namespace:ShoppingList"
x:DataType="viewmodel:UserListDetailViewModel"
x:Class="ShoppingList.UserListDetails">

Expand Down Expand Up @@ -37,6 +38,9 @@
Padding="1"
Margin="1">
<CheckBox Color="{StaticResource Secondary}"
x:DataType="model:Item"
IsChecked="{Binding IsCompleted}"
CheckedChanged="OnCheckboxClicked"
Grid.Row="1"
Grid.RowSpan="2"
Grid.Column="0" />
Expand Down
12 changes: 10 additions & 2 deletions ShoppingList/View/UserListDetails.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ namespace ShoppingList;

public partial class UserListDetails : ContentPage
{
UserListDetailViewModel _ulvm;

public UserListDetails(UserListDetailViewModel userListDetailViewModel)
{
InitializeComponent();
BindingContext = userListDetailViewModel;
_ulvm = userListDetailViewModel;
//userListDetailViewModel.RefreshUserListDetailScreen();

}



public void OnCheckboxClicked(object sender, CheckedChangedEventArgs e )
{
Item itemThatWasClicked = (Item)((CheckBox)sender).BindingContext;
itemThatWasClicked.IsCompleted = ((CheckBox)sender).IsChecked;
_ulvm.ItemWasChecked(itemThatWasClicked);
}
}

43 changes: 6 additions & 37 deletions ShoppingList/ViewModel/UserListDetailViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
namespace ShoppingList.ViewModels;

[QueryProperty("UserList", "UserList")]
//[QueryProperty("CreateFlag", "createflag")]
//[QueryProperty("NewItemId", "id")]
public partial class UserListDetailViewModel : BaseViewModel
{
readonly ItemService _itemService;
int _newItemId;

public UserListDetailViewModel()
{
Expand All @@ -22,29 +19,6 @@ public UserListDetailViewModel()
[ObservableProperty]
List<Item> items;


//public bool CreateFlag { get; set; } = false;

//TODO there is probably a better way to do this? idk
/* public int NewItemId
{
get { return _newItemId; }
set
{
if (CreateFlag)
{
_newItemId = value;
Item newItem = new()
{
Id = value
};
newItem = _itemService.GetItemById(newItem);
UserList.Items.Add(newItem);
CreateFlag = false;
}
}
}*/


[ICommand]
public async void CreateItem(UserList ul)
Expand All @@ -62,12 +36,6 @@ await Shell.Current.GoToAsync($"{nameof(ItemInput)}", true,
[ICommand]
public async void GoBackToListScreen()
{
//Shell.Current.GoToAsync("..?createflag=false");
/*await Shell.Current.GoToAsync("..?createflag=false", true,
new Dictionary<string, object>
{
{"UserList", userList}
});*/
await Shell.Current.GoToAsync($"//{nameof(MainPage)}");
}

Expand All @@ -84,11 +52,12 @@ public async void GoToItemDetail(Item item)
{
await Shell.Current.DisplayAlert(item.Name, $"Category: {item.Category} \nDescription: {item.Description} \n" +
$"Aisle: {item.Aisle}", "Ok");
/* await Shell.Current.GoToAsync(nameof(ItemDetail), true,
new Dictionary<string, object>
{
{"Item", item }
}); */
}

[ICommand]
public void ItemWasChecked(Item item)
{
_itemService.UpdateItem(item);
}


Expand Down

0 comments on commit 1fb1905

Please sign in to comment.