Skip to content

Commit

Permalink
Fixed friends
Browse files Browse the repository at this point in the history
- Changed FriendViewModel to UserIteViewModel
- changed constructor to auto populate fields when only the ID is set
- Fixed Friend API, closes #73
- Fixed Profile page generating a compiler warning
- Changed various views to account for the ViewModel changes
  • Loading branch information
ChrisK91 committed Sep 7, 2013
1 parent 397409a commit ccac90d
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 58 deletions.
8 changes: 3 additions & 5 deletions SparklrLib/Objects/Responses/Work/Friends.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

using System.Collections.Generic;

namespace SparklrLib.Objects.Responses.Work
{
public class Friends
public class Friends : List<int>
{
public int[] followers { get; set; }
public int[] following { get; set; }
public int[] recommends { get; set; }
}
}
2 changes: 1 addition & 1 deletion SparklrWP/Pages/Profile.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

<controls:PanoramaItem Header="">
<toolkit:WrapPanel>
<toolkit:HubTile Size="Large" Title="{Binding Handle}" x:Name="userProfileTile" >
<toolkit:HubTile Size="Large" Title="{Binding Handle}" x:Name="userProfileTile" Message="tap and hold to pin this user to your start screen." >
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Pin to start..." Click="PinProfileMenuItem_Click" />
Expand Down
9 changes: 6 additions & 3 deletions SparklrWP/Pages/Profile.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using SparklrLib.Objects.Responses;
using SparklrLib.Objects.Responses.Work;
using SparklrWP.Controls;
using SparklrWP.Utils;
using System;
using System.Net;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
Expand Down Expand Up @@ -219,9 +219,12 @@ private void SparklrPostControl_Tap(object sender, System.Windows.Input.GestureE
NavigationService.Navigate(new Uri("/Pages/DetailsPage.xaml?id=" + control.Post.Id, UriKind.Relative));
}

private void PinProfileMenuItem_Click(object sender, System.Windows.RoutedEventArgs e)
private async void PinProfileMenuItem_Click(object sender, System.Windows.RoutedEventArgs e)
{
Utils.TilesCreator.PinUserprofile(model.ID);
if (!await Utils.TilesCreator.PinUserprofile(model.ID))
{
MessageBox.Show("We could not create a tile for this user. Maybe he's already on your startscreen?", "We're sorry :(", MessageBoxButton.OK);
}
}
}
}
2 changes: 1 addition & 1 deletion SparklrWP/Pages/SearchPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void searchTextBox_ActionIconTapped(object sender, System.EventArgs e)

