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

Commit a0e1efb

Browse files
committed
added last message in chat and version in settings
1 parent 0b79c53 commit a0e1efb

File tree

9 files changed

+77
-70
lines changed

9 files changed

+77
-70
lines changed

Assets/TestImages/CatAvatar.jpg

33.3 KB
Loading

Views/Chats/Chat.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
mc:Ignorable="d"
1010
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
1111

12-
<StackPanel>
12+
<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
1313
<StackPanel x:Name="TopBar" Orientation="Horizontal" VerticalAlignment="Top">
14+
<Button x:Name="Back" Content="Back" />
1415
<TextBlock x:Name="ChatTitle" />
1516
</StackPanel>
1617
<ListView x:Name="MessagesList" />

Views/Chats/Chat.xaml.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,6 @@ public async void UpdateChat(long chatId, string Title)
2020
ChatId = chatId;
2121
var chat = _client.GetChatAsync(chatId: chatId);
2222
ChatTitle.Text = chat.Result.Title;
23-
24-
var messages = GetMessages();
25-
26-
await foreach (var message in messages)
27-
{
28-
var messageEntry = new ChatMessage();
29-
//messageEntry.UpdateMessage(message: message);
30-
MessagesList.Items.Add(messageEntry);
31-
}
32-
}
33-
34-
private static async IAsyncEnumerable<TdApi.Messages> GetMessages()
35-
{
36-
var messages = await _client.ExecuteAsync(new TdApi.GetChatHistory
37-
{
38-
ChatId = ChatId,
39-
FromMessageId = 0,
40-
Offset = 0,
41-
Limit = 100
42-
});
43-
44-
foreach (var message in messages.Messages_)
45-
{
46-
yield return messages;
47-
}
4823
}
4924

5025
private async void SendMessage_OnClick(object sender, RoutedEventArgs e)

Views/Chats/ChatEntry.xaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
mc:Ignorable="d"
1010
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
1111

12-
<Button Width="250px" MaxWidth="490px" MaxHeight="50px" Click="ButtonBase_OnClick">
13-
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Center">
14-
<TextBlock x:Name="textBlock_Chat_NameAndId" Text="ChatName (chatId)" />
15-
<TextBlock x:Name="textBlock_Chat_LastMessage" Text="LastMessage" />
12+
<Button Width="490px" MaxWidth="490px" MaxHeight="75px" Click="ButtonBase_OnClick" HorizontalContentAlignment="Left" Margin="5, 5, 5, 5">
13+
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal">
14+
<Image x:Name="ChatEntry_ProfilePicture" Source="../../Assets/TestImages/CatAvatar.jpg" Width="32px" Height="32px" Margin="5" />
15+
<StackPanel Orientation="Vertical">
16+
<TextBlock x:Name="textBlock_Chat_NameAndId" Text="ChatName (chatId)" FontSize="14"/>
17+
<TextBlock x:Name="textBlock_Chat_LastMessage" Text="LastMessage" FontSize="10" />
18+
</StackPanel>
1619
</StackPanel>
1720
</Button>
1821
</Page>

Views/Chats/ChatEntry.xaml.cs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,68 @@
11
using Microsoft.UI.Xaml;
22
using Microsoft.UI.Xaml.Controls;
3+
using Microsoft.UI.Xaml.Media.Imaging;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
37
using TdLib;
48
using WinRT;
9+
using CherryMerryGram;
510

611
namespace CherryMerryGram.Views.Chats
712
{
813
public sealed partial class ChatEntry : Page
914
{
10-
public ListView ChatPage;
11-
private static Chat _chat;
15+
public StackPanel ChatPage;
16+
private static Chat _chatWidget;
1217

13-
private long ChatId;
14-
private string ChatTitle;
18+
private long _chatId;
19+
private string _chatTitle;
20+
private static TdApi.Client _client = MainWindow._client;
1521

1622
public ChatEntry()
1723
{
1824
this.InitializeComponent();
1925
}
2026

21-
public void UpdateChat(long chatId, string chatName, TdApi.Message chatLastMessage )
27+
public void UpdateChat(TdApi.Chat chat)
2228
{
23-
ChatId = chatId;
24-
ChatTitle = chatName;
25-
textBlock_Chat_NameAndId.Text = $"{chatName} ({chatId})";
26-
textBlock_Chat_LastMessage.Text = chatLastMessage.Content.ToString();
29+
_chatId = chat.Id;
30+
_chatTitle = chat.Title;
31+
32+
textBlock_Chat_NameAndId.Text = $"{chat.Title} ({chat.Id})";
33+
34+
if (chat.LastMessage.Content is not TdApi.MessageContent.MessageText messageText) return;
35+
var text = messageText.Text;
36+
textBlock_Chat_LastMessage.Text = text.Text;
2737
}
2838

2939
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
3040
{
31-
if (ChatPage != null && _chat != null)
41+
if (ChatPage != null && _chatWidget != null)
3242
{
33-
ChatPage.Items.Remove(_chat);
34-
_chat = null;
43+
ChatPage.Children.Remove(_chatWidget);
44+
_chatWidget = null;
3545
}
3646

37-
_chat = new Chat();
38-
ChatPage.Items.Add(_chat);
39-
_chat.UpdateChat(ChatId, ChatTitle);
47+
_chatWidget = new Chat();
48+
ChatPage.Children.Add(_chatWidget);
49+
_chatWidget.UpdateChat(_chatId, _chatTitle);
50+
}
51+
52+
private static IEnumerable<string> GetLastMessage(TdApi.Chat chat)
53+
{
54+
switch (chat.LastMessage.DataType)
55+
{
56+
case "messageText":
57+
yield return chat.LastMessage.Content.ToString();
58+
break;
59+
case "messagePhoto":
60+
yield return chat.LastMessage.Content.DataType[4].ToString();
61+
break;
62+
case "messageSticker":
63+
yield return chat.LastMessage.Content.DataType[5].ToString();
64+
break;
65+
}
4066
}
4167
}
4268
}

