Skip to content
This repository has been archived by the owner on Apr 15, 2021. It is now read-only.

Commit

Permalink
Initial implementation for stickers
Browse files Browse the repository at this point in the history
  • Loading branch information
x2bool committed Dec 22, 2018
1 parent 36b8336 commit 0c5e50c
Show file tree
Hide file tree
Showing 14 changed files with 434 additions and 21 deletions.
44 changes: 37 additions & 7 deletions src/Tel.Egram.Components.Messenger/Explorer/MessageModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public MessageModel CreateMessage(Message message)
case TdApi.MessageContent.MessagePhoto messagePhoto:
return CreatePhotoMessage(message, messagePhoto);

// case TdApi.MessageContent.MessageSticker messageSticker:
// return CreateStickerMessage(message, messageSticker);
case TdApi.MessageContent.MessageSticker messageSticker:
return CreateStickerMessage(message, messageSticker);

// case TdApi.MessageContent.MessageVideo messageVideo:
// return CreateVideoMessage(message, messageVideo);
case TdApi.MessageContent.MessageVideo messageVideo:
return CreateVideoMessage(message, messageVideo);

default:
return CreateUnsupportedMessage(message);
Expand Down Expand Up @@ -70,7 +70,7 @@ private PhotoMessageModel CreatePhotoMessage(
{
AuthorName = authorName,
Message = message,
Photo = photo,
PhotoData = photo,
Text = text
};
}
Expand All @@ -79,14 +79,44 @@ private StickerMessageModel CreateStickerMessage(
Message message,
TdApi.MessageContent.MessageSticker messageSticker)
{
throw new System.NotImplementedException();
var user = message.User;
var chat = message.Chat;

var authorName = (user == null)
? chat.Title
: $"{user.FirstName} {user.LastName}";

var sticker = messageSticker.Sticker;

return new StickerMessageModel
{
AuthorName = authorName,
Message = message,
StickerData = sticker
};
}

private VideoMessageModel CreateVideoMessage(
Message message,
TdApi.MessageContent.MessageVideo messageVideo)
{
throw new System.NotImplementedException();
var user = message.User;
var chat = message.Chat;

var authorName = (user == null)
? chat.Title
: $"{user.FirstName} {user.LastName}";

var text = messageVideo.Caption.Text;
var video = messageVideo.Video;

return new VideoMessageModel
{
AuthorName = authorName,
Message = message,
VideoData = video,
Text = text
};
}

