Skip to content

Commit

Permalink
Better name finding, full support for groups, nicer UI, default images
Browse files Browse the repository at this point in the history
  • Loading branch information
StarGate01 committed Sep 11, 2016
1 parent 3fa7e31 commit 5736f52
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 41 deletions.
Binary file removed MidnightLockTwo/Assets/AlignmentGrid.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 30 additions & 9 deletions MidnightLockTwo/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
xmlns:model="clr-namespace:MidnightLockTwo.Models">
xmlns:model="clr-namespace:MidnightLockTwo.Models"
xmlns:whatsapp="clr-namespace:MidnightLockTwo.WhatsApp">

<Grid x:Name="LayoutRoot" Background="Transparent" d:DataContext="{d:DesignInstance Type=model:MainView, IsDesignTimeCreatable=True}">
<Grid.Resources>
<whatsapp:GroupVisibilityConverter x:Key="GroupVisibilityConverter1" />
<whatsapp:SingleVisibilityConverter x:Key="SingleVisibilityConverter1" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="WhatsApp Messages" Style="{StaticResource PhoneTextTitle2Style}" />
<TextBlock Grid.Row="0" Text="WhatsApp Messages" Style="{StaticResource PhoneTextTitle2Style}" Margin="12,0,12,10" />
<ListBox Grid.Row="1" ItemsSource="{Binding Messages}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
Expand All @@ -28,18 +33,34 @@
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="5">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Image Grid.Column="1" Source="{Binding Sender.ProfilePicture}" Width="70" />
<TextBlock Grid.Column="3" Text="{Binding Sender.Name}" FontSize="24" VerticalAlignment="Center" />
<TextBlock Grid.Column="4" VerticalAlignment="Center" Text="{Binding Count}" FontSize="36" />
<Image Grid.Column="0" Source="{Binding Sender.ProfilePicture}" Width="70">
<Image.Clip>
<EllipseGeometry Center="35,35" RadiusX="35" RadiusY="35" />
</Image.Clip>
</Image>
<Grid Grid.Column="2" Visibility="{Binding Sender.Origin, Converter={StaticResource SingleVisibilityConverter1}}" >
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Sender.Name}" FontSize="24" VerticalAlignment="Center" />
<TextBlock Grid.Row="1" Text="{Binding Sender.FullName}" FontSize="24" VerticalAlignment="Center" >
<TextBlock.Foreground>
<SolidColorBrush Color="{StaticResource PhoneInverseInactiveColor}"/>
</TextBlock.Foreground>
</TextBlock>
</Grid>
<Grid Grid.Column="2" Visibility="{Binding Sender.Origin, Converter={StaticResource GroupVisibilityConverter1}}" >
<TextBlock Text="{Binding Sender.Name}" FontSize="24" VerticalAlignment="Center" />
</Grid>
<TextBlock Grid.Column="3" VerticalAlignment="Center" Text="{Binding Count}" FontSize="36" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
Expand Down
3 changes: 2 additions & 1 deletion MidnightLockTwo/MidnightLockTwo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@
<Content Include="Test\messages.db" />
</ItemGroup>
<ItemGroup>
<Content Include="Assets\AlignmentGrid.png" />
<Content Include="Assets\ApplicationIcon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand All @@ -149,6 +148,8 @@
<Content Include="Assets\SquareTile150x150.png" />
<Content Include="Assets\SquareTile71x71.png" />
<Content Include="Assets\StoreLogo.png" />
<Content Include="Assets\WhatsApp\default-contact-icon.png" />
<Content Include="Assets\WhatsApp\default-group-icon.png" />
<Content Include="Assets\WideLogo.png" />
<Content Include="Assets\Tiles\FlipCycleTileLarge.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
6 changes: 4 additions & 2 deletions MidnightLockTwo/Models/MainView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace MidnightLockTwo.Models
{
Expand Down Expand Up @@ -54,8 +55,8 @@ private void PopulateDesignerData()
{
Messages = new List<WhatsApp.MessagesMetaData>()
{
{ new WhatsApp.MessagesMetaData(3, new WhatsApp.Sender("Max Muster", WhatsApp.Sender.OriginType.Single, new Uri(@"/Assets/ApplicationIcon.png", UriKind.Relative))) },
{ new WhatsApp.MessagesMetaData(2, new WhatsApp.Sender("Oceans 11", WhatsApp.Sender.OriginType.Group, new Uri(@"/Assets/ApplicationIcon.png", UriKind.Relative))) },
{ new WhatsApp.MessagesMetaData(3, new WhatsApp.Sender("Max", "Max Mustermann", WhatsApp.Client.FALLBACK_IMG_SINGLE, WhatsApp.Sender.OriginType.Single)) },
{ new WhatsApp.MessagesMetaData(2, new WhatsApp.Sender("Oceans 11", "Oceans 11", WhatsApp.Client.FALLBACK_IMG_GROUP, WhatsApp.Sender.OriginType.Group)) }
};
}

Expand All @@ -71,4 +72,5 @@ public MainView GetCopy()
}

}

}
87 changes: 59 additions & 28 deletions MidnightLockTwo/WhatsApp/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,34 @@ private const string
APPDATA_DB_CONTACTS_FILE = @"\LocalState\contacts.db",
APPDATA_PICTURE_DIR = @"\LocalState\profilePictures";

