<commit_before>﻿using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using Toggl.Joey.UI.Activities;
using Toggl.Joey.UI.Adapters;
using Toggl.Joey.UI.Views;
using Toggl.Phoebe.Data.DataObjects;
using Toggl.Phoebe.Data.Models;
using Toggl.Phoebe.Data.ViewModels;
using Toggl.Phoebe.Data.Views;
using ActionBar = Android.Support.V7.App.ActionBar;
using Activity = Android.Support.V7.App.AppCompatActivity;
using Fragment = Android.Support.V4.App.Fragment;
using Toolbar = Android.Support.V7.Widget.Toolbar;

namespace Toggl.Joey.UI.Fragments
{
    public class ProjectListFragment : Fragment, Toolbar.IOnMenuItemClickListener, TabLayout.IOnTabSelectedListener
    {
        private static readonly string TimeEntryIdsArg = "time_entries_ids_param";
        private static readonly int ProjectCreatedRequestCode = 1;

        private RecyclerView recyclerView;
        private TabLayout tabLayout;
        private Toolbar toolBar;
        private FloatingActionButton newProjectFab;
        private LinearLayout emptyStateLayout;

        private ProjectListViewModel viewModel;

        private IList<string> TimeEntryIds
        {
            get {
                return Arguments.GetStringArrayList (TimeEntryIdsArg);
            }
        }

        public ProjectListFragment ()
        {
        }

        public ProjectListFragment (IntPtr jref, Android.Runtime.JniHandleOwnership xfer) : base (jref, xfer)
        {
        }

        public static ProjectListFragment NewInstance (IList<string> timeEntryIds)
        {
            var fragment = new ProjectListFragment ();

            var args = new Bundle ();
            args.PutStringArrayList (TimeEntryIdsArg, timeEntryIds);
            fragment.Arguments = args;

            return fragment;
        }

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate (Resource.Layout.ProjectListFragment, container, false);

            recyclerView = view.FindViewById<RecyclerView> (Resource.Id.ProjectListRecyclerView);
            recyclerView.SetLayoutManager (new LinearLayoutManager (Activity));
            recyclerView.AddItemDecoration (new ShadowItemDecoration (Activity));
            recyclerView.AddItemDecoration (new DividerItemDecoration (Activity, DividerItemDecoration.VerticalList));

            emptyStateLayout = view.FindViewById<LinearLayout> (Resource.Id.ProjectListEmptyState);
            tabLayout = view.FindViewById<TabLayout> (Resource.Id.WorkspaceTabLayout);
            newProjectFab = view.FindViewById<AddProjectFab> (Resource.Id.AddNewProjectFAB);
            toolBar = view.FindViewById<Toolbar> (Resource.Id.ProjectListToolbar);

            var activity = (Activity)Activity;
            activity.SetSupportActionBar (toolBar);
            activity.SupportActionBar.SetDisplayHomeAsUpEnabled (true);
            activity.SupportActionBar.SetTitle (Resource.String.ChooseTimeEntryProjectDialogTitle);

            HasOptionsMenu = true;
            newProjectFab.Click += OnNewProjectFabClick;
            tabLayout.SetOnTabSelectedListener (this);

            return view;
        }

        public async override void OnViewCreated (View view, Bundle savedInstanceState)
        {
            base.OnViewCreated (view, savedInstanceState);

            viewModel = new ProjectListViewModel (TimeEntryIds);

            var adapter = new ProjectListAdapter (recyclerView, viewModel.ProjectList);
            adapter.HandleProjectSelection = OnItemSelected;
            recyclerView.SetAdapter (adapter);

            viewModel.OnIsLoadingChanged += OnDataLoaded;
            viewModel.ProjectList.OnIsLoadingChanged += OnDataLoaded;

            await viewModel.Init ();
        }

