/***************************************************** * Opponent Section of Database *****************************************************/ // Save or update opponent public int SaveOpponent(Opponents opponent) { if (opponent.ID != 0) { return(database.Update(opponent)); } else { return(database.Insert(opponent)); } }
/** * This function will allow us to be able to dispaly the names properly. * I does so since the object hasn't been bound on click, so it would normally * return null. Using OnBindingContextChanged allows us to use an * object to get results. * */ protected override void OnBindingContextChanged() { base.OnBindingContextChanged(); Matches match = (Matches)this.BindingContext; if (match != null) { Opponents opponent = database.GetOpponent(match.opponent_id); Games game = database.GetGame(match.mGameID); lblFullName.Text = opponent.oFirstName + " " + opponent.oLastName; lblGameName.Text = game.gName; } }
/** * This page will display current matches and add ability to add a new match * */ public NewMatchesPage(Opponents opponent) { InitializeComponent(); database = App.Database; Title = "Opponents"; // Layout for top items StackLayout topLayout = new StackLayout { VerticalOptions = LayoutOptions.FillAndExpand }; // Add matches to listview ListView matchListView = new ListView { ItemsSource = database.GetMatchesByID(opponent.ID), //ItemsSource = matchList, RowHeight = 75, ItemTemplate = new DataTemplate(typeof(matchCell)), HeightRequest = 750 }; // Add to top layout topLayout.Children.Add(matchListView); /////////// Table Section Below ///////////////////// StackLayout tableLayout = new StackLayout { VerticalOptions = LayoutOptions.Center }; TableView tableView = new TableView { Intent = TableIntent.Form }; tableView.HeightRequest = 700; // Cells go in sections, sections in root DatePicker dPicker = new DatePicker { Date = DateTime.Now, Format = "D" }; ViewCell vDate = new ViewCell(); vDate.View = dPicker; // Comments section EntryCell eComments = new EntryCell { Label = "Comment: " }; // Picker for Game selection Picker pGame = new Picker { ItemsSource = database.GetAllGames(), ItemDisplayBinding = new Binding("gName"), Title = "Game:" }; // Picker must be placed in ViewCell ViewCell vPicker = new ViewCell(); vPicker.View = pGame; SwitchCell sWin = new SwitchCell { Text = "Win?" }; TableSection tableSection = new TableSection("Add Match") { vDate, eComments, vPicker, sWin }; tableView.Root = new TableRoot { tableSection }; tableLayout.Children.Add(tableView); // Create button to add matches Button btnAdd = new Button { Text = "Add", HorizontalOptions = LayoutOptions.Center }; btnAdd.Clicked += (sender, e) => { // Check to make sure that we're updating an item already in database if (currentMatch != null) { currentMatch.mDate = dPicker.Date; currentMatch.mComments = eComments.Text; currentMatch.mWin = sWin.On; currentMatch.mGameID = ((Games)pGame.SelectedItem).gID; // Update match database.SaveMatch(currentMatch); // Update list matchListView.ItemsSource = database.GetMatchesByID(currentMatch.opponent_id); currentMatch = null; } else { // Create new match to save Matches newMatch = new Matches { mDate = dPicker.Date, mComments = eComments.Text, mGameID = ((Games)pGame.SelectedItem).gID, opponent_id = opponent.ID, mWin = sWin.On }; // Save new match to database database.SaveMatch(newMatch); // Update list matchListView.ItemsSource = database.GetMatchesByID(newMatch.opponent_id); } }; matchListView.ItemTapped += (sender, e) => { matchListView.SelectedItem = null; // Set current item equal to the object which was tapped // Used to be able to know if we need to update, or create new match currentMatch = (Matches)e.Item; dPicker.Date = currentMatch.mDate; eComments.Text = currentMatch.mComments; // Index needs to be offset by one, since picker and database do not // start at the same numbers. One start at 0 and the other other 1. pGame.SelectedIndex = currentMatch.mGameID - 1; sWin.On = currentMatch.mWin; }; // Create main layout StackLayout mainLayout = new StackLayout { Children = { topLayout, tableLayout, btnAdd } }; Content = mainLayout; }
// Delete one opponent public int DeleteOpponent(Opponents opponent) { return(database.Delete(opponent)); }
/** * This page will handle adding a new opponent to the database * */ public NewOpponentPage() { InitializeComponent(); Title = "Opponents"; database = App.Database; // Main layout to add components to when finished StackLayout mainLayout = new StackLayout { VerticalOptions = LayoutOptions.FillAndExpand }; StackLayout tableLayout = new StackLayout { VerticalOptions = LayoutOptions.Center }; TableView tableView = new TableView { Intent = TableIntent.Form }; // Cells have to go in sections and sections in Root EntryCell eFirstName = new EntryCell { Label = "First Name: " }; EntryCell eLastName = new EntryCell { Label = "Last Name: " }; EntryCell eAddress = new EntryCell { Label = "Address: " }; EntryCell ePhone = new EntryCell { Label = "Phone: " }; EntryCell eEmail = new EntryCell { Label = "Email: " }; TableSection section = new TableSection("Add New Opponent") { eFirstName, eLastName, eAddress, ePhone, eEmail }; // Root tableView.Root = new TableRoot { section }; // Add to table layout tableLayout.Children.Add(tableView); Button btnSave = new Button { Text = "Save" }; btnSave.Clicked += (sender, e) => { // Create new opponent to be saved in database Opponents newOpp = new Opponents { oFirstName = eFirstName.Text, oLastName = eLastName.Text, oAddress = eAddress.Text, oPhone = ePhone.Text, oEmail = eEmail.Text }; // Persiste new opponent to database database.SaveOpponent(newOpp); // Reset string fields to empty eFirstName.Text = string.Empty; eLastName.Text = string.Empty; eAddress.Text = string.Empty; ePhone.Text = string.Empty; eEmail.Text = string.Empty; }; // Add to main layout mainLayout.Children.Add(tableLayout); mainLayout.Children.Add(btnSave); Content = mainLayout; }