Skip to content

Commit

Permalink
1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
NureSyvukhaYaroslav committed Aug 20, 2022
1 parent d1c2552 commit a6badda
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 227 deletions.
8 changes: 1 addition & 7 deletions TaskManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32602.215
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TaskManager", "TaskManager\TaskManager.csproj", "{14255421-BF39-4644-A969-AAE8F96164F0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UI", "UI\UI.csproj", "{3EC2E4F3-92B9-45B0-A5B9-9F1324B377E5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TaskManager", "UI\TaskManager.csproj", "{3EC2E4F3-92B9-45B0-A5B9-9F1324B377E5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{14255421-BF39-4644-A969-AAE8F96164F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{14255421-BF39-4644-A969-AAE8F96164F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{14255421-BF39-4644-A969-AAE8F96164F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{14255421-BF39-4644-A969-AAE8F96164F0}.Release|Any CPU.Build.0 = Release|Any CPU
{3EC2E4F3-92B9-45B0-A5B9-9F1324B377E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3EC2E4F3-92B9-45B0-A5B9-9F1324B377E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EC2E4F3-92B9-45B0-A5B9-9F1324B377E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
18 changes: 18 additions & 0 deletions Tests/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
for (long q = 1; q < 100000000000; q += 5000000)
{
Console.WriteLine(FormatBytes(q));
Thread.Sleep(5);
}

string FormatBytes(long bytes)
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}

return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
}
10 changes: 10 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
35 changes: 17 additions & 18 deletions UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@
using System.IO;
using System.Windows;

namespace UI
namespace UI;

public partial class App : Application
{
public partial class App : Application
System.Windows.Forms.NotifyIcon nIcon = new System.Windows.Forms.NotifyIcon();
public App()
{
System.Windows.Forms.NotifyIcon nIcon = new System.Windows.Forms.NotifyIcon();
public App()
{
nIcon.Icon = new Icon(Directory.GetCurrentDirectory() + @"\ico.ico");
nIcon.Visible = true;
nIcon.Icon = new Icon(Directory.GetCurrentDirectory() + @"\ico.ico");
nIcon.Visible = true;

nIcon.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
nIcon.ContextMenuStrip.Items.Add("Close", null, (o, e) => { MainWindow.Close(); });
nIcon.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
nIcon.ContextMenuStrip.Items.Add("Close", null, (o, e) => { MainWindow.Close(); });

nIcon.Click += nIcon_Click;
}
nIcon.Click += nIcon_Click;
}

void nIcon_Click(object sender, EventArgs e)
void nIcon_Click(object sender, EventArgs e)
{
if (((dynamic)e).Button == System.Windows.Forms.MouseButtons.Left)
{
if (((dynamic)e).Button == System.Windows.Forms.MouseButtons.Left)
if (MainWindow.Visibility != Visibility.Collapsed)
{
if (MainWindow.Visibility != Visibility.Collapsed)
{
MainWindow.Visibility = Visibility.Visible;
MainWindow.WindowState = WindowState.Normal;
}
MainWindow.Visibility = Visibility.Visible;
MainWindow.WindowState = WindowState.Normal;
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="600">
Title="MainWindow" Height="450" Width="602">
<Grid RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Center" Width="600">
<ListView Margin="357,10,24,65" x:Name="CurrentTasks" SelectionChanged="CurrentTasks_SelectionChanged">
<ListView Margin="362,10,0,65" x:Name="CurrentTasks" SelectionChanged="CurrentTasks_SelectionChanged" HorizontalAlignment="Left" Width="219">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
Expand All @@ -15,10 +15,10 @@
</ListView.View>
</ListView>

<Button x:Name="Update" Content="Update" Margin="497,0,0,10" Click="Button_Click" HorizontalAlignment="Left" Width="53" Height="50" VerticalAlignment="Bottom"/>
<TextBox x:Name="Input" TextWrapping="Wrap" Width="135" HorizontalAlignment="Left" Margin="357,0,0,10" FontSize="16" TextChanged="Input_TextChanged" Height="50" VerticalAlignment="Bottom" TextAlignment="Center"/>
<Button x:Name="Update" Content="Update" Margin="502,0,45,10" Click="Button_Click" Height="50" VerticalAlignment="Bottom"/>
<TextBox x:Name="Input" TextWrapping="Wrap" Margin="362,0,0,10" FontSize="16" TextChanged="Input_TextChanged" Height="50" VerticalAlignment="Bottom" TextAlignment="Center" HorizontalAlignment="Left" Width="135"/>

<ListView x:Name="WatchTasks" Margin="10,10,248,10" SelectionChanged="WatchTasks_SelectionChanged">
<ListView x:Name="WatchTasks" Margin="15,10,0,10" SelectionChanged="WatchTasks_SelectionChanged" HorizontalAlignment="Left" Width="342">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}" />
Expand All @@ -28,6 +28,6 @@
</GridView>
</ListView.View>
</ListView>
<CheckBox x:Name="Check" Content="" HorizontalAlignment="Left" Margin="556,392,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="Check" Content="" Margin="561,0,0,27" Height="15" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="20"/>
</Grid>
</Window>
26 changes: 15 additions & 11 deletions UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using UI.Models;
using UI.Services;

namespace UI;

public partial class MainWindow : Window
{
TaskService taskService;
private readonly TaskService taskService;
private readonly ProcessService processService;

public MainWindow()
{
InitializeComponent();

taskService = new TaskService();
processService = new ProcessService();

CurrentTasks.ItemsSource = taskService.MapProcesses(Process.GetProcesses());
WatchTasks.ItemsSource = taskService.GetTasks();
UpdateList(false);
}

protected override void OnClosing(CancelEventArgs e)
{
UpdateList(false);

if (this.Visibility != Visibility.Hidden)
{
e.Cancel = true;
Expand All @@ -37,17 +41,16 @@ private void UpdateList(bool check)
{
if (check && Input.Text != "")
{
CurrentTasks.ItemsSource = taskService.MapProcesses(Process.GetProcesses())
CurrentTasks.ItemsSource = processService.GetProcess()
.Where(x => x.Name.ToLower().Contains(Input.Text.ToLower()));

WatchTasks.ItemsSource = taskService.GetTasks()
.Where(x => x.Name.ToLower().Contains(Input.Text.ToLower())).ToArray();
.Where(x => x.Name.ToLower().Contains(Input.Text.ToLower()));
}

else
{
CurrentTasks.ItemsSource = taskService.MapProcesses(Process.GetProcesses());

CurrentTasks.ItemsSource = processService.GetProcess();
WatchTasks.ItemsSource = taskService.GetTasks();
}

Expand All @@ -59,17 +62,18 @@ private void Button_Click(object sender, RoutedEventArgs e)
UpdateList(true);
}

private void Input_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
private void Input_TextChanged(object sender, TextChangedEventArgs e)
{
UpdateList(true);
}

private void WatchTasks_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0 && Check.IsChecked == true)
if (Check.IsChecked == true && e.AddedItems.Count > 0)
{
taskService.Delete(((Task)e.AddedItems[0]!).Name);
taskService.Delete(((TaskDTO)e.AddedItems[0]!).Name);
UpdateList(false);
Input.Text = "";
}
}

Expand Down
34 changes: 34 additions & 0 deletions UI/Models/ProcessDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace UI.Models;

public class ProcessDTO
{
public string Name { get; set; }
public long Memory { get; set; }
public string MemoryStr
{
get
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
long bytes = Memory;
double dblSByte = bytes;
int i;

for (i = 0;
i < Suffix.Length && bytes >= 1024;
i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}

return String.Format("{0:0.#} {1}", dblSByte, Suffix[i]);
}
}

public ProcessDTO(string name, long memory)
{
Name = name;
Memory = memory;
}
}
8 changes: 4 additions & 4 deletions UI/Task.cs → UI/Models/TaskDTO.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;

namespace UI;
namespace UI.Models;

public class Task
public class TaskDTO
{
public string Name { get; set; }

Expand All @@ -12,9 +12,9 @@ public class Task
public string CurrentTimeStr => $"{CurrentTime / 60}h {CurrentTime % 60}m";

public bool State { get; set; }
public string StateStr => (State) ? "Running" : "Stopped";
public string StateStr => State ? "Running" : "Stopped";

public Task(string name)
public TaskDTO(string name)
{
Name = name;
}
Expand Down
34 changes: 0 additions & 34 deletions UI/Proc.cs

This file was deleted.

26 changes: 26 additions & 0 deletions UI/Services/ProcessService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Diagnostics;
using System.Linq;
using UI.Models;

namespace UI.Services;

public class ProcessService
{
public ProcessDTO[] GetProcess()
{
return MapProcesses(Process.GetProcesses());
}

public ProcessDTO[] MapProcesses(Process[] p)
{
ProcessDTO[] res = new ProcessDTO[p.Length];

for (int i = 0; i < p.Length; i++)
{
var nP = new ProcessDTO(p[i].ProcessName, p[i].PrivateMemorySize64);
res[i] = nP;
}

return res.OrderByDescending(x => x.Memory).ToArray();
}
}
54 changes: 54 additions & 0 deletions UI/Services/Repository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UI.Models;

namespace TaskManager.Services;

public class Repository
{
private readonly string path = Directory.GetCurrentDirectory() + @"\save.txt";

public void Save(List<TaskDTO> tasks)
{
using (var sw = new StreamWriter(path))
{
sw.WriteLine(JsonConvert.SerializeObject(tasks, new JsonSerializerSettings() { Formatting = Formatting.Indented }));
}
}

public List<TaskDTO> OnInit()
{
List<TaskDTO> tasks;

try
{
using var sr = new StreamReader(path);
tasks = JsonConvert.DeserializeObject<List<TaskDTO>>(sr.ReadToEnd(), new JsonSerializerSettings() { Formatting = Formatting.Indented })!;

if (tasks.Count == 0)
{
throw new Exception();
}
}

catch
{
tasks = new List<TaskDTO>
{
new TaskDTO("Opera"),
new TaskDTO("Discord"),
new TaskDTO("Telegram")
};

using var sw = new StreamWriter(path);
sw.WriteLine(JsonConvert.SerializeObject(tasks, new JsonSerializerSettings() { Formatting = Formatting.Indented }));
}

return tasks;
}
}
Loading

0 comments on commit a6badda

Please sign in to comment.