private readonly Uri FALLBACK_IMG = new Uri(@"/Assets/ApplicationIcon.png", UriKind.Relative);
public static readonly Uri
FALLBACK_IMG_SINGLE = new Uri(@"/Assets/WhatsApp/default-contact-icon.png", UriKind.Relative),
FALLBACK_IMG_GROUP = new Uri(@"/Assets/WhatsApp/default-group-icon.png", UriKind.Relative);

private const string
FALLBACK_NAME = "Unknown",
FALLBACK_FULLNAME = "Unknown Number";

private static readonly Sender FALLBACK_SENDER = new Sender(FALLBACK_NAME, FALLBACK_FULLNAME, FALLBACK_IMG_SINGLE, Sender.OriginType.Single);

#endregion

#region Statements

private const string
UNREAD_MSGS_SQL = @"
SELECT Conversations.UnreadMessageCount, Conversations.Jid, Conversations.GroupSubject, Messages.PushName
FROM Conversations
INNER JOIN Messages
ON Messages.MessageID = Conversations.FirstUnreadMessageID
SELECT Conversations.UnreadMessageCount, Conversations.Jid, Conversations.GroupSubject
FROM Conversations
WHERE UnreadMessageCount <> 0;",
PHOTO_ID = @"
SELECT ChatPictures.WaPhotoId
PERSONAL_INFO_SQL = @"
SELECT ChatPictures.WaPhotoId , PhoneNumbers.RawPhoneNumber, UserStatuses.ContactName, UserStatuses.FirstName, UserStatuses.PushName
FROM ChatPictures
LEFT OUTER JOIN PhoneNumbers
ON ChatPictures.Jid = PhoneNumbers.Jid
LEFT OUTER JOIN UserStatuses
ON ChatPictures.Jid = UserStatuses.Jid
WHERE ChatPictures.Jid = ?;";
private Statement unreadMsgs, photoId;
private Statement unreadMsgs, personalInfo;

#endregion

Expand All @@ -79,24 +89,25 @@ public async Task Initialize()
await msgDatabase.OpenAsync(SqliteOpenMode.OpenRead);
unreadMsgs = await msgDatabase.PrepareStatementAsync(UNREAD_MSGS_SQL);
await contactsDatabase.OpenAsync(SqliteOpenMode.OpenRead);
photoId = await contactsDatabase.PrepareStatementAsync(PHOTO_ID);
personalInfo = await contactsDatabase.PrepareStatementAsync(PERSONAL_INFO_SQL);
_clientState = State.Ready;
}