private MessageModel CreateUnsupportedMessage(Message message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@

namespace Tel.Egram.Components.Messenger.Explorer.Messages
{
public class PhotoMessageModel : MessageModel, ISupportsActivation
public class PhotoMessageModel : PreviewableMessageModel, ISupportsActivation
{
public string Text { get; set; }

public Preview Preview { get; set; }

public TdApi.Photo Photo { get; set; }
public TdApi.Photo PhotoData { get; set; }

public PhotoMessageModel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ public static IDisposable BindPreviewLoading(
model,
Locator.Current.GetService<IPreviewLoader>());
}

public static IDisposable BindPreviewLoading(
this VideoMessageModel model)
{
return BindPreviewLoading(
model,
Locator.Current.GetService<IPreviewLoader>());
}

public static IDisposable BindPreviewLoading(
this StickerMessageModel model)
{
return BindPreviewLoading(
model,
Locator.Current.GetService<IPreviewLoader>());
}

public static IDisposable BindPreviewLoading(
this PhotoMessageModel model,
Expand All @@ -36,28 +52,115 @@ public static IDisposable BindPreviewLoading(

return Disposable.Empty;
}

public static IDisposable BindPreviewLoading(
this VideoMessageModel model,
IPreviewLoader previewLoader)
{
if (model.Preview == null)
{
model.Preview = GetPreview(previewLoader, model);

if (model.Preview == null || model.Preview.Bitmap == null)
{
return LoadPreview(previewLoader, model)
.Subscribe(preview =>
{
model.Preview = preview;
});
}
}

return Disposable.Empty;
}

public static IDisposable BindPreviewLoading(
this StickerMessageModel model,
IPreviewLoader previewLoader)
{
if (model.Preview == null)
{
model.Preview = GetPreview(previewLoader, model);

if (model.Preview == null || model.Preview.Bitmap == null)
{
return LoadPreview(previewLoader, model)
.Subscribe(preview =>
{
model.Preview = preview;
});
}
}

return Disposable.Empty;
}

private static Preview GetPreview(IPreviewLoader previewLoader, PhotoMessageModel model)
{
if (model.Photo != null)
if (model.PhotoData != null)
{
return previewLoader.GetPreview(model.Photo, PreviewQuality.High);
return previewLoader.GetPreview(model.PhotoData, PreviewQuality.High);
}

return null;
}

private static IObservable<Preview> LoadPreview(IPreviewLoader previewLoader, PhotoMessageModel model)
{
if (model.Photo != null)
if (model.PhotoData != null)
{
return previewLoader.LoadPreview(model.Photo, PreviewQuality.Low)
.Concat(previewLoader.LoadPreview(model.Photo, PreviewQuality.High));
return previewLoader.LoadPreview(model.PhotoData, PreviewQuality.Low)
.Concat(previewLoader.LoadPreview(model.PhotoData, PreviewQuality.High));
}

return Observable.Empty<Preview>();
}

private static Preview GetPreview(IPreviewLoader previewLoader, VideoMessageModel model)
{
if (model.VideoData?.Thumbnail != null)
{
return previewLoader.GetPreview(model.VideoData.Thumbnail);
}

return null;
}

private static IObservable<Preview> LoadPreview(IPreviewLoader previewLoader, VideoMessageModel model)
{
if (model.VideoData?.Thumbnail != null)
{
return previewLoader.LoadPreview(model.VideoData.Thumbnail);
}

return Observable.Empty<Preview>();
}

private static Preview GetPreview(IPreviewLoader previewLoader, StickerMessageModel model)
{
if (model.StickerData?.Thumbnail != null)
{
return previewLoader.GetPreview(model.StickerData.Thumbnail);
}

return null;
}

private static IObservable<Preview> LoadPreview(IPreviewLoader previewLoader, StickerMessageModel model)
{
if (model.StickerData != null)
{
if (model.StickerData?.Thumbnail != null)
{
return previewLoader.LoadPreview(model.StickerData.Thumbnail)
.Concat(previewLoader.LoadPreview(model.StickerData));
}

return previewLoader.LoadPreview(model.StickerData);
}

return Observable.Empty<Preview>();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using TdLib;
using Tel.Egram.Graphics.Previews;

namespace Tel.Egram.Components.Messenger.Explorer.Messages
{
public class PreviewableMessageModel : MessageModel
{
public Preview Preview { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using System.Reactive.Disposables;
using ReactiveUI;
using TdLib;

namespace Tel.Egram.Components.Messenger.Explorer.Messages
{
public class StickerMessageModel : MessageModel, ISupportsActivation
public class StickerMessageModel : PreviewableMessageModel, ISupportsActivation
{
public string Text { get; set; }
public TdApi.Sticker StickerData { get; set; }

public StickerMessageModel()
{
this.WhenActivated(disposables =>
{
this.BindAvatarLoading()
.DisposeWith(disposables);
this.BindPreviewLoading()
.DisposeWith(disposables);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
using System.Reactive.Disposables;
using ReactiveUI;
using TdLib;

namespace Tel.Egram.Components.Messenger.Explorer.Messages
{
public class VideoMessageModel : MessageModel, ISupportsActivation
public class VideoMessageModel : PreviewableMessageModel, ISupportsActivation
{
public string Text { get; set; }

public TdApi.Video VideoData { get; set; }

public VideoMessageModel()
{
this.WhenActivated(disposables =>
{
this.BindAvatarLoading()
.DisposeWith(disposables);
this.BindPreviewLoading()
.DisposeWith(disposables);
});
}

Expand Down
8 changes: 8 additions & 0 deletions src/Tel.Egram.Graphics/Previews/IPreviewLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@ public interface IPreviewLoader
Preview GetPreview(TdApi.Photo photo, PreviewQuality quality);

IObservable<Preview> LoadPreview(TdApi.Photo photo, PreviewQuality quality);

Preview GetPreview(TdApi.PhotoSize photoSize);

IObservable<Preview> LoadPreview(TdApi.PhotoSize photoSize);

Preview GetPreview(TdApi.Sticker sticker);

IObservable<Preview> LoadPreview(TdApi.Sticker sticker);
}
}
46 changes: 46 additions & 0 deletions src/Tel.Egram.Graphics/Previews/PreviewLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,52 @@ public IObservable<Preview> LoadPreview(
});
}

public Preview GetPreview(TdApi.PhotoSize photoSize)
{
var file = photoSize?.Photo;

return new Preview
{
Bitmap = GetBitmap(file),
Quality = PreviewQuality.High
};
}

public IObservable<Preview> LoadPreview(TdApi.PhotoSize photoSize)
{
var file = photoSize?.Photo;

return LoadBitmap(file)
.Select(bitmap => new Preview
{
Bitmap = bitmap,
Quality = PreviewQuality.High
});
}

public Preview GetPreview(TdApi.Sticker sticker)
{
var file = sticker.Sticker_;

return new Preview
{
Bitmap = GetBitmap(file),
Quality = PreviewQuality.High
};
}

public IObservable<Preview> LoadPreview(TdApi.Sticker sticker)
{
var file = sticker.Sticker_;

return LoadBitmap(file)
.Select(bitmap => new Preview
{
Bitmap = bitmap,
Quality = PreviewQuality.High
});
}

private IObservable<IBitmap> LoadBitmap(TdApi.File file)
{
if (file != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
<messages:PhotoMessageControl />
</DataTemplate>

<DataTemplate DataType="{x:Type model:VideoMessageModel}">
<messages:VideoMessageControl />
</DataTemplate>

<DataTemplate DataType="{x:Type model:StickerMessageModel}">
<messages:StickerMessageControl />
</DataTemplate>

<DataTemplate DataType="{x:Type model:UnsupportedMessageModel}">
<messages:UnsupportedMessageControl />
</DataTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
DataContext="{Binding Preview}">

<Image
Width="300"
Height="300"
MaxHeight="300"
Stretch="Uniform"
Source="{Binding Bitmap}" />

</Border>
Expand Down
Loading

0 comments on commit 0c5e50c

Please sign in to comment.