private async void btnDeleteMes_Click(object sender, RoutedEventArgs e) { try { SQLiteAsyncConnection conn = new SQLiteAsyncConnection("DBMail"); var result = await conn.QueryAsync <MesHeader>("select * from mesheader"); foreach (MesHeader item in result) { foreach (var itemid in listIdMes) { if (itemid.Contains(item.idmes)) { MesHeader mes = new MesHeader(); mes.idmes = item.idmes; mes.content = item.content; mes.txt1 = item.txt1; mes.txt2 = item.txt2; mes.txt3 = item.txt3; mes.txt4 = item.txt4; await conn.DeleteAsync(mes); } } } gridMes.Children.Clear(); reloadwithPage(i); } catch (Exception) { } }
//Search public async void searchMail(string keyword) { SQLiteAsyncConnection conn = new SQLiteAsyncConnection("DBMail"); List <MesHeader> listMes = new List <MesHeader>(); List <MesImportant> listImportant = new List <MesImportant>(); //list important var resultimportant = await conn.QueryAsync <MesImportant>("select * from mesimportant"); foreach (var item in resultimportant) { MesImportant mesimportant = new MesImportant(); mesimportant.idimportant = item.idimportant; listImportant.Add(mesimportant); } var result = await conn.QueryAsync <MesHeader>("select * from mesheader"); for (int i = 0; i < result.Count; i++) { MesHeader mes = new MesHeader(); mes.idmes = result[i].idmes; mes.txt1 = result[i].txt1; mes.txt2 = result[i].txt2; mes.txt3 = result[i].txt3; mes.txt4 = result[i].txt4; mes.content = result[i].content; listMes.Add(mes); } List <MesHeader> listSearch = listMes.FindAll(x => x.txt1.Contains(keyword) || x.txt2.Contains(keyword) || x.txt3.Contains(keyword)); for (int i = 0; i < listSearch.Count; i++) { MessageHeader userControlMessage = new MessageHeader(); userControlMessage.Name = "Mes" + i; if (i % 2 == 0) { userControlMessage.backgroundColorMes = ConvertColor(242, 244, 246); } else { userControlMessage.backgroundColorMes = ConvertColor(249, 250, 249); } //important foreach (var item in listImportant) { if (item.idimportant == listMes[i].idmes) { userControlMessage.tagSecondPro = Visibility.Visible; userControlMessage.tagFirstPro = Visibility.Collapsed; } } userControlMessage.txtHeader1 = listSearch[i].txt1; userControlMessage.txtHeader2 = listSearch[i].txt2; userControlMessage.txtHeader3 = "-" + listSearch[i].txt3; string[] str = listSearch[i].txt4.Replace("-", "+").Split('+'); userControlMessage.txtHeader4 = str[0]; userControlMessage.txtId = listSearch[i].idmes; userControlMessage.txtContent = listSearch[i].content; userControlMessage.txtHeader1Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.txtHeader2Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.txtHeader3Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.txtHeader4Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.Margin = new Thickness(0, 0, 0, 5); gridMes.Children.Add(userControlMessage); userControlMessage.btnDetail += UserControlMessage_btnDetail; userControlMessage.btnCheckedClick += UserControlMessage_btnCheckedClick; } }
//Data left and right public async void leftRightMesDB(int start) { try { SQLiteAsyncConnection conn = new SQLiteAsyncConnection("DBMail"); List <MesHeader> listMes = new List <MesHeader>(); List <MesUnread> listMesUnread = new List <MesUnread>(); List <MesImportant> listImportant = new List <MesImportant>(); //list important var resultimportant = await conn.QueryAsync <MesImportant>("select * from mesimportant"); foreach (var item in resultimportant) { MesImportant mesimportant = new MesImportant(); mesimportant.idimportant = item.idimportant; listImportant.Add(mesimportant); } //list message all var result = await conn.QueryAsync <MesHeader>("select * from mesheader"); for (int i = 0; i < result.Count; i++) { MesHeader mes = new MesHeader(); mes.idmes = result[i].idmes; mes.txt1 = result[i].txt1; mes.txt2 = result[i].txt2; mes.txt3 = result[i].txt3; mes.txt4 = result[i].txt4; mes.content = result[i].content; listMes.Add(mes); } //list message unread var resultunread = await conn.QueryAsync <MesUnread>("select * from mesunread"); foreach (var item in resultunread) { MesUnread mesunread = new MesUnread(); mesunread.idmes = item.idmes; mesunread.txt1 = item.txt1; mesunread.txt2 = item.txt2; mesunread.txt3 = item.txt3; mesunread.txt4 = item.txt4; mesunread.content = item.content; listMesUnread.Add(mesunread); } for (int i = start; i < listMes.Count; i++) { MessageHeader userControlMessage = new MessageHeader(); userControlMessage.Name = "Mes" + i; if (i % 2 == 0) { userControlMessage.backgroundColorMes = ConvertColor(242, 244, 246); } else { userControlMessage.backgroundColorMes = ConvertColor(249, 250, 249); } //important foreach (var item in listImportant) { if (item.idimportant == listMes[i].idmes) { userControlMessage.tagSecondPro = Visibility.Visible; userControlMessage.tagFirstPro = Visibility.Collapsed; } } //id all vs id unread foreach (MesUnread mes in listMesUnread) { if (mes.idmes == listMes[i].idmes) { userControlMessage.backgroundColorMes = ConvertColor(72, 137, 181); userControlMessage.opacityMes = 0.6; } } userControlMessage.txtHeader1 = listMes[i].txt1; userControlMessage.txtHeader2 = listMes[i].txt2; userControlMessage.txtHeader3 = "-" + listMes[i].txt3; string[] str = listMes[i].txt4.Replace("-", "+").Split('+'); userControlMessage.txtHeader4 = str[0]; userControlMessage.txtId = listMes[i].idmes; userControlMessage.txtContent = listMes[i].content; userControlMessage.txtHeader1Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.txtHeader2Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.txtHeader3Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.txtHeader4Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.Margin = new Thickness(0, 0, 0, 5); gridMes.Children.Add(userControlMessage); userControlMessage.btnDetail += UserControlMessage_btnDetail; userControlMessage.btnCheckedClick += UserControlMessage_btnCheckedClick; } } catch (Exception) { } }
//Get data from DB public async void dataMesInDB() { try { SQLiteAsyncConnection conn = new SQLiteAsyncConnection("DBMail"); List <MesHeader> listMes = new List <MesHeader>(); List <MesUnread> listMesUnread = new List <MesUnread>(); List <MesImportant> listImportant = new List <MesImportant>(); //list important var resultimportant = await conn.QueryAsync <MesImportant>("select * from mesimportant"); foreach (var item in resultimportant) { MesImportant mesimportant = new MesImportant(); mesimportant.idimportant = item.idimportant; listImportant.Add(mesimportant); } //list all message var result = await conn.QueryAsync <MesHeader>("select * from mesheader"); //Progress Bar await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => proBar3.Maximum = result.Count); for (int i = 0; i < result.Count; i++) { MesHeader mes = new MesHeader(); mes.idmes = result[i].idmes; mes.txt1 = result[i].txt1; mes.txt2 = result[i].txt2; mes.txt3 = result[i].txt3; mes.txt4 = result[i].txt4; mes.content = result[i].content; listMes.Add(mes); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => proBar3.Value = i); } //list unread var resultunread = await conn.QueryAsync <MesUnread>("select * from mesunread"); foreach (var item in resultunread) { MesUnread mesunread = new MesUnread(); mesunread.idmes = item.idmes; mesunread.txt1 = item.txt1; mesunread.txt2 = item.txt2; mesunread.txt3 = item.txt3; mesunread.txt4 = item.txt4; mesunread.content = item.content; listMesUnread.Add(mesunread); } if (listMes.Count <= 26) { txtTotalMes.Text = string.Format("1-{0} of {1}", listMes.Count, listMes.Count); btnRightMes.IsEnabled = false; btnLeftMes.IsEnabled = false; } else { txtTotalMes.Text = string.Format("1-{0} of {1}", 26, listMes.Count); btnRightMes.Background = ConvertColor(61, 98, 128); btnLeftMes.IsEnabled = false; } await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => proBar4.Maximum = 26); for (int i = 0; i < 26; i++) { MessageHeader userControlMessage = new MessageHeader(); userControlMessage.Name = "Mes" + i; if (i % 2 == 0) { userControlMessage.backgroundColorMes = ConvertColor(242, 244, 246); } else { userControlMessage.backgroundColorMes = ConvertColor(249, 250, 249); } //important foreach (var item in listImportant) { if (item.idimportant == listMes[i].idmes) { userControlMessage.tagSecondPro = Visibility.Visible; userControlMessage.tagFirstPro = Visibility.Collapsed; } } //id all vs id unread foreach (MesUnread mes in listMesUnread) { if (mes.idmes == listMes[i].idmes) { userControlMessage.backgroundColorMes = ConvertColor(72, 137, 181); userControlMessage.opacityMes = 0.6; } } userControlMessage.txtHeader1 = listMes[i].txt1; userControlMessage.txtHeader2 = listMes[i].txt2; userControlMessage.txtHeader3 = "-" + listMes[i].txt3; string[] str = listMes[i].txt4.Replace("-", "+").Split('+'); userControlMessage.txtHeader4 = str[0]; userControlMessage.txtId = listMes[i].idmes; userControlMessage.txtContent = listMes[i].content; userControlMessage.txtHeader1Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.txtHeader2Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.txtHeader3Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.txtHeader4Font = new FontFamily("RobotoFont/Roboto-Thin.ttf#Roboto"); userControlMessage.Margin = new Thickness(0, 0, 0, 5); gridMes.Children.Add(userControlMessage); userControlMessage.btnDetail += UserControlMessage_btnDetail; userControlMessage.btnCheckedClick += UserControlMessage_btnCheckedClick; await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => proBar4.Value = i); } } catch (Exception) { } finally { //ProgressBar proBar3.Visibility = Visibility.Collapsed; proBar4.Visibility = Visibility.Collapsed; } }
private async void btnReloadMes_Click(object sender, RoutedEventArgs e) { try { UserCredential credential = await serviceGmail.getCredential(); var getProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile(); if (credential != null && getProfile != null) { SQLiteAsyncConnection conn = new SQLiteAsyncConnection("DBMail"); await conn.DropTableAsync <MesHeader>(); await conn.DropTableAsync <MesUnread>(); await conn.DropTableAsync <MesImportant>(); await conn.CreateTablesAsync <MesHeader, MesUnread, MesImportant>(); List <MesHeader> lstMes = serviceGmail.getAllMail(credential); List <MesUnread> lstMesUnread = serviceGmail.getMesUnread(credential); List <MesImportant> lstImportant = serviceGmail.getMesImportant(credential); //bike showbike.Visibility = Visibility.Visible; showStoryboard.Begin(); if (lstImportant != null) { foreach (MesImportant item in lstImportant) { MesImportant mesimportant = new MesImportant(); mesimportant.idimportant = item.idimportant; await conn.InsertAsync(mesimportant); } } if (lstMes != null) { //Progress Bar await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => proBar1.Maximum = lstMes.Count); for (int i = 0; i < lstMes.Count; i++) { MesHeader mes = new MesHeader(); mes.idmes = lstMes[i].idmes; mes.txt1 = lstMes[i].txt1; mes.txt2 = lstMes[i].txt2; mes.txt3 = lstMes[i].txt3; mes.txt4 = lstMes[i].txt4; mes.content = lstMes[i].content; await conn.InsertAsync(mes); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => proBar1.Value = i); } } if (lstMesUnread != null) { //unread await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => proBar2.Maximum = lstMesUnread.Count); for (int i = 0; i < lstMesUnread.Count; i++) { MesUnread mesunread = new MesUnread(); mesunread.idmes = lstMesUnread[i].idmes; mesunread.txt1 = lstMesUnread[i].txt1; mesunread.txt2 = lstMesUnread[i].txt2; mesunread.txt3 = lstMesUnread[i].txt3; mesunread.txt4 = lstMesUnread[i].txt4; mesunread.content = lstMesUnread[i].content; await conn.InsertAsync(mesunread); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => proBar2.Value = i); } } } } catch (Exception) { } finally { gridMes.Children.Clear(); dataMesInDB(); //ProgressBar proBar1.Visibility = Visibility.Collapsed; proBar2.Visibility = Visibility.Collapsed; showStoryboard.Stop(); showbike.Visibility = Visibility.Collapsed; btnRightMes.IsEnabled = true; } }
public List<MesHeader> getAllMail(UserCredential credential) { // Create Gmail API service. var service = new GmailService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); //get Message id and threadid List<Message> resultMes = new List<Message>(); UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List("me"); request.LabelIds = "INBOX"; ListMessagesResponse response = request.Execute(); resultMes.AddRange(response.Messages); request.PageToken = response.NextPageToken; //get Detail Message List<Message> resultDetail = new List<Message>(); foreach (var item in resultMes) { Message mes = new Message(); Message mesd = service.Users.Messages.Get("me", item.Id).Execute(); mes.Snippet = mesd.Snippet; mes.Id = mesd.Id; mes.LabelIds = mesd.LabelIds; mes.Payload = mesd.Payload; resultDetail.Add(mes); } List<MesHeader> mesLHeader = new List<MesHeader>(); foreach (Message itemmes in resultDetail) { MesHeader mes = new MesHeader(); mes.txt3 = itemmes.Snippet; foreach (var item in itemmes.Payload.Headers) { if (item.Name == "From") { mes.txt1 = item.Value; } else if (item.Name == "Subject") { mes.txt2 = item.Value; } else if (item.Name == "Date") { mes.txt4 = item.Value; } } //get content if (itemmes.Payload.Parts != null) { foreach (var item in itemmes.Payload.Parts) { if (item.MimeType == "text/html") { mes.content = item.Body.Data; } else if (item.MimeType == "multipart/related" || item.MimeType == "multipart/alternative") { foreach (var itemm in item.Parts) { if (itemm.MimeType == "text/html") { mes.content = itemm.Body.Data; } else if (itemm.MimeType == "multipart/related" || itemm.MimeType == "multipart/alternative") { foreach (var itemmini in itemm.Parts) { if (itemmini.MimeType == "text/html") { mes.content = itemmini.Body.Data; } } } } } } } else { mes.content = itemmes.Payload.Body.Data; } //get content when text/html null if (mes.content == null) { if (itemmes.Payload.Parts != null) { foreach (var item in itemmes.Payload.Parts) { if (item.MimeType == "text/plain") { mes.content = item.Body.Data; } else if (item.MimeType == "multipart/related" || item.MimeType == "multipart/alternative") { foreach (var itemm in item.Parts) { if (itemm.MimeType == "text/plain") { mes.content = itemm.Body.Data; } else if (itemm.MimeType == "multipart/related" || itemm.MimeType == "multipart/alternative") { foreach (var itemmini in itemm.Parts) { if (itemmini.MimeType == "text/plain") { mes.content = itemmini.Body.Data; } } } } } } } else { mes.content = itemmes.Payload.Body.Data; } } mes.idmes = itemmes.Id; mesLHeader.Add(mes); } return mesLHeader; }