Skip to content

Commit

Permalink
20231222204000
Browse files Browse the repository at this point in the history
  • Loading branch information
KalevaAalto committed Dec 22, 2023
2 parents be1b6c2 + 38cf628 commit 4c36f48
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 37 deletions.
3 changes: 2 additions & 1 deletion App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ sealed partial class App : Application
/// <summary>
/// 注入依赖
/// </summary>
public static IServiceProvider Services { get; private set; } = new ServiceCollection()
public static IServiceProvider services { get; private set; } = new ServiceCollection()
.AddSingleton<Services.LoginStatus>()
.AddSingleton<Services.StaticValues>()
.BuildServiceProvider();


Expand Down
4 changes: 2 additions & 2 deletions Models/Enums/MailType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace MicaApps.Upw.Mail.Models.Enums
/// <summary>
/// 邮件类型
/// </summary>
internal enum MailType
public enum MailType
{
Receiving, Sending
Receive, Send , Junk ,Deleted
}
}
10 changes: 8 additions & 2 deletions Models/Letter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ namespace MicaApps.Upw.Mail.Models
/// <summary>
/// 专门用于储存每封信件
/// </summary>
internal class Letter :MimeMessage
internal class Letter
{
public string title { get => this.Subject; }
public MimeMessage mimeMessage { get;private set; }
public string title { get => this.mimeMessage.Subject; }


public Letter(MimeMessage mimeMessage)
{
this.mimeMessage = mimeMessage;
}

}
}
2 changes: 1 addition & 1 deletion Models/UserData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal abstract class UserData



public abstract Task CollectLetter();
public abstract Task CollectLetter(ObservableCollection<Letter> letters,Enums.MailType mailType);
public abstract Task LoginIn();


Expand Down
42 changes: 37 additions & 5 deletions Models/UserDataByImap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using MailKit.Security;
using Windows.UI.Xaml;
using MailKit;
using MailKit.Search;
using System.Collections.ObjectModel;
using Windows.Media.Protection.PlayReady;

namespace MicaApps.Upw.Mail.Models
{
Expand Down Expand Up @@ -36,15 +39,44 @@ public override async Task LoginIn()
await this.imapInfo.ConnectAsync(this.username,this.authorizationCode);
}

public override async Task CollectLetter()
public override async Task CollectLetter(ObservableCollection<Letter> letters,Enums.MailType mailType)
{
var inbox = this.imapInfo.Inbox;
await inbox.OpenAsync(FolderAccess.ReadOnly);
for (int i = 0; i < inbox.Count; i++)




IList<UniqueId> ids = null;
switch (mailType)
{
case Enums.MailType.Receive:
await inbox.OpenAsync(FolderAccess.ReadOnly);
ids = await inbox.SearchAsync(SearchQuery.All);
break;
case Enums.MailType.Send:
inbox = this.imapInfo.GetFolder(SpecialFolder.Sent);
await inbox.OpenAsync(FolderAccess.ReadOnly);
ids = await inbox.SearchAsync(SearchQuery.All);
break;
case Enums.MailType.Deleted:
await inbox.OpenAsync(FolderAccess.ReadOnly);
ids = await inbox.SearchAsync(SearchQuery.Deleted);
break;
}



letters.Clear();

if(ids != null)
{
var message = await inbox.GetMessageAsync(i);
this.letters.Add(message as Letter);
foreach (var id in ids)
{
var message = await inbox.GetMessageAsync(id);
letters.Add(new Letter(message));
}
}

}


Expand Down
5 changes: 4 additions & 1 deletion Pages/HomePagel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
xmlns:local="using:MicaApps.Upw.Mail.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:enum="using:MicaApps.Upw.Mail.Models.Enums"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<NavigationView ItemInvoked="NavigationView_ItemInvoked" IsBackButtonVisible="Collapsed">
<NavigationView.MenuItems>
<NavigationViewItem Tag="receive">收件箱</NavigationViewItem>
<NavigationViewItem Tag="{x:Bind enum:MailType.Receive}">收件箱</NavigationViewItem>
<NavigationViewItem Tag="{x:Bind enum:MailType.Send}">发件箱</NavigationViewItem>
<NavigationViewItem Tag="{x:Bind enum:MailType.Deleted}">已删邮件</NavigationViewItem>
</NavigationView.MenuItems>
<Frame x:Name="frame_main"/>
</NavigationView>
Expand Down
11 changes: 2 additions & 9 deletions Pages/HomePagel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,8 @@ public HomePagel()
private void NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
// 获取点击的菜单项的 Tag
string tag = args.InvokedItemContainer.Tag.ToString();