private void UserStackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
FriendViewModel i = ((StackPanel)sender).DataContext as FriendViewModel;
UserItemViewModel i = ((StackPanel)sender).DataContext as UserItemViewModel;
if (i != null)
{
NavigationService.Navigate(new System.Uri("/Pages/Profile.xaml?userId=" + i.Id.ToString(), System.UriKind.Relative));
Expand Down
10 changes: 5 additions & 5 deletions SparklrWP/SampleData/SearchViewModelSampleData.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SparklrWP" Keyword="Searchstring">
<local:SearchViewModel.Users>
<local:FriendViewModel Name="Test1" />
<local:FriendViewModel Name="Test2" />
<local:FriendViewModel Name="Test3" />
<local:FriendViewModel Name="Test4" />
<local:FriendViewModel Name="Test5" />
<local:UserItemViewModel Name="Test1" />
<local:UserItemViewModel Name="Test2" />
<local:UserItemViewModel Name="Test3" />
<local:UserItemViewModel Name="Test4" />
<local:UserItemViewModel Name="Test5" />
</local:SearchViewModel.Users>
</local:SearchViewModel>
2 changes: 1 addition & 1 deletion SparklrWP/SparklrWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
<Compile Include="Utils\Task.cs" />
<Compile Include="Utils\TilesCreator.cs" />
<Compile Include="ViewModels\CommentModel.cs" />
<Compile Include="ViewModels\FriendViewModel.cs" />
<Compile Include="ViewModels\UserItemViewModel.cs" />
<Compile Include="ViewModels\InboxViewModel.cs" />
<Compile Include="ViewModels\ChatViewModel.cs" />
<Compile Include="ViewModels\SearchViewModel.cs" />
Expand Down
18 changes: 9 additions & 9 deletions SparklrWP/Utils/GroupedObservableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@ protected override void InsertItem(int index, T item)

static class CollectionExtensions
{
public static ObservableCollectionWithItemNotification<GroupedObservableCollection<FriendViewModel>> GroupFriends(this ObservableCollectionWithItemNotification<FriendViewModel> initialCollection)
public static ObservableCollectionWithItemNotification<GroupedObservableCollection<UserItemViewModel>> GroupFriends(this ObservableCollectionWithItemNotification<UserItemViewModel> initialCollection)
{
ObservableCollectionWithItemNotification<GroupedObservableCollection<FriendViewModel>> grouped = new ObservableCollectionWithItemNotification<GroupedObservableCollection<FriendViewModel>>();
ObservableCollectionWithItemNotification<GroupedObservableCollection<UserItemViewModel>> grouped = new ObservableCollectionWithItemNotification<GroupedObservableCollection<UserItemViewModel>>();

// sort the input
List<FriendViewModel> sorted = (from friend in initialCollection orderby friend.Name select friend).ToList<FriendViewModel>();
List<UserItemViewModel> sorted = (from friend in initialCollection orderby friend.Name select friend).ToList<UserItemViewModel>();

string alphabet = "#abcdefghijklmnopqrstuvwxyz";
GroupedObservableCollection<FriendViewModel> tmp;
GroupedObservableCollection<UserItemViewModel> tmp;


foreach (char c in alphabet)
{
tmp = new GroupedObservableCollection<FriendViewModel>(c.ToString());
tmp = new GroupedObservableCollection<UserItemViewModel>(c.ToString());

List<FriendViewModel> friendStartingWithLetter = (from friend in sorted where friend.Name.StartsWith(c.ToString(), System.StringComparison.InvariantCultureIgnoreCase) select friend).ToList<FriendViewModel>();
List<UserItemViewModel> friendStartingWithLetter = (from friend in sorted where friend.Name.StartsWith(c.ToString(), System.StringComparison.InvariantCultureIgnoreCase) select friend).ToList<UserItemViewModel>();

foreach (FriendViewModel f in friendStartingWithLetter)
foreach (UserItemViewModel f in friendStartingWithLetter)
tmp.Add(f);

grouped.Add(tmp);
Expand All @@ -69,11 +69,11 @@ public static ObservableCollectionWithItemNotification<GroupedObservableCollecti
return grouped;
}

public static void AddFriend(this ObservableCollection<GroupedObservableCollection<FriendViewModel>> collection, FriendViewModel f)
public static void AddFriend(this ObservableCollection<GroupedObservableCollection<UserItemViewModel>> collection, UserItemViewModel f)
{
string firstLetter = f.Name[0].ToString();

foreach (GroupedObservableCollection<FriendViewModel> c in collection)
foreach (GroupedObservableCollection<UserItemViewModel> c in collection)
{
if (string.Compare(firstLetter, c.Title, System.StringComparison.InvariantCultureIgnoreCase) == 0)
{
Expand Down
28 changes: 9 additions & 19 deletions SparklrWP/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ public sealed class MainViewModel : INotifyPropertyChanged, IDisposable
public MainViewModel()
{
Items = new ObservableCollectionWithItemNotification<PostItemViewModel>();
GroupedItems = (new ObservableCollectionWithItemNotification<FriendViewModel>()).GroupFriends();
GroupedItems = (new ObservableCollectionWithItemNotification<UserItemViewModel>()).GroupFriends();

streamUpdater = new Timer(streamUpdater_Tick, null, Timeout.Infinite, Timeout.Infinite);

//We do not start the updater here. It will be started by the callback of the reponse
//Warning: Possible issue where a internet conenction is not stable

loadData();
//TODO: Fix friends
//loadFriends();
loadFriends();
}

/// <summary>
Expand Down Expand Up @@ -253,24 +252,15 @@ private async void loadFriends()
{
List<int> friends = new List<int>();

foreach (int id in fargs.Object.followers)
foreach (int id in fargs.Object)
{
friends.Add(id);
}

foreach (int id in fargs.Object.following)
{
if (!friends.Contains(id)) friends.Add(id);
}

JSONRequestEventArgs<SparklrLib.Objects.Responses.Work.Username[]> uargs = await App.Client.GetUsernamesAsync(friends.ToArray());
foreach (int id in friends)
{
AddFriend(new FriendViewModel(id)
{
Name = App.Client.Usernames.ContainsKey(id) ? App.Client.Usernames[id] : "User " + id,
Image = "http://d.sparklr.me/i/t" + id + ".jpg"
});
AddFriend(new UserItemViewModel(id, App.Client.Usernames[id], "http://d.sparklr.me/i/t" + id + ".jpg"));
}

#if DEBUG
Expand All @@ -279,15 +269,15 @@ private async void loadFriends()
if (group.HasItems)
{
App.logger.log("Group {0} has the following entries:", group.Title);
App.logger.log(String.Join<FriendViewModel>(", ", group.ToArray()));
App.logger.log(String.Join<UserItemViewModel>(", ", group.ToArray()));
}
}
#endif
}
}

ObservableCollectionWithItemNotification<GroupedObservableCollection<FriendViewModel>> _groupedItems;
public ObservableCollectionWithItemNotification<GroupedObservableCollection<FriendViewModel>> GroupedItems
ObservableCollectionWithItemNotification<GroupedObservableCollection<UserItemViewModel>> _groupedItems;
public ObservableCollectionWithItemNotification<GroupedObservableCollection<UserItemViewModel>> GroupedItems
{
get
{
Expand Down Expand Up @@ -341,7 +331,7 @@ private set
}
}

public void AddFriend(FriendViewModel f)
public void AddFriend(UserItemViewModel f)
{
GroupedItems.AddFriend(f);
}
Expand Down Expand Up @@ -369,7 +359,7 @@ private void Dispose(bool disposing)

public void RefreshFriends()
{
GroupedItems = (new ObservableCollectionWithItemNotification<FriendViewModel>()).GroupFriends();
GroupedItems = (new ObservableCollectionWithItemNotification<UserItemViewModel>()).GroupFriends();
loadFriends();
}
}
Expand Down
10 changes: 3 additions & 7 deletions SparklrWP/ViewModels/SearchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public SearchViewModel()
{
}

private ObservableCollection<FriendViewModel> users = new ObservableCollection<FriendViewModel>();
public ObservableCollection<FriendViewModel> Users
private ObservableCollection<UserItemViewModel> users = new ObservableCollection<UserItemViewModel>();
public ObservableCollection<UserItemViewModel> Users
{
get
{
Expand Down Expand Up @@ -95,11 +95,7 @@ public async void Search()
if (results.Object.users != null)
foreach (SearchUser user in results.Object.users)
{
Users.Add(new FriendViewModel(user.id)
{
Name = user.username,
Image = "http://d.sparklr.me/i/t" + user.id + ".jpg"
});
Users.Add(new UserItemViewModel(user.id, user.username, "http://d.sparklr.me/i/t" + user.id + ".jpg"));
}

if (results.Object.posts != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
using System;
using SparklrLib.Objects;
using SparklrLib.Objects.Responses.Work;
using System;
using System.ComponentModel;

namespace SparklrWP
{
public class FriendViewModel : INotifyPropertyChanged
public class UserItemViewModel : INotifyPropertyChanged
{
public FriendViewModel(int Id)
public UserItemViewModel(int Id)
{
this.Id = Id;
this.Image = "http://d.sparklr.me/i/t" + Id + ".jpg";
loadUserdata();
}

private async void loadUserdata()
{
JSONRequestEventArgs<Username[]> result = await App.Client.GetUsernamesAsync(new int[] { Id });

if (result.IsSuccessful && result.Object.Length > 0)
{
this.Name = result.Object[0].username;
}
}

public UserItemViewModel(int Id, string Name = null, string Image = null, bool isOnline = false)
{
this.Id = Id;
this.Name = Name;
this.Image = Image;
this.IsOnline = isOnline;
}

//For design
public FriendViewModel()
public UserItemViewModel()
{
}

Expand All @@ -22,7 +44,7 @@ public string Name
{
return _name;
}
set
private set
{
if (value != _name)
{
Expand All @@ -31,14 +53,15 @@ public string Name
}
}
}

private string _image;
public string Image
{
get
{
return _image;
}
set
private set
{
if (value != _image)
{
Expand All @@ -47,14 +70,15 @@ public string Image
}
}
}

private bool _isOnline;
public bool IsOnline
{
get
{
return _isOnline;
}
set
private set
{
if (value != _isOnline)
{
Expand All @@ -79,6 +103,7 @@ private set
}
}
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
Expand Down

0 comments on commit ccac90d

Please sign in to comment.