public TResult ParseContentDelegate <TResult>(JContainer contentJContainer, string contentString, BindConvert bindConvert, ParameterInfo parameterInfo, IApplication httpApp, IHttpRequest request, Func <object, TResult> onParsed, Func <string, TResult> onFailure) { if (!(contentJContainer is JObject)) { return(onFailure($"JSON Content is {contentJContainer.Type} and headers can only be parsed from objects.")); } var contentJObject = contentJContainer as JObject; var key = Content; if (!contentJObject.TryGetValue(key, out JToken valueToken)) { return(onFailure($"Key[{key}] was not found in JSON")); } if (typeof(System.Net.Http.Headers.MediaTypeHeaderValue) == parameterInfo.ParameterType) { var contentEncodedBase64String = valueToken.Value <string>(); var mediaHeaderType = contentEncodedBase64String.MatchRegexInvoke( "data:(?<contentType>[^;]+);base64,.+", (contentType) => new MediaTypeHeaderValue(contentType), types => types.First( (ct, next) => ct, () => new MediaTypeHeaderValue("application/data"))); return(onParsed(mediaHeaderType)); } throw new NotImplementedException(); }
public void UpdateMessageInteractionInfo(MessageViewModel message) { if (message.InteractionInfo?.ReplyInfo?.ReplyCount > 0 && !message.IsChannelPost) { RepliesGlyph.Text = "\uE93E\u00A0\u00A0"; RepliesLabel.Text = $"{message.InteractionInfo.ReplyInfo.ReplyCount} "; } else { RepliesGlyph.Text = string.Empty; RepliesLabel.Text = string.Empty; } var views = string.Empty; if (message.InteractionInfo?.ViewCount > 0) { views = BindConvert.ShortNumber(message.InteractionInfo.ViewCount); views += " "; } if (message.IsChannelPost && !string.IsNullOrEmpty(message.AuthorSignature)) { views += $"{message.AuthorSignature}, "; } else if (message.ForwardInfo?.Origin is MessageForwardOriginChannel fromChannel && !string.IsNullOrEmpty(fromChannel.AuthorSignature)) { views += $"{fromChannel.AuthorSignature}, "; } ViewsGlyph.Text = message.InteractionInfo?.ViewCount > 0 ? "\uE607\u00A0\u00A0" : string.Empty; ViewsLabel.Text = views; }
private string ConvertAmount(long value, bool integer) { var grams = BindConvert.Grams(value, true); var split = grams.Split(' '); var gem = split[0] == "\uD83D\uDC8E"; var amount = split[gem ? 1 : 0].Split('.'); if (integer) { if (gem) { return($"\uD83D\uDC8E {amount[0]}"); } return(amount[0]); } if (!gem) { return($".{amount[1]} \uD83D\uDC8E"); } return($".{amount[1]}"); }
private void OnElementPrepared(Microsoft.UI.Xaml.Controls.ItemsRepeater sender, Microsoft.UI.Xaml.Controls.ItemsRepeaterElementPreparedEventArgs args) { var button = args.Element as Button; var content = button.Content as Grid; var nearby = sender.ItemsSourceView.GetAt(args.Index) as ChatNearby; var chat = ViewModel.CacheService.GetChat(nearby.ChatId); if (chat == null) { return; } var title = content.Children[1] as TextBlock; title.Text = ViewModel.ProtoService.GetTitle(chat); if (ViewModel.CacheService.TryGetSupergroup(chat, out Supergroup supergroup)) { var subtitle = content.Children[2] as TextBlock; subtitle.Text = string.Format("{0}, {1}", BindConvert.Distance(nearby.Distance), Locale.Declension("Members", supergroup.MemberCount)); } else { var subtitle = content.Children[2] as TextBlock; subtitle.Text = BindConvert.Distance(nearby.Distance); } var photo = content.Children[0] as ProfilePicture; photo.Source = PlaceholderHelper.GetChat(ViewModel.ProtoService, chat, 36); button.Command = ViewModel.OpenChatCommand; button.CommandParameter = nearby; }
private string ConvertAmount(long value) { if (value > 0) { return(BindConvert.Grams(value, false)); } return(string.Empty); }
public static async Task <TResult> OnContentsAsync <TResource, TResult>( this IQueryable <TResource> requestQuery, HttpMethod method, Func <TResource[], TResult> onContents) { var response = await requestQuery .ApplyMethod(method) .SendAsync(); var jsonContent = await response.Content.ReadAsStringAsync(); var request = (requestQuery as RequestMessage <TResource>); var httpApp = request.InvokeApplication as HttpApplication; var converter = new BindConvert(httpApp); var resources = JsonConvert.DeserializeObject <TResource[]>(jsonContent, converter); return(onContents(resources)); }
private string ConvertText(WalletInfoState state, long amount) { switch (state) { case WalletInfoState.Created: return(Strings.Resources.WalletCongratulationsinfo); case WalletInfoState.Ready: return(Strings.Resources.WalletReadyInfo); case WalletInfoState.Sent: return(string.Format(Strings.Resources.WalletSendDoneText, BindConvert.Grams(amount, false))); case WalletInfoState.TooBad: return(Strings.Resources.WalletTooBadInfo); default: return(null); } }
public void ConvertViews(MessageViewModel message) { var number = string.Empty; if (message.Views > 0) { number = BindConvert.ShortNumber(Math.Max(message.Views, 1)); number += " "; } if (message.IsChannelPost && !string.IsNullOrEmpty(message.AuthorSignature)) { number += $"{message.AuthorSignature}, "; } else if (message.ForwardInfo?.Origin is MessageForwardOriginChannel forwardedPost && !string.IsNullOrEmpty(forwardedPost.AuthorSignature)) { number += $"{forwardedPost.AuthorSignature}, "; } ViewsGlyph.Text = message.Views > 0 ? "\uE607\u00A0\u00A0" : string.Empty; ViewsLabel.Text = number; }
private string ConvertBalance(long value) { return(string.Format(Strings.Resources.WalletSendBalance, BindConvert.Grams(value, true))); }
private void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args) { if (args.InRecycleQueue) { return; } if (args.Item is DateTime dateHeader) { var border = args.ItemContainer.ContentTemplateRoot as Border; var label = border.Child as TextBlock; label.Text = BindConvert.DayGrouping(dateHeader); return; } var item = args.Item as RawTransaction; var root = args.ItemContainer.ContentTemplateRoot as Grid; var content = root.Children[0] as Grid; var headline = content.Children[0] as TextBlock; var address = content.Children[1] as TextBlock; var message = content.Children[2] as TextBlock; var timestamp = content.Children[3] as TextBlock; headline.Inlines.Clear(); long amount; IList <byte> comment; if (item.InMsg != null) { amount = item.InMsg.Value; comment = item.InMsg.Message; } else { amount = 0; comment = null; } foreach (var msg in item.OutMsgs) { amount -= msg.Value; if (comment == null || comment.IsEmpty()) { comment = msg.Message; } } amount -= item.Fee; if (amount > 0) { headline.Inlines.Add(new Run { Text = ConvertAmount(amount), Foreground = new SolidColorBrush(Windows.UI.Colors.Green), FontWeight = FontWeights.SemiBold }); headline.Inlines.Add(new Run { Text = $" {Strings.Resources.WalletFrom}" }); address.Text = ConvertAddress(item.InMsg.Source); } else { headline.Inlines.Add(new Run { Text = ConvertAmount(amount), Foreground = new SolidColorBrush(Windows.UI.Colors.Red), FontWeight = FontWeights.SemiBold }); if (item.OutMsgs.IsEmpty()) { address.Text = Strings.Resources.WalletTransactionFee; } else { headline.Inlines.Add(new Run { Text = $" {Strings.Resources.WalletTo}" }); address.Text = ConvertAddress(item.OutMsgs[0].Destination); } } if (comment != null && comment.Count > 0) { message.Text = Encoding.UTF8.GetString(comment.ToArray()); message.Visibility = Visibility.Visible; } else { message.Visibility = Visibility.Collapsed; } timestamp.Text = BindConvert.Current.Date((int)item.Utime); }
private string ConvertAmount(long value) { return(BindConvert.Grams(value, true)); }
private void UpdateHeaderDate(bool intermediate) { var panel = Messages.ItemsPanelRoot as ItemsStackPanel; if (panel == null || panel.FirstVisibleIndex < 0) { return; } var firstVisibleId = 0L; var lastVisibleId = 0L; var minItem = true; var minDate = true; var minDateIndex = panel.FirstVisibleIndex; for (int i = panel.FirstVisibleIndex; i <= panel.LastVisibleIndex; i++) { var container = Messages.ContainerFromIndex(i) as SelectorItem; if (container == null) { continue; } var message = Messages.ItemFromContainer(container) as MessageViewModel; if (message == null) { continue; } if (firstVisibleId == 0) { firstVisibleId = message.Id; } else if (message.Id != 0) { lastVisibleId = message.Id; } if (minItem && i >= panel.FirstVisibleIndex) { var transform = container.TransformToVisual(DateHeaderRelative); var point = transform.TransformPoint(new Point()); if (point.Y + container.ActualHeight >= 0) { minItem = false; if (message.SchedulingState is MessageSchedulingStateSendAtDate sendAtDate) { DateHeader.CommandParameter = null; DateHeaderLabel.Text = string.Format(Strings.Resources.MessageScheduledOn, BindConvert.DayGrouping(Utils.UnixTimestampToDateTime(sendAtDate.SendDate))); } else if (message.SchedulingState is MessageSchedulingStateSendWhenOnline) { DateHeader.CommandParameter = null; DateHeaderLabel.Text = Strings.Resources.MessageScheduledUntilOnline; } else { DateHeader.CommandParameter = message.Date; DateHeaderLabel.Text = BindConvert.DayGrouping(Utils.UnixTimestampToDateTime(message.Date)); } } } if (message.Content is MessageHeaderDate && minDate && i >= panel.FirstVisibleIndex) { var transform = container.TransformToVisual(DateHeaderRelative); var point = transform.TransformPoint(new Point()); var height = (float)DateHeader.ActualHeight; var offset = (float)point.Y + height; minDate = false; if (/*offset >= 0 &&*/ offset < height) { container.Opacity = 0; minDateIndex = int.MaxValue; // Force show } else { container.Opacity = 1; minDateIndex = i; } if (offset >= height && offset < height * 2) { _dateHeader.Offset = new Vector3(0, -height * 2 + offset, 0); } else { _dateHeader.Offset = Vector3.Zero; } } else { container.Opacity = 1; } } _dateHeaderTimer.Stop(); _dateHeaderTimer.Start(); ShowHideDateHeader(minDateIndex > 0, minDateIndex > 0 && minDateIndex < int.MaxValue); // Read and play messages logic: //TODO: //if (messages.Count > 0 && _windowContext.ActivationMode == CoreWindowActivationMode.ActivatedInForeground) //{ // ViewModel.ProtoService.Send(new ViewMessages(chat.Id, ViewModel.ThreadId, messages, false)); //} //if (animations.Count > 0 && !intermediate) //{ // Play(animations, ViewModel.Settings.IsAutoPlayAnimationsEnabled, false); //} // Pinned banner if (firstVisibleId == 0 || lastVisibleId == 0) { return; } if (ViewModel.LockedPinnedMessageId < firstVisibleId) { ViewModel.LockedPinnedMessageId = 0; } var thread = ViewModel.Thread; if (thread != null) { var message = thread.Messages.LastOrDefault(); if (message == null || (firstVisibleId <= message.Id && lastVisibleId >= message.Id)) { PinnedMessage.UpdateMessage(ViewModel.Chat, null, false, 0, 1, false); } else { PinnedMessage.UpdateMessage(ViewModel.Chat, ViewModel.CreateMessage(message), false, 0, 1, false); } } else if (ViewModel.PinnedMessages.Count > 0) { var currentPinned = ViewModel.LockedPinnedMessageId != 0 ? ViewModel.PinnedMessages.LastOrDefault(x => x.Id < firstVisibleId) ?? ViewModel.PinnedMessages.LastOrDefault() : ViewModel.PinnedMessages.LastOrDefault(x => x.Id <= lastVisibleId) ?? ViewModel.PinnedMessages.FirstOrDefault(); if (currentPinned != null) { //PinnedMessage.UpdateIndex(ViewModel.PinnedMessages.IndexOf(currentPinned), ViewModel.PinnedMessages.Count, intermediate); PinnedMessage.UpdateMessage(ViewModel.Chat, currentPinned, false, ViewModel.PinnedMessages.IndexOf(currentPinned), ViewModel.PinnedMessages.Count, intermediate); } else { PinnedMessage.UpdateMessage(ViewModel.Chat, null, false, 0, 1, false); } } }
public async Task <IHttpResponse> ParseContentValuesAsync( IApplication httpApp, IHttpRequest request, Func < CastDelegate, string[], Task <IHttpResponse> > onParsedContentValues) { if (!request.HasBody) { return(await BodyMissing("Body was not provided")); } var contentString = await request.ReadContentAsStringAsync(); if (contentString.IsNullOrWhiteSpace()) { return(await BodyMissing("JSON body content is empty")); } var bindConvert = new BindConvert(request, httpApp as HttpApplication); try { var contentJObject = Newtonsoft.Json.Linq.JObject.Parse(contentString); CastDelegate parser = (paramInfo, onParsed, onFailure) => { return(paramInfo .GetAttributeInterface <IBindJsonApiValue>() .ParseContentDelegate(contentJObject, contentString, bindConvert, paramInfo, httpApp, request, onParsed, onFailure)); }; var keys = contentJObject .Properties() .Select(jProperty => jProperty.Name) .ToArray(); return(await onParsedContentValues(parser, keys)); } catch (Newtonsoft.Json.JsonReaderException) { try { var contentJArray = Newtonsoft.Json.Linq.JArray.Parse(contentString); CastDelegate parser = (paramInfo, onParsed, onFailure) => { return(paramInfo .GetAttributeInterface <IBindJsonApiValue>() .ParseContentDelegate(contentJArray, contentString, bindConvert, paramInfo, httpApp, request, onParsed, onFailure)); }; var keys = new string[] { }; return(await onParsedContentValues(parser, keys)); } catch (Exception ex) { return(await BodyMissing(ex.Message)); } } catch (Exception ex) { return(await BodyMissing(ex.Message)); } Task <IHttpResponse> BodyMissing(string failureMessage) { CastDelegate emptyParser = (paramInfo, onParsed, onFailure) => { var key = paramInfo .GetAttributeInterface <IBindApiValue>() .GetKey(paramInfo) .ToLowerNullSafe(); var type = paramInfo.ParameterType; return(onFailure($"[{key}] could not be parsed ({failureMessage}).")); }; var exceptionKeys = new string[] { }; return(onParsedContentValues(emptyParser, exceptionKeys)); } }