Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Added new PoGoBaseViewModel #1616

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PokemonGo-UWP/PokemonGo-UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@
<Compile Include="ViewModels\GameMapPageViewModel.cs" />
<Compile Include="ViewModels\AchievementDetailPageViewModel.cs" />
<Compile Include="ViewModels\PlayerProfileViewModel.cs" />
<Compile Include="ViewModels\base\PoGoViewModelBase.cs" />
<Compile Include="ViewModels\PokedexDetailViewModel.cs" />
<Compile Include="ViewModels\PokedexPageViewModel.cs" />
<Compile Include="ViewModels\PokemonDetailsPageViewModel.cs" />
Expand Down
29 changes: 1 addition & 28 deletions PokemonGo-UWP/ViewModels/GameMapPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace PokemonGo_UWP.ViewModels
{
public class GameMapPageViewModel : ViewModelBase
public class GameMapPageViewModel : PoGoViewModelBase
{

public GameMapPageViewModel()
Expand Down Expand Up @@ -139,16 +139,6 @@ public override async Task OnNavigatedFromAsync(IDictionary<string, object> susp

#region Game Management Vars

/// <summary>
/// Player's profile, we use it just for the username
/// </summary>
private PlayerData _playerProfile;

/// <summary>
/// Stats for the current player, including current level and experience related stuff
/// </summary>
private PlayerStats _playerStats;

/// <summary>
/// Response to the level up event
/// </summary>
Expand All @@ -175,23 +165,6 @@ public ElementTheme CurrentTheme
/// </summary>
public string MapServiceToken => ApplicationKeys.MapServiceToken;

/// <summary>
/// Player's profile, we use it just for the username
/// </summary>
public PlayerData PlayerProfile
{
get { return _playerProfile; }
private set { Set(ref _playerProfile, value); }
}

/// <summary>
/// Stats for the current player, including current level and experience related stuff
/// </summary>
public PlayerStats PlayerStats
{
get { return _playerStats; }
private set { Set(ref _playerStats, value); }
}

/// <summary>
/// Response to the level up event
Expand Down
78 changes: 2 additions & 76 deletions PokemonGo-UWP/ViewModels/PlayerProfileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
using System.Threading.Tasks;
using Windows.UI.Xaml.Navigation;
using PokemonGo_UWP.Utils;
using POGOProtos.Data;
using POGOProtos.Data.Player;
using POGOProtos.Enums;
using Template10.Mvvm;
using Template10.Services.NavigationService;
using Windows.UI.Xaml.Controls;
using Newtonsoft.Json;
using PokemonGo_UWP.Views;
using Template10.Common;
using Google.Protobuf;

namespace PokemonGo_UWP.ViewModels
{
public class PlayerProfileViewModel : ViewModelBase
public class PlayerProfileViewModel : PoGoViewModelBase
{
#region Lifecycle Handlers

Expand All @@ -30,84 +25,15 @@ public class PlayerProfileViewModel : ViewModelBase
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode,
IDictionary<string, object> suspensionState)
{
if (suspensionState.Any())
{
// Recovering the state
PlayerProfile = new PlayerData();
PlayerStats = new PlayerStats();
PlayerProfile.MergeFrom(ByteString.FromBase64((string)suspensionState[nameof(PlayerProfile)]).CreateCodedInput());
PlayerStats.MergeFrom(ByteString.FromBase64((string)suspensionState[nameof(PlayerStats)]).CreateCodedInput());
RaisePropertyChanged(() => PlayerProfile);
RaisePropertyChanged(() => PlayerStats);
}
else
{
// No saved state, get them from the client
PlayerProfile = GameClient.PlayerProfile;
PlayerStats = GameClient.PlayerStats;
}
await base.OnNavigatedToAsync(parameter, mode, suspensionState);
ReadPlayerStatsValues();
await Task.CompletedTask;
}

/// <summary>
/// Save state before navigating
/// </summary>
/// <param name="suspensionState"></param>
/// <param name="suspending"></param>
/// <returns></returns>
public override async Task OnNavigatedFromAsync(IDictionary<string, object> suspensionState, bool suspending)
{
if (suspending)
{
suspensionState[nameof(PlayerProfile)] = PlayerProfile.ToByteString().ToBase64();
suspensionState[nameof(PlayerStats)] = PlayerStats.ToByteString().ToBase64();
}
await Task.CompletedTask;
}

public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
args.Cancel = false;
await Task.CompletedTask;
}

#endregion

#region Game Management Vars

/// <summary>
/// Player's profile, we use it just for the username
/// </summary>
private PlayerData _playerProfile;

/// <summary>
/// Stats for the current player, including current level and experience related stuff
/// </summary>
private PlayerStats _playerStats;

#endregion

#region Bindable Game Vars

/// <summary>
/// Player's profile, we use it just for the username
/// </summary>
public PlayerData PlayerProfile
{
get { return _playerProfile; }
set { Set(ref _playerProfile, value); }
}

/// <summary>
/// Stats for the current player, including current level and experience related stuff
/// </summary>
public PlayerStats PlayerStats
{
get { return _playerStats; }
set { Set(ref _playerStats, value); }
}

public ObservableCollection<KeyValuePair<AchievementType, object>> Achievements { get; } =
new ObservableCollection<KeyValuePair<AchievementType, object>>();

Expand Down
16 changes: 1 addition & 15 deletions PokemonGo-UWP/ViewModels/PokemonInventoryPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@

namespace PokemonGo_UWP.ViewModels
{
public class PokemonInventoryPageViewModel : ViewModelBase
public class PokemonInventoryPageViewModel : PoGoViewModelBase
{
#region Game Management Vars

/// <summary>
/// Player's profile, we use it just for the username
/// </summary>
private PlayerData _playerProfile;

/// <summary>
/// Egg selected for incubation
/// </summary>
Expand Down Expand Up @@ -123,15 +118,6 @@ public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)

#region Bindable Game Vars

/// <summary>
/// Player's profile, we use it just for the username
/// </summary>
public PlayerData PlayerProfile
{
get { return _playerProfile; }
set { Set(ref _playerProfile, value); }
}

/// <summary>
/// Sorting mode for current Pokemon view
/// </summary>
Expand Down
102 changes: 102 additions & 0 deletions PokemonGo-UWP/ViewModels/base/PoGoViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Google.Protobuf;
using POGOProtos.Data;
using POGOProtos.Data.Player;
using PokemonGo_UWP.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Template10.Mvvm;
using Template10.Services.NavigationService;
using Windows.UI.Xaml.Navigation;

namespace PokemonGo_UWP.ViewModels {
/// <summary>
/// This Base View Models handles basic implementations which are used in many other ViewModels.
/// </summary>
public abstract class PoGoViewModelBase : ViewModelBase {
#region Lifecycle Handlers

/// <summary>
/// </summary>
/// <param name="parameter"></param>
/// <param name="mode"></param>
/// <param name="suspensionState"></param>
/// <returns></returns>
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode,
IDictionary<string, object> suspensionState) {

if(suspensionState.Any()) {
// Recovering the state
PlayerProfile = new PlayerData();
PlayerStats = new PlayerStats();
PlayerProfile.MergeFrom(ByteString.FromBase64((string)suspensionState[nameof(PlayerProfile)]).CreateCodedInput());
PlayerStats.MergeFrom(ByteString.FromBase64((string)suspensionState[nameof(PlayerStats)]).CreateCodedInput());
RaisePropertyChanged(() => PlayerProfile);
RaisePropertyChanged(() => PlayerStats);
} else {
// No saved state, get them from the client
PlayerProfile = GameClient.PlayerProfile;
PlayerStats = GameClient.PlayerStats;
}

await Task.CompletedTask;
}

/// <summary>
/// Save state before navigating
/// </summary>
/// <param name="suspensionState"></param>
/// <param name="suspending"></param>
/// <returns></returns>
public override async Task OnNavigatedFromAsync(IDictionary<string, object> suspensionState, bool suspending) {
if(suspending) {
suspensionState[nameof(PlayerProfile)] = PlayerProfile.ToByteString().ToBase64();
suspensionState[nameof(PlayerStats)] = PlayerStats.ToByteString().ToBase64();
}
await Task.CompletedTask;
}

public override async Task OnNavigatingFromAsync(NavigatingEventArgs args) {
args.Cancel = false;
await Task.CompletedTask;
}

#endregion

#region Game Management Vars

/// <summary>
/// Player's profile, we use it just for the username
/// </summary>
private PlayerData _playerProfile;

/// <summary>
/// Stats for the current player, including current level and experience related stuff
/// </summary>
private PlayerStats _playerStats;

#endregion

#region Bindable GameVars

/// <summary>
/// Player's profile, we use it just for the username
/// </summary>
public PlayerData PlayerProfile
{
get { return _playerProfile; }
protected set { Set(ref _playerProfile, value); }
}

/// <summary>
/// Stats for the current player, including current level and experience related stuff
/// </summary>
public PlayerStats PlayerStats
{
get { return _playerStats; }
protected set { Set(ref _playerStats, value); }
}

#endregion
}
}