Skip to content

Instantly share code, notes, and snippets.

@jgable
Created March 24, 2011 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgable/886012 to your computer and use it in GitHub Desktop.
Save jgable/886012 to your computer and use it in GitHub Desktop.
HackatopiaALL - All TeamServices and VM's for pivot win phone 7 app
using System;
using System.Windows;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Collections.Generic;
#region TeamServices
/// <summary>
/// Our Hello response object; /Index action
/// </summary>
public class HelloWorldResponse
{
public bool Success { get; set; }
public DateTime Time { get; set; }
public string Message { get; set; }
}
/// <summary>
/// Team information; copied from Web project Models.
/// </summary>
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Captain { get; set; }
public List<string> MemberNames { get; set; }
public Team()
{
MemberNames = new List<string>();
}
}
/// <summary>
/// Our Team List info; /List Action
/// </summary>
public class TeamListResponse
{
public bool Success { get; set; }
public List<Team> Teams { get; set; }
}
/// <summary>
/// Our Single Team info; /Id/1 Action
/// </summary>
public class TeamGetResponse
{
public bool Success { get; set; }
public Team Team { get; set; }
}
/// <summary>
/// Our Search response info; /Search/blah
/// </summary>
public class TeamSearchResponse
{
public bool Success { get; set; }
public List<Team> Teams { get; set; }
}
public class HelloWorldService : JsonService<HelloWorldResponse>
{
private static string Url = "http://localhost:59527/Team";
public void GetHello(Action<HelloWorldResponse> onResult, Action<Exception> onError)
{
StartServiceCall(Url, onResult, onError);
}
}
public class TeamListService : JsonService<TeamListResponse>
{
private static string Url = "http://localhost:59527/Team/List";
public void GetTeams(Action<TeamListResponse> onResult, Action<Exception> onError)
{
StartServiceCall(Url, onResult, onError);
}
}
public class TeamGetService : JsonService<TeamGetResponse>
{
private static string Url = "http://localhost:59527/Team/Id/{0}";
public void GetTeam(int id, Action<TeamGetResponse> onResult, Action<Exception> onError)
{
StartServiceCall(string.Format(Url, id), onResult, onError);
}
}
public class TeamSearchService : JsonService<TeamSearchResponse>
{
private static string Url = "http://localhost:59527/Team/Search/{0}";
public void SearchTeams(string searchTerm, Action<TeamSearchResponse> onResult, Action<Exception> onError)
{
StartServiceCall(string.Format(Url, searchTerm), onResult, onError);
}
}
#endregion TeamServices
#region MVVM
public class HelloWorldVM : ViewModelBase
{
private HelloWorldService _helloServ = new HelloWorldService();
private string message;
public string Message
{
get { return message; }
set
{
if (message != value)
{
message = value;
OnPropertyChanged("Message");
}
}
}
private DateTime serverTime;
public DateTime ServerTime
{
get { return serverTime; }
set
{
if (serverTime != value)
{
serverTime = value;
OnPropertyChanged("ServerTime");
}
}
}
private bool success;
public bool Success
{
get { return success; }
set
{
if (success != value)
{
success = value;
OnPropertyChanged("Success");
}
}
}
public ICommand HelloCmd { get; private set; }
public HelloWorldVM()
{
HelloCmd = new DelegateCommand<object>(x => HandleHelloCommand());
}
private void HandleHelloCommand()
{
_helloServ.GetHello(
resp =>
{
this.Success = resp.Success;
this.Message = resp.Message;
this.ServerTime = resp.Time;
},
err =>
{
MessageBox.Show("Not cool, bro", "Error'd", MessageBoxButton.OK);
});
}
}
public class MainPageVM : ViewModelBase
{
public HelloWorldVM HelloWorld { get; set; }
public TeamListVM TeamList { get; set; }
public TeamInfoVM TeamInfo { get; set; }
public TeamSearchVM TeamSearch { get; set; }
public MainPageVM()
{
HelloWorld = new HelloWorldVM();
TeamList = new TeamListVM();
TeamInfo = new TeamInfoVM();
TeamSearch = new TeamSearchVM();
}
}
public class TeamListVM : ViewModelBase
{
private TeamListService _listServ = new TeamListService();
private ObservableCollection<Team> results;
public ObservableCollection<Team> Results
{
get { return results; }
set
{
if (results != value)
{
results = value;
OnPropertyChanged("Results");
}
}
}
public ICommand Retrieve { get; private set; }
public TeamListVM()
{
Retrieve = new DelegateCommand<object>(x => HandleRetrieve());
Results = new ObservableCollection<Team>();
}
private void HandleRetrieve()
{
Results.Clear();
_listServ.GetTeams(
resp =>
{
resp.Teams.ForEach(t => Results.Add(t));
},
err =>
{
MessageBox.Show("Not cool, bro", "Error'd", MessageBoxButton.OK);
});
}
}
public class TeamInfoVM : ViewModelBase
{
private TeamGetService _teamServ = new TeamGetService();
private string name;
public string Name
{
get { return name; }
set
{
if (name != value)
{
name = value;
OnPropertyChanged("Name");
}
}
}
private string description;
public string Description
{
get { return description; }
set
{
if (description != value)
{
description = value;
OnPropertyChanged("Description");
}
}
}
private string captain;
public string Captain
{
get { return captain; }
set
{
if (captain != value)
{
captain = value;
OnPropertyChanged("Captain");
}
}
}
private ObservableCollection<string> members;
public ObservableCollection<string> Members
{
get { return members; }
set
{
if (members != value)
{
members = value;
OnPropertyChanged("Members");
}
}
}
public ICommand GetInfo { get; private set; }
public TeamInfoVM()
{
GetInfo = new DelegateCommand<SCommandArgs>(args => HandleGetInfo(args.CommandParameter.ToString()));
Members = new ObservableCollection<string>();
}
private void HandleGetInfo(string id)
{
int teamId;
if(!Int32.TryParse(id, out teamId))
return;
_teamServ.GetTeam(teamId,
resp =>
{
if (resp == null || !resp.Success || resp.Team == null)
{
MessageBox.Show("Team Not Found", "Invalid", MessageBoxButton.OK);
return;
}
this.Name = resp.Team.Name;
this.Description = resp.Team.Description;
this.Captain = resp.Team.Captain;
this.Members.Clear();
resp.Team.MemberNames.ForEach(n => this.Members.Add(n));
},
err =>
{
MessageBox.Show("Not cool, bro", "Error'd", MessageBoxButton.OK);
});
}
}
public class TeamSearchVM : ViewModelBase
{
private TeamSearchService _searchServ = new TeamSearchService();
private ObservableCollection<Team> results;
public ObservableCollection<Team> Results
{
get { return results; }
set
{
if (results != value)
{
results = value;
OnPropertyChanged("Results");
}
}
}
public ICommand Search { get; private set; }
public TeamSearchVM()
{
Search = new DelegateCommand<SCommandArgs>(args => HandleSearch(args.CommandParameter.ToString()));
Results = new ObservableCollection<Team>();
}
private void HandleSearch(string term)
{
if (String.IsNullOrEmpty(term))
return;
Results.Clear();
_searchServ.SearchTeams(term,
resp =>
{
if (resp == null || !resp.Success || resp.Teams == null)
{
MessageBox.Show("Nothing found", "Empty", MessageBoxButton.OK);
return;
}
resp.Teams.ForEach(t => Results.Add(t));
},
err =>
{
MessageBox.Show("Not cool, bro", "Error'd", MessageBoxButton.OK);
});
}
}
#endregion
<Grid
x:Name="LayoutRoot"
Background="Transparent">
<!--Pivot Control -->
<!-- xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls" -->
<controls:Pivot
x:Name="pivot"
Title="HACKATOPIA DEMO">
<!-- Hello World Service -->
<controls:PivotItem
DataContext="{Binding HelloWorld}"
Header="hello">
<Grid>
<TextBlock
HorizontalAlignment="Left"
VerticalAlignment="Top"
Style="{StaticResource PhoneTextSubtleStyle}"
Text="{Binding ServerTime}" />
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Style="{StaticResource PhoneTextLargeStyle}"
Text="{Binding Message}" />
<Button
VerticalAlignment="Bottom"
Content="Say Hello"
cmd:SingleEventCommand.RoutedEventName="Click"
cmd:SingleEventCommand.TheCommandToRun="{Binding HelloCmd}" />
</Grid>
</controls:PivotItem>
<!-- Team List -->
<controls:PivotItem
DataContext="{Binding TeamList}"
Header="list">
<Grid>
<ListBox
x:Name="TeamList"
SelectionChanged="TeamList_SelectionChanged"
ItemsSource="{Binding Results}"
Margin="0 0 0 80">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock
Style="{StaticResource PhoneTextLargeStyle}"
Text="{Binding Name}" />
<TextBlock
Margin="15 5 10 10"
Style="{StaticResource PhoneTextSmallStyle}"
TextWrapping="Wrap"
Text="{Binding Description}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button
Content="Load Teams"
VerticalAlignment="Bottom"
cmd:SingleEventCommand.TheCommandToRun="{Binding Retrieve}"
cmd:SingleEventCommand.RoutedEventName="Click" />
</Grid>
</controls:PivotItem>
<!-- Team Id -->
<controls:PivotItem
x:Name="teamPivot"
DataContext="{Binding TeamInfo}"
Header="team">
<Grid>
<StackPanel>
<TextBlock
Text="{Binding Name}"
Style="{StaticResource PhoneTextLargeStyle}" />
<TextBlock
Margin="15 5 10 10"
Style="{StaticResource PhoneTextSmallStyle}"
TextWrapping="Wrap"
Text="{Binding Description}" />
<TextBlock
Text="MEMBERS"
FontWeight="Bold"
Margin="0 10 0 5"></TextBlock>
<ItemsControl
ItemsSource="{Binding Members}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding}"
Style="{StaticResource PhoneTextSubtleStyle}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</controls:PivotItem>
<!-- Search -->
<controls:PivotItem
DataContext="{Binding TeamSearch}"
Header="search">
<Grid>
<TextBox
x:Name="searchTxt"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
Margin="0 0 160 0"></TextBox>
<Button
Content="Search"
Width="150"
VerticalAlignment="Top"
HorizontalAlignment="Right"
cmd:SingleEventCommand.TheCommandToRun="{Binding Search}"
cmd:SingleEventCommand.CommandParameter="{Binding ElementName=searchTxt, Path=Text}"
cmd:SingleEventCommand.RoutedEventName="Click" />
<ListBox
x:Name="SearchList"
SelectionChanged="TeamList_SelectionChanged"
ItemsSource="{Binding Results}"
Margin="0 75 0 0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock
Style="{StaticResource PhoneTextLargeStyle}"
Text="{Binding Name}" />
<TextBlock
Margin="15 5 10 10"
Style="{StaticResource PhoneTextSmallStyle}"
TextWrapping="Wrap"
Text="{Binding Description}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
</controls:Pivot>
</Grid>
using Microsoft.Phone.Controls;
public partial class MainPage : PhoneApplicationPage
{
private MainPageVM ViewModel;
// Constructor
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
ViewModel = new MainPageVM();
this.DataContext = ViewModel;
}
private void TeamList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (e.AddedItems == null || e.AddedItems.Count < 1)
return;
var selected = e.AddedItems[0] as Team;
pivot.SelectedItem = teamPivot;
ViewModel.TeamInfo.GetInfo.Execute(new SCommandArgs(null, e, selected.Id));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment