Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Save and Load
Browse files Browse the repository at this point in the history
ref #45
  • Loading branch information
dhcgn committed Apr 7, 2020
1 parent aceb76f commit e3738e7
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 33 deletions.
11 changes: 7 additions & 4 deletions Planungsboard.Presentation/UserControls/MenuUserControl.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<UserControl x:Class="Planungsboard.Presentation.MenuUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels="clr-namespace:Planungsboard.Presentation.ViewModels">
xmlns:viewModels="clr-namespace:Planungsboard.Presentation.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

<UserControl.Resources>
<Style x:Key="BaseStyle" TargetType="FrameworkElement">
Expand All @@ -13,6 +15,7 @@
</Style>
<Style x:Key="SideBarHintStyle" TargetType="TextBlock" BasedOn="{StaticResource BaseStyle}">
<Setter Property="FontSize" Value="10" />
<Setter Property="Foreground" Value="Gray" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>

Expand All @@ -23,7 +26,7 @@
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<StackPanel Grid.Row="0" d:DataContext="{d:DesignInstance viewModels:MainViewModel }">
<Button Content="Open On GitHib" FontSize="5" Style="{StaticResource SideBarButtonStyle}"
CommandParameter="https://github.com/haevg-rz/Planungsboard/" />
<Rectangle Height="100" />
Expand All @@ -33,8 +36,8 @@

<Separator Style="{StaticResource BaseStyle}" />

<Button Content="Save" Style="{StaticResource SideBarButtonStyle}" />
<Button Content="Load" Style="{StaticResource SideBarButtonStyle}" />
<Button Content="Save" Style="{StaticResource SideBarButtonStyle}" Command="{Binding SaveCommand}" />
<Button Content="Load" Style="{StaticResource SideBarButtonStyle}" Command="{Binding LoadCommand}"/>

</StackPanel>

Expand Down
137 changes: 108 additions & 29 deletions Planungsboard.Presentation/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text.Json;
using System.Text.Json.Serialization;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using Microsoft.Win32;
using Planungsboard.Presentation.Message;
using Planungsboard.Presentation.Views;

Expand All @@ -17,6 +23,8 @@ public MainViewModel()
this.QuarterBackCommand = new RelayCommand(this.QuarterBackCommandHandling);
this.QuarterNextCommand = new RelayCommand(this.QuarterNextCommandHandling);
this.NewTeamCommand = new RelayCommand(this.NewTeamCommandHandling);
this.SaveCommand = new RelayCommand(this.SaveCommandHandling);
this.LoadCommand = new RelayCommand(this.LoadCommandHandling);

this.DisplayQuarters = new List<string>
{
Expand Down Expand Up @@ -50,17 +58,17 @@ public MainViewModel()
teams.ForEach(team => team.SetColor());
this.Teams = new ObservableCollection<Team>(teams);

var backlogCards = Enumerable.Range(0, 12).Select(_ => new Card()).ToList();
var backlogCards2 = Enumerable.Range(0, 12).Select(_ => new Card()).ToList();
var rnd = new Random();
var alpha = "qwertzuioplkjhgfdsayxcvbnm";
foreach (var c in backlogCards)
foreach (var c in backlogCards2)
{
c.Effort = rnd.Next(1, 10) ^ 2;
c.Id = rnd.Next(10000, 99999).ToString();
c.Title = alpha.OrderBy(c => Guid.NewGuid()).Take(rnd.Next(3, 5)).Select(c => c.ToString()).Aggregate((s, s1) => s + s1).ToUpper();
}

this.BacklogCards = new ObservableCollection<Card>(backlogCards);
this.BacklogCards = new ObservableCollection<Card>(backlogCards2);

var futureCards = Enumerable.Range(0, 21).Select(_ => new Card()).ToList();
foreach (var c in futureCards)
Expand All @@ -71,33 +79,9 @@ public MainViewModel()

this.FutureCards = new ObservableCollection<Card>(futureCards);

MessengerInstance.Register<DropMessage>(this,this.DropMessageHandling );
this.MessengerInstance.Register<DropMessage>(this,this.DropMessageHandling );
}

private void DropMessageHandling(DropMessage dropMessage)
{
if (dropMessage.Card.AssignedQuarter == null || !dropMessage.Card.AssignedQuarter.Any())
{
dropMessage.Card.AssignedQuarter = new List<string> {this.DisplayQuarters[dropMessage.DisplayQuarterIndex]};
}

dropMessage.Team.Cards.Add(dropMessage.Card);
dropMessage.Team.SetColor();

if (this.FutureCards.Contains(dropMessage.Card))
this.FutureCards.Remove(dropMessage.Card);

if (this.BacklogCards.Contains(dropMessage.Card))
this.BacklogCards.Remove(dropMessage.Card);

// TODO
this.Teams = new ObservableCollection<Team>(this.Teams);
base.RaisePropertyChanged(() => this.Teams);
}

public ObservableCollection<Card> FutureCards { get; set; }

public ObservableCollection<Card> BacklogCards { get; set; }

private (int quarter, int year) ConvertFromQuater(string input)
{
Expand Down Expand Up @@ -203,6 +187,22 @@ private static List<Card> CreateDebugData_Cards()

#region Properties

private ObservableCollection<Card> backlogCards;

public ObservableCollection<Card> BacklogCards
{
get => this.backlogCards;
set => base.Set(ref this.backlogCards,value );
}

private ObservableCollection<Card> futureCards;

public ObservableCollection<Card> FutureCards
{
get => this.futureCards;
set => base.Set(ref this.futureCards, value);
}

private List<string> displayQuarters;

public List<string> DisplayQuarters
Expand All @@ -211,7 +211,13 @@ public List<string> DisplayQuarters
set => this.Set(ref this.displayQuarters, value);
}

public ObservableCollection<Team> Teams { get; set; }
private ObservableCollection<Team> teams;

public ObservableCollection<Team> Teams
{
get => this.teams;
set => base.Set(ref this.teams, value);
}

private double teamLabelWidth;

Expand All @@ -229,11 +235,34 @@ public double TeamLabelWidth
public RelayCommand QuarterNextCommand { get; set; }
public RelayCommand QuarterBackCommand { get; set; }
public RelayCommand NewTeamCommand { get; set; }
public RelayCommand SaveCommand { get; set; }
public RelayCommand LoadCommand { get; set; }

#endregion

#region Commands Handling

private void DropMessageHandling(DropMessage dropMessage)
{
if (dropMessage.Card.AssignedQuarter == null || !dropMessage.Card.AssignedQuarter.Any())
{
dropMessage.Card.AssignedQuarter = new List<string> {this.DisplayQuarters[dropMessage.DisplayQuarterIndex]};
}

dropMessage.Team.Cards.Add(dropMessage.Card);
dropMessage.Team.SetColor();

if (this.FutureCards.Contains(dropMessage.Card))
this.FutureCards.Remove(dropMessage.Card);

if (this.BacklogCards.Contains(dropMessage.Card))
this.BacklogCards.Remove(dropMessage.Card);

// TODO
this.Teams = new ObservableCollection<Team>(this.Teams);
base.RaisePropertyChanged(() => this.Teams);
}

private void QuarterNextCommandHandling()
{
var newQuarterList = new List<string>();
Expand Down Expand Up @@ -265,6 +294,53 @@ private void NewTeamCommandHandling()
if (windows.Result != null) this.Teams.Add(windows.Result);
}

private void SaveCommandHandling()
{
var appFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Planungsboard");
if (!Directory.Exists(appFolder))
{
Directory.CreateDirectory(appFolder);
}

var save = new Storage
{
BacklogCards = this.BacklogCards,
FutureCards = this.FutureCards,
Teams = this.Teams,
};

var json = JsonSerializer.Serialize(save, new JsonSerializerOptions()
{
WriteIndented = true,
});

var path = Path.Combine(appFolder, $"{DateTime.Now:yyyy-MM-dd_HH.mm.ss}_Planungsboard.json");
File.WriteAllText(path, json);

Process.Start("notepad.exe",path);
}



public void LoadCommandHandling()
{
var openFileDialog = new OpenFileDialog();
var dialog = openFileDialog.ShowDialog();
if (dialog.HasValue && dialog.Value)
{
var json = File.ReadAllText(openFileDialog.FileName);
var r = JsonSerializer.Deserialize<Storage>(json);

this.BacklogCards = new ObservableCollection<Card>(r.BacklogCards);
this.FutureCards = new ObservableCollection<Card>(r.FutureCards);
foreach (var rTeam in r.Teams)
{
rTeam.SetColor();
}
this.Teams = new ObservableCollection<Team>(r.Teams);
}
}

private void QuarterBackCommandHandling()
{
var newQuarterList = new List<string>();
Expand Down Expand Up @@ -302,6 +378,8 @@ public class Card
public string Title { get; set; }
public int Effort { get; set; }
public List<string> AssignedQuarter { get; set; }

[JsonIgnore]
public string Color { get; set; } = "#FA58F4";
}

Expand All @@ -315,6 +393,7 @@ public Team()

public string Name { get; set; }
public List<Card> Cards { get; set; }

public string Color { get; set; }

public void SetColor()
Expand Down
11 changes: 11 additions & 0 deletions Planungsboard.Presentation/ViewModels/Storage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace Planungsboard.Presentation.ViewModels
{
public class Storage
{
public IEnumerable<Card> BacklogCards { get; set; }
public IEnumerable<Card> FutureCards { get; set; }
public IEnumerable<Team> Teams { get; set; }
}
}

0 comments on commit e3738e7

Please sign in to comment.