bib_Talk_Chatprogramm/bibtalk/bib-talk/UI/MainWindow.xaml.cs
2024-07-02 22:40:43 +02:00

194 lines
5.3 KiB
C#

using bib_talk.Repository;
using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace bib_talk
{
public partial class MainWindow : Window
{
MainWindowDatabase database;
private DispatcherTimer _timer;
public string loggedinUser;
private bool isFirstLoad = true;
public MainWindow()
{
InitializeComponent();
InitializeTimer();
LoadData();
database = new MainWindowDatabase(this);
}
public MainWindow(string user)
{
loggedinUser = user;
InitializeComponent();
InitializeTimer();
LoadData();
database = new MainWindowDatabase(this);
database.LoadMessages();
}
private void InitializeTimer()
{
_timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(2)
};
_timer.Tick += (sender, e) => { database.LoadMessages(); database.LoadOnlineUsers(); };
_timer.Start();
}
public void LoadData()
{
loggedinuser.Text = "Logged in as: " + loggedinUser;
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
database.LogoutUser();
this.Close();
}
private void DraggableArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
{
DragMove();
}
}
private async void messageBOX_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
database.SendMessage();
}
}
private async void send_Click(object sender, RoutedEventArgs e)
{
database.SendMessage();
}
private void ChatFenster_Click(object sender, RoutedEventArgs e)
{
if (popup.IsOpen = true)
{
popup.Focus();
}
else
{
popup.IsOpen = true;
}
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 1 wurde geklickt!");
ClosePopup();
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 2 wurde geklickt!");
ClosePopup();
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button 3 wurde geklickt!");
ClosePopup();
}
private void Popup_LostFocus(object sender, RoutedEventArgs e)
{
ClosePopup();
}
private void ClosePopup()
{
popup.IsOpen = false;
}
private void onlineUsersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private async void uploadpbclick(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png, *.gif) | *.jpg; *.jpeg; *.png; *.gif";
if (openFileDialog.ShowDialog() == true)
{
string filePath = openFileDialog.FileName;
await UploadProfileImage(filePath);
}
}
private async Task UploadProfileImage(string filePath)
{
using (var client = new HttpClient())
{
var form = new MultipartFormDataContent();
var stream = File.OpenRead(filePath);
form.Add(new StreamContent(stream), "file", Path.GetFileName(filePath));
var response = await client.PostAsync($"http://daddypig.dns.navy:5114/api/users/upload-profile-image?username={loggedinUser}", form);
if (response.IsSuccessStatusCode)
{
MessageBox.Show("Profilbild erfolgreich hochgeladen");
}
else
{
MessageBox.Show("Fehler beim Hochladen des Profilbilds");
}
}
}
}
public class Message
{
public int Id { get; set; }
public string Username { get; set; }
public string Text { get; set; }
public DateTime Timestamp { get; set; }
}
public class MessageDto
{
public string Username { get; set; }
public string Message { get; set; }
public DateTime Timestamp { get; set; }
}
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Birthday { get; set; }
public string IsOnline { get; set; }
public string ProfileImagePath { get; set; }
}
}