        private void OnDataLoaded (object sender, EventArgs e)
        {
            if (!viewModel.IsLoading) {
                if (viewModel.Model == null) {
                    Activity.Finish ();
                }
            }

            if (viewModel.ProjectList.IsLoading || viewModel.IsLoading) {
                return;
            }

            EnsureCorrectState ();

            // Create tabs
            if (viewModel.ProjectList.Workspaces.Count > 2) {
                int i = 0;
                foreach (var ws in viewModel.ProjectList.Workspaces) {
                    var tab = tabLayout.NewTab().SetText (ws.Data.Name);
                    tabLayout.AddTab (tab);
                    if (ws.Data.Id == viewModel.TimeEntryList[0].WorkspaceId) {
                        viewModel.ProjectList.CurrentWorkspaceIndex = i;
                        tab.Select();
                    }
                    i++;
                }
            }
        }

        private void EnsureCorrectState ()
        {
            // Set toolbar scrollable or not.
            var _params = (AppBarLayout.LayoutParams) toolBar.LayoutParameters;

            if (viewModel.ProjectList.Workspaces.Count > 2) {
                tabLayout.Visibility = ViewStates.Visible;
                _params.ScrollFlags  = AppBarLayout.LayoutParams.ScrollFlagScroll | AppBarLayout.LayoutParams.ScrollFlagEnterAlways;
            } else {
                tabLayout.Visibility = ViewStates.Gone;
                _params.ScrollFlags = 0;
            }

            toolBar.LayoutParameters = _params;
            recyclerView.Visibility = viewModel.ProjectList.IsEmpty ? ViewStates.Gone : ViewStates.Visible;
            emptyStateLayout.Visibility = viewModel.ProjectList.IsEmpty ? ViewStates.Visible : ViewStates.Gone;
        }

        private void OnNewProjectFabClick (object sender, EventArgs e)
        {
            var entryList = new List<TimeEntryData> (viewModel.TimeEntryList);

            // Show create project activity instead
            var intent = BaseActivity.CreateDataIntent<NewProjectActivity, List<TimeEntryData>>
                         (Activity, entryList, NewProjectActivity.ExtraTimeEntryDataListId);
            StartActivityForResult (intent, ProjectCreatedRequestCode);
        }