Views/ChatsView.xaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
mc:Ignorable="d"
1010
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
1111

12-
<StackPanel Orientation="Horizontal">
13-
<ListView x:Name="ChatsList" Width="500px" />
14-
<ListView x:Name="Chat" />
12+
<StackPanel Orientation="Horizontal" Padding="0, 50, 0, 0">
13+
<ScrollView>
14+
<StackPanel x:Name="ChatsList" Width="500px" />
15+
</ScrollView>
16+
<ScrollView>
17+
<StackPanel x:Name="Chat" />
18+
</ScrollView>
1519
</StackPanel>
1620
</Page>

Views/ChatsView.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public ChatsView()
2020

2121
private async void GenerateChatEntries()
2222
{
23-
var chats = GetChats(2000);
23+
var chats = GetChats(4000);
2424

2525
await foreach (var chat in chats)
2626
{
2727
var chatEntry = new ChatEntry();
28-
chatEntry.UpdateChat(chat.Id, chat.Title, chat.LastMessage);
28+
chatEntry.UpdateChat(chat);
2929
chatEntry.ChatPage = Chat;
30-
ChatsList.Items.Add(chatEntry);
30+
ChatsList.Children.Add(chatEntry);
3131
}
3232
}
3333

Views/SettingsView.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<HyperlinkButton Content="Channel" NavigateUri="https://t.me/cherrymerrygram" Width="100px" />
1515
<HyperlinkButton Content="Chat" NavigateUri="https://t.me/cherrymerrygramchat" Width="100px" />
1616
<HyperlinkButton Content="Source code" NavigateUri="https://github.com/cherryymerryy/CherryMerryGram" Width="100px" />
17+
<HyperlinkButton x:Name="Version" Content="Version: " NavigateUri="" Width="100px" />
1718
</StackPanel>
1819
</StackPanel>
1920
</Page>

Views/SettingsView.xaml.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
using Microsoft.UI.Xaml;
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
23
using Microsoft.UI.Xaml.Controls;
3-
using Microsoft.UI.Xaml.Controls.Primitives;
4-
using Microsoft.UI.Xaml.Data;
5-
using Microsoft.UI.Xaml.Input;
6-
using Microsoft.UI.Xaml.Media;
7-
using Microsoft.UI.Xaml.Navigation;
8-
using System;
9-
using System.Collections.Generic;
10-
using System.IO;
11-
using System.Linq;
12-
using System.Runtime.InteropServices.WindowsRuntime;
13-
using Windows.Foundation;
14-
using Windows.Foundation.Collections;
15-
16-
// To learn more about WinUI, the WinUI project structure,
17-
// and more about our project templates, see: http://aka.ms/winui-project-info.
4+
using Newtonsoft.Json.Linq;
185

196
namespace CherryMerryGram.Views
207
{
21-
/// <summary>
22-
/// An empty page that can be used on its own or navigated to within a Frame.
23-
/// </summary>
248
public sealed partial class SettingsView : Page
259
{
2610
public SettingsView()
2711
{
2812
this.InitializeComponent();
13+
14+
GetLatestVersion();
15+
}
16+
17+
private async Task GetLatestVersion()
18+
{
19+
using var client = new HttpClient();
20+
const string requestUri = "https://api.github.com/repos/cherryymerryy/CherryMerryGram/releases/latest";
21+
var response = await client.GetAsync(requestUri);
22+
var content = await response.Content.ReadAsStringAsync();
23+
dynamic json = JObject.Parse(content);
24+
Version.Content = $"Version: {json.tag_name}";
25+
Version.NavigateUri = json.html_url;
2926
}
3027
}
3128
}

0 commit comments

Comments
 (0)