Skip to content

Commit

Permalink
Ability to change chat background, other fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Jun 10, 2017
1 parent c9ed0ec commit 20eee33
Show file tree
Hide file tree
Showing 19 changed files with 415 additions and 50 deletions.
5 changes: 5 additions & 0 deletions Unigram/Unigram.Api/Helpers/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public static string GetTempFileName(string fileName)
return Path.Combine(ApplicationData.Current.LocalFolder.Path, SettingsHelper.SessionGuid, "temp", fileName);
}

public static string GetTempFilePath(string fileName)
{
return Path.Combine(SettingsHelper.SessionGuid, "temp", fileName);
}

public static Uri GetTempFileUri(string fileName)
{
return new Uri($"ms-appdata:///local/{SettingsHelper.SessionGuid}/temp/{fileName}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ private void OnDownloading(object state)
part.ParentItem.Parts.Insert(currentItemIndex + 1, missingPart);
}
}
else if (data.Length == part.Limit && (part.Number + 1) == part.ParentItem.Parts.Count)
{
var currentItemIndex = part.ParentItem.Parts.IndexOf(part);
var missingPart = new DownloadablePart(part.ParentItem, part.Offset + part.Limit, part.Limit, currentItemIndex + 1);

part.ParentItem.Parts.Insert(currentItemIndex + 1, missingPart);
}

isCanceled = part.ParentItem.IsCancelled;

Expand Down
12 changes: 0 additions & 12 deletions Unigram/Unigram.Api/TL/Partial/TLWallPaper.Partial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ public TLPhotoSizeBase Thumb
}
}

private TLPhotoSizeBase _medium;
public TLPhotoSizeBase Medium
{
get
{
if (_medium == null)
InitializeSizes();

return _medium;
}
}

private TLPhotoSizeBase _full;
public TLPhotoSizeBase Full
{
Expand Down
27 changes: 27 additions & 0 deletions Unigram/Unigram.Api/TL/Partial/TLWallPaperSolid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;

namespace Telegram.Api.TL
{
public partial class TLWallPaperSolid
{
private Color? _backgroundColor;
public Color BackgroundColor
{
get
{
if (_backgroundColor == null)
_backgroundColor = Windows.UI.Color.FromArgb(0xFF,
(byte)((BgColor >> 16) & 0xFF),
(byte)((BgColor >> 8) & 0xFF),
(byte)((BgColor & 0xFF)));

return _backgroundColor.Value;
}
}
}
}
1 change: 1 addition & 0 deletions Unigram/Unigram.Api/Unigram.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@
<Compile Include="TL\Partial\TLUserProfilePhoto.Partial.cs" />
<Compile Include="TL\Partial\TLUserProfilePhotoBase.Partial.cs" />
<Compile Include="TL\Partial\TLWallPaper.Partial.cs" />
<Compile Include="TL\Partial\TLWallPaperSolid.cs" />
<Compile Include="TL\Partial\TLWebDocument.Partial.cs" />
<Compile Include="TL\Partial\TLWebPageBase.Partial.cs" />
<Compile Include="TL\TLAccountAuthorizations.cs" />
Expand Down
34 changes: 34 additions & 0 deletions Unigram/Unigram.Core/Common/ApplicationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,40 @@ public bool IsContactsSyncEnabled
}
}

private int? _selectedBackground;
public int SelectedBackground
{
get
{
if (_selectedBackground == null)
_selectedBackground = GetValueOrDefault("SelectedBackground", 1000001);

return _selectedBackground ?? 1000001;
}
set
{
_selectedBackground = value;
AddOrUpdateValue("SelectedBackground", value);
}
}

private int? _selectedColor;
public int SelectedColor
{
get
{
if (_selectedColor == null)
_selectedColor = GetValueOrDefault("SelectedColor", 0);

return _selectedColor ?? 0;
}
set
{
_selectedColor = value;
AddOrUpdateValue("SelectedColor", value);
}
}

