Skip to content

Instantly share code, notes, and snippets.

@RosiersRobin
Created November 26, 2017 20:58
Show Gist options
  • Save RosiersRobin/c6ca9ddc371abcc9f2d71e1ecd74b251 to your computer and use it in GitHub Desktop.
Save RosiersRobin/c6ca9ddc371abcc9f2d71e1ecd74b251 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WunderlistApp.Model;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace WunderlistApp.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : TabbedPage
{
ObservableCollection<Folder> ObservedFoldersList;
ObservableCollection<Lists> ObservedListsList;
public MainPage()
{
GeneratePage();
InitializeComponent();
// Set different colors :D
this.BarBackgroundColor = Color.FromHex("#5b7a59");
this.BarTextColor = Color.FromHex("#ffffff");
this.BackgroundColor = Color.FromHex("#f7f7f7");
}
private async Task GeneratePage()
{
List<Folder> folders = await ApiManager.GetFoldersAsync();
ObservedFoldersList = new ObservableCollection<Folder>(folders);
// TEMPLATE
// Make a template for the view
var template = new DataTemplate(typeof(TextCell));
// Create a text-field (same as <Label /> in xaml).
// title is here the property of the class Lists
template.SetBinding(TextCell.TextProperty, "title");
// Set a color to the label.
template.SetValue(TextCell.TextColorProperty, Color.FromHex("#212121"));
// Make the splashscreen
ActivityIndicator actInd = new ActivityIndicator
{
IsRunning = true,
IsEnabled = true,
VerticalOptions = LayoutOptions.Center
};
ContentPage loadingPage = new ContentPage
{
Content = new Grid
{
Children =
{
actInd
}
}
};
// Generate the page
this.Children.Add(loadingPage);
// Loop trough the folders with var f
foreach (var f in ObservedFoldersList)
{
// Make a new List object for the lists in a folder
List<Lists> lists = new List<Lists>();
ObservedListsList = new ObservableCollection<Lists>(lists);
// Loop trough the ID's of the lists
foreach (int id in f.list_ids)
{
// Add the lists to the lists object using an async link.
ObservedListsList.Add(await ApiManager.GetSpecificListAsync(id));
}
// listview for the ContentPage
ListView lvwLists = new ListView
{
ItemsSource = ObservedListsList,
SeparatorVisibility = SeparatorVisibility.None,
// Here, we specify the template to be used (it's defined above of this) under the 'TEMPLATE' tag.
ItemTemplate = template
};
Button btn_add_new_list = new Button
{
Text = "Add a new list",
BackgroundColor = Color.FromHex("#5b7a59"),
TextColor = Color.FromHex("#FAFAFA"),
BorderRadius = 0,
Margin = new Thickness(20),
CommandParameter = f
};
// Event listeners
lvwLists.ItemSelected += LvwLists_ItemSelected;
btn_add_new_list.Clicked += Btn_add_new_list_Clicked;
ContentPage page = new ContentPage
{
Title = f.title,
Content = new StackLayout
{
Children =
{
lvwLists,
btn_add_new_list
}
}
};
// Generate the page
this.Children.Add(page);
}
this.Children.Remove(loadingPage);
actInd.IsRunning = false;
}
// Selected event, this will pass the data to the new page and there we can select the data to make a detail :)
private void LvwLists_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
ListView lv = sender as ListView;
//Lists selected = lvwLists.SelectedItem as Lists;
Lists selected = lv.SelectedItem as Lists;
if (selected != null)
{
// DisplayAlert("ID item Selected: ", selected.id.ToString(), "Ok");
Navigation.PushAsync(new DetailListPage(selected.id, selected.title));
// disable the visual selection state
((ListView)sender).SelectedItem = null;
}
}
private void Btn_add_new_list_Clicked(object sender, EventArgs e)
{
// DisplayAlert("Whoops!", "Ahw, something went wrong! Can't make a new item, because the API screwed up...", "Ok");
Folder selected_folder = (sender as Button).CommandParameter as Folder;
Debug.WriteLine(selected_folder);
Navigation.PushAsync(new AddListPage(selected_folder));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment