Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
added sending message
Browse files Browse the repository at this point in the history
  • Loading branch information
cherryymerryy committed Jul 30, 2024
1 parent bbec8a1 commit e3858b1
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 22 deletions.
3 changes: 0 additions & 3 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
</configSections>
<userSettings>
<CherryMerryGram.cherrymerrygram_config>
<setting name="appId" serializeAs="String">
<value />
</setting>
</CherryMerryGram.cherrymerrygram_config>
</userSettings>
</configuration>
12 changes: 12 additions & 0 deletions CherryMerryGram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
<ItemGroup>
<None Remove="Views\AccountView.xaml" />
<None Remove="Views\ChatsView.xaml" />
<None Remove="Views\Chats\Chat.xaml" />
<None Remove="Views\Chats\ChatEntry.xaml" />
<None Remove="Views\Chats\ChatMessage.xaml" />
<None Remove="Views\HelpView.xaml" />
<None Remove="Views\LoginView.xaml" />
<None Remove="Views\SettingsView.xaml" />
Expand Down Expand Up @@ -52,6 +54,16 @@
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<Page Update="Views\Chats\ChatMessage.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Page Update="Views\Chats\Chat.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Page Update="Views\LoginView.xaml">
<Generator>MSBuild:Compile</Generator>
Expand Down
6 changes: 3 additions & 3 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ private static async Task ProcessUpdates(TdApi.Update update)
var filesLocation = Path.Combine(AppContext.BaseDirectory, "db");
await _client.ExecuteAsync(new TdApi.SetTdlibParameters
{
ApiId = _config.ApiId,
ApiHash = _config.ApiHash,
ApiId = Config.Config.ApiId,
ApiHash = Config.Config.ApiHash,
DeviceModel = "PC",
SystemLanguageCode = "en",
ApplicationVersion = _config.ApplicationVersion,
ApplicationVersion = Config.Config.ApplicationVersion,
DatabaseDirectory = filesLocation,
FilesDirectory = filesLocation,
});
Expand Down
4 changes: 0 additions & 4 deletions Views/AccountView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace CherryMerryGram.Views
public sealed partial class AccountView : Page
{
private static readonly TdClient _client = MainWindow._client;
private static MainWindow _app;

private string _displayName;
private string _username;
Expand All @@ -17,8 +16,6 @@ public sealed partial class AccountView : Page
public AccountView()
{
this.InitializeComponent();

//_app = new MainWindow();

InitializeAllVariables();
}
Expand All @@ -42,7 +39,6 @@ private async void InitializeAllVariables()
private async void Button_LogOut_OnClick(object sender, RoutedEventArgs e)
{
await _client.ExecuteAsync(new TdApi.LogOut());
_app.UpdateWindow();
}
}
}
23 changes: 23 additions & 0 deletions Views/Chats/Chat.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<Page
x:Class="CherryMerryGram.Views.Chats.Chat"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CherryMerryGram.Views.Chats"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel>
<StackPanel x:Name="TopBar" Orientation="Horizontal" VerticalAlignment="Top">
<TextBlock x:Name="ChatTitle" />
</StackPanel>
<ListView x:Name="MessagesList" />
<StackPanel x:Name="UserActionsPanel" Orientation="Horizontal" VerticalAlignment="Bottom">
<TextBox x:Name="UserMessageInput" PlaceholderText="Enter your message"/>
<Button x:Name="Stickers" Content="Stickers" Width="70" MaxHeight="25" MaxWidth="25" />
<Button x:Name="SendMessage" Content="Send" Width="70" MaxHeight="25" MaxWidth="25" Click="SendMessage_OnClick" />
</StackPanel>
</StackPanel>
</Page>
65 changes: 65 additions & 0 deletions Views/Chats/Chat.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.UI.Xaml.Controls;
using System.Collections.Generic;
using TdLib;
using Microsoft.UI.Xaml;