private TLAccountTmpPassword _tmpPassword;
public TLAccountTmpPassword TmpPassword
{
Expand Down
5 changes: 3 additions & 2 deletions Unigram/Unigram/Controls/BubbleListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Telegram.Api.Aggregator;
using Telegram.Api.Helpers;
using Telegram.Api.TL;
using Unigram.Converters;
using Unigram.ViewModels;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand Down Expand Up @@ -183,7 +184,7 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
}
else
{
bubble.Padding = new Thickness(52, 0, 52, 0);
bubble.Padding = new Thickness(52, 0, MessageToShareConverter.Convert(message) ? 4 : 52, 0);
}
}
}
Expand All @@ -201,7 +202,7 @@ protected override void PrepareContainerForItemOverride(DependencyObject element
}
else
{
bubble.Padding = new Thickness(12, 0, 52, 0);
bubble.Padding = new Thickness(12, 0, MessageToShareConverter.Convert(message) ? 4 : 52, 0);
}
}
}
Expand Down
85 changes: 85 additions & 0 deletions Unigram/Unigram/Controls/DialogBackgroundPresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Api.Aggregator;
using Telegram.Api.Helpers;
using Unigram.Common;
using Unigram.Views;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Shapes;

namespace Unigram.Controls
{
public class DialogBackgroundPresenter : ContentControl, IHandle<string>
{
private DialogBackground _defaultBackground;
private Image _imageBackground;
private Rectangle _colorBackground;

public DialogBackgroundPresenter()
{
Reload();
UnigramContainer.Current.ResolveType<ITelegramEventAggregator>().Subscribe(this);
}

public void Handle(string message)
{
if (message.Equals("Wallpaper"))
{
Reload();
}
}

private async void Reload()
{
var selectedBackground = ApplicationSettings.Current.SelectedBackground;
var selectedColor = ApplicationSettings.Current.SelectedColor;

if (selectedColor == 0)
{
if (selectedBackground != 1000001)
{
var item = await ApplicationData.Current.LocalFolder.TryGetItemAsync(FileUtils.GetTempFilePath("wallpaper.jpg"));
if (item is StorageFile file)
{
if (_imageBackground == null)
_imageBackground = new Image { Stretch = Stretch.UniformToFill };

using (var stream = await file.OpenReadAsync())
{
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(stream);
_imageBackground.Source = bitmap;
}

Content = _imageBackground;
}
}
else
{
if (_defaultBackground == null)
_defaultBackground = new DialogBackground();

Content = _defaultBackground;
}
}
else
{
if (_colorBackground == null)
_colorBackground = new Rectangle();

_colorBackground.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(0xFF,
(byte)((selectedColor >> 16) & 0xFF),
(byte)((selectedColor >> 8) & 0xFF),
(byte)((selectedColor & 0xFF))));

Content = _colorBackground;
}
}
}
}
31 changes: 18 additions & 13 deletions Unigram/Unigram/Converters/MessageToShareConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,42 @@ public object Convert(object value, Type targetType, object parameter, string la
return Visibility.Collapsed;
}

return Convert(message) ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}