public async Task<List<MessagesMetaData>> GetUnreadMessagesMetaData()
{
if (_clientState != State.Ready) throw new Exception("Object is not initialized!");
if (_clientState != State.Ready) throw new InvalidOperationException();
List<MessagesMetaData> messages = new List<MessagesMetaData>();
unreadMsgs.Reset();
while (await unreadMsgs.StepAsync())
{
string name = unreadMsgs.GetTextAt(3);
string groupName = unreadMsgs.GetTextAt(2);
if (groupName != null && groupName != "") name = groupName;
string imagePath = await GetThumbProfilePicture(unreadMsgs.GetTextAt(1));
messages.Add(new MessagesMetaData(unreadMsgs.GetIntAt(0),
new Sender(name, (groupName == null || groupName == "") ? Sender.OriginType.Single : Sender.OriginType.Group,
imagePath == null ? FALLBACK_IMG : new Uri(imagePath, UriKind.Absolute))));
Sender sender = await GetPersonalInfo(unreadMsgs.GetTextAt(1), (groupName == null || groupName == "") ? Sender.OriginType.Single : Sender.OriginType.Group);
if(sender.Origin == Sender.OriginType.Group)
{
sender.Name = groupName;
sender.FullName = groupName;
}
messages.Add(new MessagesMetaData(unreadMsgs.GetIntAt(0), sender));
}
return messages;
}
Expand All @@ -105,7 +116,7 @@ private Task<string> FindRootDir()
{
return Task.Run(() =>
{
Func<string> yieldDir = () =>
string foundDir = new Func <string> (() =>
{
if (IsolatedStorageSettings.ApplicationSettings.Contains(WA_ROOT_DIR_CONF_KEY))
{
Expand All @@ -116,29 +127,49 @@ private Task<string> FindRootDir()
if (Directory.Exists(dirToTry)) return dirToTry;
dirToTry = Directory.GetFiles(APPDATA_ROOT_DIR).FirstOrDefault(p => p.Contains(WA_PACKAGE_ID));
return dirToTry;
};
string foundDir = yieldDir();
})();
if (foundDir != null)
{
IsolatedStorageSettings.ApplicationSettings[WA_ROOT_DIR_CONF_KEY] = foundDir;
IsolatedStorageSettings.ApplicationSettings.Save();
return foundDir;
}
throw new Exception("No WhatsApp package found!");
throw new DirectoryNotFoundException();
});
}

private async Task<string> GetThumbProfilePicture(string jid)
private async Task<Sender> GetPersonalInfo(string jid, Sender.OriginType type)
{
if (_clientState != State.Ready) throw new Exception("Object is not initialized!");
photoId.Reset();
photoId.BindTextParameterAt(1, jid);
if(await photoId.StepAsync())
if (_clientState != State.Ready) throw new InvalidOperationException();
personalInfo.Reset();
personalInfo.BindTextParameterAt(1, jid);
if(await personalInfo.StepAsync())
{
string waPhotoId = photoId.GetTextAt(0);
if (waPhotoId != null && waPhotoId != "") return rootDir + APPDATA_PICTURE_DIR + @"\" + jid + waPhotoId + @"_thumb";
string waPhotoId = personalInfo.GetTextAt(0);
Uri profileImage = (type == Sender.OriginType.Single) ? FALLBACK_IMG_SINGLE : FALLBACK_IMG_GROUP;
if (waPhotoId != null && waPhotoId != "") profileImage = new Uri(rootDir + APPDATA_PICTURE_DIR + @"\" + jid + waPhotoId + @"_thumb", UriKind.Absolute);
if (type == Sender.OriginType.Group) return new Sender(string.Empty, string.Empty, profileImage, type);
else
{
string[] nameCandidates = new string[] { personalInfo.GetTextAt(3), personalInfo.GetTextAt(4), personalInfo.GetTextAt(1) };
string name = nameCandidates[2];
if (nameCandidates[0] != null && nameCandidates[0] != "") name = nameCandidates[0];
else if (nameCandidates[1] != null && nameCandidates[1] != "") name = nameCandidates[1];
string fullName = personalInfo.GetTextAt(2);
if (name == null || name == "")
{
if(fullName == null || fullName == "")
{
name = FALLBACK_NAME;
fullName = FALLBACK_FULLNAME;
}
else name = fullName;
}
else if (fullName == null || fullName == "") fullName = name;
return new Sender(name, fullName, profileImage, type);
}
}
return null;
return FALLBACK_SENDER;
}

public void Dispose()
Expand Down
42 changes: 41 additions & 1 deletion MidnightLockTwo/WhatsApp/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows;

namespace MidnightLockTwo.WhatsApp
{
Expand Down Expand Up @@ -31,6 +33,20 @@ public string Name
}
}

private string _fullName;
public string FullName
{
get
{
return _fullName;
}
set
{
_fullName = value;
RaisePropertyChanged("FullName");
}
}

private OriginType _origin;
public OriginType Origin
{
Expand Down Expand Up @@ -59,9 +75,10 @@ public Uri ProfilePicture
}
}

public Sender(string name, OriginType origin, Uri profilePicture)
public Sender(string name, string fullName, Uri profilePicture, OriginType origin)
{
_name = name;
_fullName = fullName;
_origin = origin;
_profilePicture = profilePicture;
}
Expand All @@ -79,4 +96,27 @@ public Sender GetCopy()

}

public class GroupVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((Sender.OriginType)value) == Sender.OriginType.Group ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; }

}

public class SingleVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((Sender.OriginType)value) == Sender.OriginType.Single ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; }

}


}

0 comments on commit 5736f52

Please # to comment.