public MonstersViewModel() { Title = "Monsters List"; Dataset = new ObservableCollection <Creature>(); LoadDataCommand = new Command(async() => await ExecuteLoadDataCommand()); MessagingCenter.Subscribe <DeleteMonsterPage, Creature>(this, "DeleteData", async(obj, data) => { Dataset.Remove(data); await DataStore.DeleteAsync_Creature(data); }); MessagingCenter.Subscribe <CreateMonster, Creature>(this, "AddData", async(obj, data) => { Dataset.Add(data); await DataStore.AddAsync_Creature(data); }); MessagingCenter.Subscribe <EditMonsterPage, Creature>(this, "EditData", async(obj, data) => { // Find the Item, then update it var myData = Dataset.FirstOrDefault(arg => arg.Id == data.Id); if (myData == null) { return; } myData.Update(data); await DataStore.UpdateAsync_Creature(myData); _needsRefresh = true; }); }
public CharactersViewModel() { Title = "Characters List"; Dataset = new ObservableCollection <Creature>(); LoadDataCommand = new Command(async() => await ExecuteLoadDataCommand()); //loading the characters //subsribing to methods which are called from views MessagingCenter.Subscribe <DeleteCharacter, Creature>(this, "DeleteData", async(obj, data) => { //deleting data from the dataset Dataset.Remove(data); await DataStore.DeleteAsync_Creature(data); }); //subsribing to methods which are called from views MessagingCenter.Subscribe <NewCharacter, Creature>(this, "AddData", async(obj, data) => { //adding the data to dataset Dataset.Add(data); await DataStore.AddAsync_Creature(data); }); //subsribing to methods which are called from views MessagingCenter.Subscribe <TeamViewModel, Creature>(this, "AddData", async(obj, data) => { //adding the data in team dataset Dataset.Add(data); await DataStore.AddAsync_Creature(data); }); //subsribing to methods which are called from views MessagingCenter.Subscribe <EditCharacter, Creature>(this, "EditData", async(obj, data) => { // Find the Item, then update it var myData = Dataset.FirstOrDefault(arg => arg.Id == data.Id); if (myData == null) { return; } myData.Update(data); await DataStore.UpdateAsync_Creature(myData); _needsRefresh = true; }); }
async Task ExecuteLoadDataCommand() { if (IsBusy) { return; } IsBusy = true; try { Dataset.Clear(); //var dataset = await SQLDataStore.GetAllAsync_Creature(true); if (CharactersViewModel.Instance.Dataset.Count == 0) { CharactersViewModel.Instance.LoadDataCommand.Execute(null); } else if (CharactersViewModel.Instance.NeedsRefresh()) { CharactersViewModel.Instance.LoadDataCommand.Execute(null); } var dataset = CharactersViewModel.Instance.GetAllCreatures(); int teamCount = 0; foreach (var data in dataset) { if ((data.Type == 0) && (teamCount < 6) && data.OnTeam)//the creature is a character, the team is not full, and it is on the current team { teamCount++; Dataset.Add(data); } } if (teamCount < 6) { foreach (var data in dataset) //if the team is not full more characters must be added { if ((data.Type == 0) && (teamCount < 6) && !Dataset.Contains(data)) //the creature is a character, the team is not full, and the character is not in the team { teamCount++; data.OnTeam = true; Dataset.Add(data); } } } if (teamCount < 6)//if you didn't make enough characters you get some sucky ones { int numOfSuckyCharacters = 0; for (int i = teamCount; i < 6; i++) { numOfSuckyCharacters++; Creature character = new Creature(); character.Type = 0; character.OnTeam = true; character.Name = "Sucky Character " + numOfSuckyCharacters.ToString(); character.Attack = 1; character.Defense = 1; character.Speed = 1; character.MaxHealth = 1; character.CurrHealth = character.MaxHealth; Dataset.Add(character); await DataStore.AddAsync_Creature(character); } } // ***temp for demo*** foreach (var data in Dataset) { data.RHandItemID = "bow"; data.LHandItemID = "bow"; } } catch (Exception ex) { Debug.WriteLine(ex); } finally { IsBusy = false; } }