// 根据 Tag 导航到相应的页面
switch (tag)
{
case @"receive":
this.frame_main.Navigate(typeof(MailPage),tag);
break;
}
Models.Enums.MailType tag = (Models.Enums.MailType)args.InvokedItemContainer.Tag;
this.frame_main.Navigate(typeof(MailPage), tag);
}
}
}
12 changes: 7 additions & 5 deletions Pages/Login/LoginByImapPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace MicaApps.Upw.Mail.Pages.Login
/// </summary>
public sealed partial class LoginByImapPage : Page
{
private readonly Services.StaticValues staticValues = App.Services.GetService<Services.StaticValues>();
private readonly Services.LoginStatus loginStatus = App.Services.GetService<Services.LoginStatus>();
private readonly Services.StaticValues staticValues = App.services.GetService<Services.StaticValues>();
private readonly Services.LoginStatus loginStatus = App.services.GetService<Services.LoginStatus>();

public LoginByImapPage()
{
Expand All @@ -35,14 +35,16 @@ public LoginByImapPage()

private void combox_mailhost_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Grid grid = new Grid();
var m = grid.Margin;
if (this.staticValues.imapInfos.ContainsKey(this.combox_mailhost.Text))
{
this.host = this.staticValues.imapInfos[this.combox_mailhost.Text].host;
}
}


private string username { get => this.textbox_username.Text + '@' + this.combox_mailhost.Text; }
private string password { get => this.textbox_password.Text; }
private string host { get=>this.textbox_host.Text; }
private string host { get => this.textbox_host.Text; set => this.textbox_host.Text = value; }
private int port { get => Convert.ToInt32(this.textbox_port.Text); }


Expand Down
11 changes: 5 additions & 6 deletions Pages/MailPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,24 @@
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView Grid.Row="0" Grid.Column="0" BorderThickness="2" Margin="10" x:Name="listview" ItemClick="listview_ItemClick">
<ListView Grid.Row="0" Grid.Column="0" BorderThickness="2" Margin="10" x:Name="listview" SelectionChanged="listview_SelectionChanged" ItemsSource="{x:Bind letters}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:Letter">
<Grid BorderThickness="2" Margin="2" Background="AliceBlue">
<TextBlock Text="{Binding title}" FontSize="25"/>
<Grid Margin="1">
<TextBlock Text="{Binding title}" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

<TextBlock Margin="10" x:Name="textblock_area" Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" Text="s"/>
<WebView Grid.Row="0" Grid.Column="1" Margin="10" x:Name="webview"/>


<TextBlock Grid.Row="1" x:Name="textblock_status" Text="Null" FontSize="30"/>


<!--<StackPanel>
Expand Down
48 changes: 43 additions & 5 deletions Pages/MailPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace MicaApps.Upw.Mail.Pages
/// </summary>
public sealed partial class MailPage : Page
{
private readonly Services.LoginStatus loginStatus = App.Services.GetService<Services.LoginStatus>();
private string tag;
private readonly Services.LoginStatus loginStatus = App.services.GetService<Services.LoginStatus>();
private Models.Enums.MailType tag;
private DispatcherTimer timer;


Expand All @@ -41,13 +41,13 @@ public MailPage()
this.timer.Interval = TimeSpan.FromSeconds(1); // 设置定时器间隔为1秒
this.timer.Tick += (sender, e) =>
{
this.textblock_status.Text = this.loginStatus.userData != null ? this.loginStatus.userData.letters.Count().ToString() : @"未连接";

};
}




private ObservableCollection<Models.Letter> letters = new ObservableCollection<Models.Letter>();


/// <summary>
Expand All @@ -60,8 +60,46 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
if (e.Parameter != null)
{
// 处理传递过来的参数
this.tag = (string)e.Parameter;
this.tag = (Models.Enums.MailType)e.Parameter;
// 在这里添加你的逻辑
if (this.loginStatus.IsLogin)
{
await this.loginStatus.userData.CollectLetter(this.letters,this.tag);
}

}

timer.Start();
}


protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);

// 停止定时器,释放资源
timer.Stop();
}






private void listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// 获取点击的项的数据
Models.Letter clickedItem = e.AddedItems.First() as Models.Letter;

// 处理点击事件,例如显示一个消息框

if (clickedItem != null && clickedItem.mimeMessage.HtmlBody !=null)
{
this.webview.NavigateToString(clickedItem.mimeMessage.HtmlBody);
}
else
{
this.webview.NavigateToString(@"Error");
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Services/LoginStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ internal class LoginStatus
public bool IsLogin { get=>this.userData != null && this.userData.IsLogin;}






}
}

0 comments on commit 4c36f48

Please sign in to comment.