Skip to content

Commit

Permalink
added archived lists functionality - will expand
Browse files Browse the repository at this point in the history
  • Loading branch information
Programming-With-Chris committed Aug 23, 2022
1 parent 03d16d9 commit 50c87dc
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ShoppingList/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
ContentTemplate="{DataTemplate views:SettingsView}"/>
<!-- Had to remove Icon, it was causing issues on IOS, Settings page wouldn't route-->

<ShellContent Title="Your Archived Lists"
Route="ArchivedListView"
ContentTemplate="{DataTemplate views:ArchivedListView}"/>
</FlyoutItem>

</Shell>
2 changes: 2 additions & 0 deletions ShoppingList/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static MauiApp CreateMauiApp()
builder.Services.AddTransient<UserListDetailViewModel>();
builder.Services.AddTransient<UserListDataInputViewModel>();
builder.Services.AddSingleton<MainPage>();
builder.Services.AddSingleton<ArchivedListView>();
builder.Services.AddSingleton<ArchivedListViewModel>();
builder.Services.AddTransient<UserListDetails>();
builder.Services.AddTransient<UserListDataInput>();
builder.Services.AddSingleton<SettingsView>();
Expand Down
6 changes: 6 additions & 0 deletions ShoppingList/Model/UserList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public enum ListType
[Column("type")]
public ListType Type { get; set; }

[Column("creation_dt")]
public DateTime CreationDate { get; set; }

[Column("archive_dt")]
public DateTime? ArchiveDate { get; set; }

public UserList()
{
Items = new();
Expand Down
33 changes: 29 additions & 4 deletions ShoppingList/Services/UserListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ public UserListService(DatabaseHandler db)

public List<UserList> GetUserLists()
{
var returnLists = _db.GetAllQuery<UserList>();
var returnLists = _db.GetAllQuery<UserList>();

returnLists = returnLists.Where(x => x.ArchiveDate is null).ToList<UserList>();
return returnLists;
}

public List<UserList> GetArchivedUserLists()
{
var returnLists = _db.GetAllQuery<UserList>();

returnLists = returnLists.Where(x => x.ArchiveDate is not null).ToList<UserList>();
return returnLists;
}

Expand All @@ -41,15 +51,30 @@ public UserList CreateUserList(UserList newlist)
{
Guard.IsNotNull(newlist, nameof(newlist));

newlist.CreationDate = DateTime.Now;

return _db.Insert(newlist);

}

public void DeleteUserList(UserList newlist)
public void ArchiveUserList(UserList delList)
{
Guard.IsNotNull(newlist, nameof(newlist));
Guard.IsNotNull(delList, nameof(delList));

_db.Delete(newlist);
delList.ArchiveDate = DateTime.Now;

_db.Update<UserList>(delList);

}

public void UnarchiveUserList(UserList userList)
{
Guard.IsNotNull(userList);

userList.ArchiveDate = null;

_db.Update<UserList>(userList);

}

public UserList GetLastUserListOfType(UserList newList)
Expand Down
6 changes: 6 additions & 0 deletions ShoppingList/ShoppingList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@
<MauiXaml Update="View\UserListDetails.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Condition=" '$(EnableDefaultXamlItems)' == 'true' " Update="View\MainPage %28copy%29.xaml">
<SubType>Designer</SubType>
</MauiXaml>
<MauiXaml Condition=" '$(EnableDefaultXamlItems)' == 'true' " Update="View\ArchivedListView.xaml">
<SubType>Designer</SubType>
</MauiXaml>
</ItemGroup>

<ItemGroup>
Expand Down
44 changes: 44 additions & 0 deletions ShoppingList/View/ArchivedListView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model="clr-namespace:ShoppingList.Model"
xmlns:viewmodel="clr-namespace:ShoppingList.ViewModels"
x:DataType="viewmodel:ArchivedListViewModel"
Title="{Binding Title}"
x:Name="CurrentPage"
BackgroundColor="{DynamicResource Secondary}"
x:Class="ShoppingList.ArchivedListView">

<Grid ColumnDefinitions="*,*"
ColumnSpacing="5"
RowDefinitions="*,Auto">

<CollectionView Grid.ColumnSpan="2"
ItemsSource="{Binding UserLists}"
SelectionMode="None">
<CollectionView.ItemTemplate >
<DataTemplate x:DataType="model:UserList">
<Grid Padding="10">
<Frame BackgroundColor="{DynamicResource Tertiary}">
<Frame.GestureRecognizers>
<TapGestureRecognizer CommandParameter="{Binding .}"
Tapped="FrameTapped"/>
</Frame.GestureRecognizers>
<VerticalStackLayout>
<Label Text="{Binding Name}"
FontSize="Medium"
TextColor="{DynamicResource Accent}"/>
<Label Text="{Binding Type}"
FontSize="Micro"
TextColor="{DynamicResource Accent}"
Margin="25,0,0,0"/>
</VerticalStackLayout>
</Frame>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

</Grid>

</ContentPage>
42 changes: 42 additions & 0 deletions ShoppingList/View/ArchivedListView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using ShoppingList.ViewModels;
using ShoppingList.Controls;

namespace ShoppingList;

public partial class ArchivedListView : ContentPage
{
ArchivedListViewModel _alvm;

public ArchivedListView(ArchivedListViewModel archivedListViewModel)
{
InitializeComponent();
BindingContext = archivedListViewModel;
_alvm = archivedListViewModel;
_alvm.GetArchivedLists();

}

//There is currently a bug in .net maui where OnAppearing doesn't trigger on Shell Items
// like it should - Leaving this here anyways for when it's fixed
// https://github.com/dotnet/maui/issues/8164
protected override void OnAppearing()
{
base.OnAppearing();
_alvm.GetArchivedLists();

}

private async void FrameTapped(object sender, EventArgs e)
{

var frame = sender as Frame;
await frame.ScaleTo(1.1, 75, Easing.BounceIn);
await frame.ScaleTo(1.0, 75, Easing.BounceOut);

var userList = ((TappedEventArgs)e).Parameter as UserList;

_alvm.RestoreUserList(userList);

}
}

8 changes: 7 additions & 1 deletion ShoppingList/View/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ public MainPage(UserListViewModel ulViewModel)

}