        private async void OnItemSelected (object m)
        {
            ProjectModel project = null;
            WorkspaceModel workspace = null;
            TaskData task = null;<commit_msg>Fix Cast error.<commit_after>﻿using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using Toggl.Joey.UI.Activities;
using Toggl.Joey.UI.Adapters;
using Toggl.Joey.UI.Views;
using Toggl.Phoebe.Data.DataObjects;
using Toggl.Phoebe.Data.Models;
using Toggl.Phoebe.Data.ViewModels;
using Toggl.Phoebe.Data.Views;
using ActionBar = Android.Support.V7.App.ActionBar;
using Activity = Android.Support.V7.App.AppCompatActivity;
using Fragment = Android.Support.V4.App.Fragment;
using Toolbar = Android.Support.V7.Widget.Toolbar;

namespace Toggl.Joey.UI.Fragments
{
    public class ProjectListFragment : Fragment, Toolbar.IOnMenuItemClickListener, TabLayout.IOnTabSelectedListener
    {
        private static readonly string TimeEntryIdsArg = "time_entries_ids_param";
        private static readonly int ProjectCreatedRequestCode = 1;

        private RecyclerView recyclerView;
        private TabLayout tabLayout;
        private Toolbar toolBar;
        private FloatingActionButton newProjectFab;
        private LinearLayout emptyStateLayout;

        private ProjectListViewModel viewModel;

        private IList<string> TimeEntryIds
        {
            get {
                return Arguments.GetStringArrayList (TimeEntryIdsArg);
            }
        }

        public ProjectListFragment ()
        {
        }

        public ProjectListFragment (IntPtr jref, Android.Runtime.JniHandleOwnership xfer) : base (jref, xfer)
        {
        }

        public static ProjectListFragment NewInstance (IList<string> timeEntryIds)
        {
            var fragment = new ProjectListFragment ();

            var args = new Bundle ();
            args.PutStringArrayList (TimeEntryIdsArg, timeEntryIds);
            fragment.Arguments = args;

            return fragment;
        }

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate (Resource.Layout.ProjectListFragment, container, false);

            recyclerView = view.FindViewById<RecyclerView> (Resource.Id.ProjectListRecyclerView);
            recyclerView.SetLayoutManager (new LinearLayoutManager (Activity));
            recyclerView.AddItemDecoration (new ShadowItemDecoration (Activity));
            recyclerView.AddItemDecoration (new DividerItemDecoration (Activity, DividerItemDecoration.VerticalList));

            emptyStateLayout = view.FindViewById<LinearLayout> (Resource.Id.ProjectListEmptyState);
            tabLayout = view.FindViewById<TabLayout> (Resource.Id.WorkspaceTabLayout);
            newProjectFab = view.FindViewById<AddProjectFab> (Resource.Id.AddNewProjectFAB);
            toolBar = view.FindViewById<Toolbar> (Resource.Id.ProjectListToolbar);

            var activity = (Activity)Activity;
            activity.SetSupportActionBar (toolBar);
            activity.SupportActionBar.SetDisplayHomeAsUpEnabled (true);
            activity.SupportActionBar.SetTitle (Resource.String.ChooseTimeEntryProjectDialogTitle);

            HasOptionsMenu = true;
            newProjectFab.Click += OnNewProjectFabClick;
            tabLayout.SetOnTabSelectedListener (this);

            return view;
        }

        public async override void OnViewCreated (View view, Bundle savedInstanceState)
        {
            base.OnViewCreated (view, savedInstanceState);

            viewModel = new ProjectListViewModel (TimeEntryIds);

            var adapter = new ProjectListAdapter (recyclerView, viewModel.ProjectList);
            adapter.HandleProjectSelection = OnItemSelected;
            recyclerView.SetAdapter (adapter);

            viewModel.OnIsLoadingChanged += OnDataLoaded;
            viewModel.ProjectList.OnIsLoadingChanged += OnDataLoaded;

            await viewModel.Init ();
        }

        private void OnDataLoaded (object sender, EventArgs e)
        {
            if (!viewModel.IsLoading) {
                if (viewModel.Model == null) {
                    Activity.Finish ();
                }
            }

            if (viewModel.ProjectList.IsLoading || viewModel.IsLoading) {
                return;
            }

            EnsureCorrectState ();

            // Create tabs
            if (viewModel.ProjectList.Workspaces.Count > 2) {
                int i = 0;
                foreach (var ws in viewModel.ProjectList.Workspaces) {
                    var tab = tabLayout.NewTab().SetText (ws.Data.Name);
                    tabLayout.AddTab (tab);
                    if (ws.Data.Id == viewModel.TimeEntryList[0].WorkspaceId) {
                        viewModel.ProjectList.CurrentWorkspaceIndex = i;
                        tab.Select();
                    }
                    i++;
                }
            }
        }

        private void EnsureCorrectState ()
        {
            // Set toolbar scrollable or not.
            var _params = new AppBarLayout.LayoutParams (toolBar.LayoutParameters);

            if (viewModel.ProjectList.Workspaces.Count > 2) {
                tabLayout.Visibility = ViewStates.Visible;
                _params.ScrollFlags  = AppBarLayout.LayoutParams.ScrollFlagScroll | AppBarLayout.LayoutParams.ScrollFlagEnterAlways;
            } else {
                tabLayout.Visibility = ViewStates.Gone;
                _params.ScrollFlags = 0;
            }

            toolBar.LayoutParameters = _params;
            recyclerView.Visibility = viewModel.ProjectList.IsEmpty ? ViewStates.Gone : ViewStates.Visible;
            emptyStateLayout.Visibility = viewModel.ProjectList.IsEmpty ? ViewStates.Visible : ViewStates.Gone;
        }

        private void OnNewProjectFabClick (object sender, EventArgs e)
        {
            var entryList = new List<TimeEntryData> (viewModel.TimeEntryList);

            // Show create project activity instead
            var intent = BaseActivity.CreateDataIntent<NewProjectActivity, List<TimeEntryData>>
                         (Activity, entryList, NewProjectActivity.ExtraTimeEntryDataListId);
            StartActivityForResult (intent, ProjectCreatedRequestCode);
        }

        private async void OnItemSelected (object m)
        {
            ProjectModel project = null;
            WorkspaceModel workspace = null;
            TaskData task = null;
{'fraction_non_alphanumeric': 0.07628562577447336, 'fraction_numerical': 0.0017038413878562577, 'mean_word_length': 2.296655603778402, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3616861', 'n_tokens_mistral': 3337, 'n_tokens_neox': 3181, 'n_words': 672}