namespace CherryMerryGram.Views.Chats
{
public sealed partial class Chat : Page
{
private static TdClient _client = MainWindow._client;
private static long ChatId;

public Chat()
{
this.InitializeComponent();
}

public async void UpdateChat(long chatId, string Title)
{
ChatId = chatId;
var chat = _client.GetChatAsync(chatId: chatId);
ChatTitle.Text = chat.Result.Title;

var messages = GetMessages();

await foreach (var message in messages)
{
var messageEntry = new ChatMessage();
messageEntry.UpdateMessage(message: message);
MessagesList.Items.Add(messageEntry);
}
}

private static async IAsyncEnumerable<TdApi.Messages> GetMessages()
{
var messages = await _client.ExecuteAsync(new TdApi.GetChatHistory
{
ChatId = ChatId,
FromMessageId = 0,
Offset = 0,
Limit = 100
});

foreach (var message in messages.Messages_)
{
yield return message;
}
}

private async void SendMessage_OnClick(object sender, RoutedEventArgs e)
{
await _client.ExecuteAsync(new TdApi.SendMessage
{
ChatId = ChatId,
InputMessageContent = new TdApi.InputMessageContent.InputMessageText
{
Text = new TdApi.FormattedText
{
Text = UserMessageInput.Text
}
}
});
}
}
}
10 changes: 6 additions & 4 deletions Views/Chats/ChatEntry.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel>
<TextBlock x:Name="textBlock_ChatName" Text="ChatName" />
<TextBlock x:Name="textBlock_ChatId" Text="ChatId" />
</StackPanel>
<Button Width="250px" MaxWidth="490px" MaxHeight="50px" Click="ButtonBase_OnClick">
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Center">
<TextBlock x:Name="textBlock_Chat_NameAndId" Text="ChatName (chatId)" />
<TextBlock x:Name="textBlock_Chat_LastMessage" Text="LastMessage" />
</StackPanel>
</Button>
</Page>
30 changes: 27 additions & 3 deletions Views/Chats/ChatEntry.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using TdLib;
using WinRT;

namespace CherryMerryGram.Views.Chats
{
public sealed partial class ChatEntry : Page
{
public ListView ChatPage;
private static Chat _chat;

private long ChatId;
private string ChatTitle;

public ChatEntry()
{
this.InitializeComponent();
}

public void UpdateChat(int chatId, string chatName)
public void UpdateChat(long chatId, string chatName, TdApi.Message chatLastMessage )
{
ChatId = chatId;
ChatTitle = chatName;
textBlock_Chat_NameAndId.Text = $"{chatName} ({chatId})";
textBlock_Chat_LastMessage.Text = chatLastMessage.Content.ToString();
}

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
textBlock_ChatId.Text = chatId.ToString();
textBlock_ChatName.Text = chatName;
if (ChatPage != null && _chat != null)
{
ChatPage.Items.Remove(_chat);
_chat = null;
}

_chat = new Chat();
ChatPage.Items.Add(_chat);
_chat.UpdateChat(ChatId, ChatTitle);
}
}
}
15 changes: 15 additions & 0 deletions Views/Chats/ChatMessage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Page
x:Class="CherryMerryGram.Views.Chats.ChatMessage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CherryMerryGram.Views.Chats"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel Orientation="Vertical">
<TextBlock x:Name="Username" Text="@username" />
</StackPanel>
</Page>
18 changes: 18 additions & 0 deletions Views/Chats/ChatMessage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.UI.Xaml.Controls;
using TdLib;

namespace CherryMerryGram.Views.Chats
{
public sealed partial class ChatMessage : Page
{
public ChatMessage()
{
this.InitializeComponent();
}

public void UpdateMessage(TdApi.Message message)
{
Username.Text = message.AuthorSignature;
}
}
}
4 changes: 2 additions & 2 deletions Views/ChatsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel Orientation="Horizontal">
<ListView x:Name="ChatList" Width="250px">
</ListView>
<ListView x:Name="ChatsList" Width="500px" />
<ListView x:Name="Chat" />
</StackPanel>
</Page>
6 changes: 3 additions & 3 deletions Views/ChatsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ private async void GenerateChatEntries()
await foreach (var chat in chats)
{
var chatEntry = new ChatEntry();
int chatId = unchecked((int)chat.Id);
chatEntry.UpdateChat(chatId, chat.Title);
ChatList.Items.Add(chatEntry);
chatEntry.UpdateChat(chat.Id, chat.Title, chat.LastMessage);
chatEntry.ChatPage = Chat;
ChatsList.Items.Add(chatEntry);
}
}

Expand Down

0 comments on commit e3858b1

Please sign in to comment.