private async void NewListButtonPressed(object sender, EventArgs e)
protected override void OnAppearing()
{
base.OnAppearing();
_ulvm.GetUserLists();
}

private async void NewListButtonPressed(object sender, EventArgs e)
{

var circularButton = sender as CircularButton;
Expand Down
85 changes: 85 additions & 0 deletions ShoppingList/ViewModel/ArchivedListViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using ShoppingList.Model.Api;

namespace ShoppingList.ViewModels;

public partial class ArchivedListViewModel : BaseViewModel
{

private UserListService _uls;
private ItemService _itemService;

public ObservableCollection<UserList> UserLists { get; } = new();

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

public ArchivedListViewModel(UserListService userListService, ItemService itemService)
{
_uls = userListService;
_itemService = itemService;
Title = "My Archived Lists";
}

[RelayCommand]
public void GetArchivedLists()
{
if (IsBusy)
return;

try
{
IsBusy = true;
var userLists = _uls.GetArchivedUserLists();

if (UserLists.Count != 0)
UserLists.Clear();

foreach (var userList in userLists)
{
UserLists.Add(userList);
}

}
catch (Exception e)
{
Debug.WriteLine(e);
Shell.Current.DisplayAlert("Error!",
$"Unable to get UserLists: {e.Message}", "Ok");
}
finally
{
IsBusy = false;
}
}
[RelayCommand]
public async void CreateUserList()
{
if (IsBusy)
return;


await Shell.Current.GoToAsync($"{nameof(UserListDataInput)}", true,
new Dictionary<string, object>
{
{"UserLists", UserLists}
});

}

[RelayCommand]
public async void RestoreUserList(UserList restoreList)
{
if (IsBusy)
return;

bool shouldRestore = await Shell.Current.DisplayAlert("Restore List?", "Would You Like To Restore This List",
"Yes", "Cancel");

if (shouldRestore)
_uls.UnarchiveUserList(restoreList);


GetArchivedLists();
}
}
2 changes: 1 addition & 1 deletion ShoppingList/ViewModel/UserListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ await Shell.Current.GoToAsync($"{nameof(UserListDetails)}?id={ul.Id}", true,
[RelayCommand]
public void DeleteItem(UserList ul)
{
_uls.DeleteUserList(ul);
_uls.ArchiveUserList(ul);

UserLists.Remove(ul);

Expand Down

0 comments on commit 50c87dc

Please sign in to comment.