public static bool Convert(TLMessage message)
{
if (message.IsSticker())
{
return Visibility.Collapsed;
return false;
}
else if (message.HasFwdFrom && message.FwdFrom.HasChannelId && !message.IsOut)
{
return Visibility.Visible;
return true;
}
else if (message.HasFromId && !message.IsPost)
{
if (message.Media is TLMessageMediaEmpty || message.Media == null || message.Media is TLMessageMediaWebPage webpageMedia && !(webpageMedia.WebPage is TLWebPage))
{
return Visibility.Collapsed;
return false;
}

var user = message.From;
if (user != null && user.IsBot)
{
return Visibility.Visible;
return true;
}

if (!message.IsOut)
{
if (message.Media is TLMessageMediaGame || message.Media is TLMessageMediaInvoice)
{
return Visibility.Visible;
return true;
}

var parent = message.Parent as TLChannel;
Expand All @@ -53,7 +63,7 @@ public object Convert(object value, Type targetType, object parameter, string la
//TLRPC.Chat chat = MessagesController.getInstance().getChat(messageObject.messageOwner.to_id.channel_id);
//return chat != null && chat.username != null && chat.username.length() > 0 && !(messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaContact) && !(messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeo);

return parent.HasUsername && !(message.Media is TLMessageMediaContact) && !(message.Media is TLMessageMediaGeo) ? Visibility.Visible : Visibility.Collapsed;
return parent.HasUsername && !(message.Media is TLMessageMediaContact) && !(message.Media is TLMessageMediaGeo);
}
}
}
Expand All @@ -67,16 +77,11 @@ public object Convert(object value, Type targetType, object parameter, string la

if (message.ToId is TLPeerChannel && (!message.HasViaBotId && !message.HasReplyToMsgId))
{
return Visibility.Visible;
return true;
}
}

return Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
return false;
}
}
}
2 changes: 1 addition & 1 deletion Unigram/Unigram/Converters/UsernameConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public object Convert(object value, Type targetType, object parameter, string la
{
if (string.IsNullOrEmpty((string)value))
{
return "None";
return parameter == null ? "None" : null;
}

return $"@{value}";
Expand Down
6 changes: 6 additions & 0 deletions Unigram/Unigram/Selectors/WallPaperTemplateSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Unigram.Selectors
{
public class WallPaperTemplateSelector : DataTemplateSelector
{
public DataTemplate DefaultTemplate { get; set; }

public DataTemplate ItemTemplate { get; set; }
public DataTemplate SolidTemplate { get; set; }

Expand All @@ -20,6 +22,10 @@ protected override DataTemplate SelectTemplateCore(object item, DependencyObject
{
return SolidTemplate;
}
else if (item is TLWallPaper wallpaper && wallpaper.Id.Equals(1000001))
{
return DefaultTemplate;
}

return ItemTemplate;
}
Expand Down
6 changes: 2 additions & 4 deletions Unigram/Unigram/Themes/Media.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@
<Grid Width="72" Height="72" Background="Black" VerticalAlignment="Top">
<Image Source="{Binding Media.Document, Converter={StaticResource DefaultPhotoConverter}}" Width="72" Height="72" Stretch="UniformToFill"/>

<Grid Width="48" Height="48" CornerRadius="24" Background="{ThemeResource MessageOverlayBackgroundBrush}">
<controls:TransferButton Completed="Download_Click" Transferable="{Binding Media.Document}" IsTransferring="{Binding Media.Document.IsTransferring}" Foreground="White" CornerRadius="24" RequestedTheme="Dark"/>
<controls:ProgressBarRing Background="Transparent" Foreground="White" Value="{Binding Media.Document.Progress}" IsHitTestVisible="False"/>
</Grid>
<controls:TransferButton Completed="Download_Click" Transferable="{Binding Media.Document}" IsTransferring="{Binding Media.Document.IsTransferring}" Style="{StaticResource MediaTransferButtonStyle}"/>
<controls:ProgressBarRing Background="Transparent" Foreground="White" Value="{Binding Media.Document.Progress}" IsHitTestVisible="False"/>
</Grid>
<StackPanel Margin="8,0,0,2" VerticalAlignment="Center" Grid.Column="1">
<TextBlock Text="{Binding Media.Document.FileName}" Foreground="{ThemeResource MessageForegroundBrush}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" Style="{ThemeResource BaseTextBlockStyle}"/>
Expand Down
1 change: 1 addition & 0 deletions Unigram/Unigram/Unigram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<Compile Include="Common\TLBitmapContext.cs" />
<Compile Include="Common\VisualUtilities.cs" />
<Compile Include="Common\VoIPConnection.cs" />
<Compile Include="Controls\DialogBackgroundPresenter.cs" />
<Compile Include="Controls\GroupedGridView.cs" />
<Compile Include="Controls\GroupedListView.cs" />
<Compile Include="Controls\ImageCropper.cs" />
Expand Down
Loading

0 comments on commit 20eee33

Please sign in to comment.