-
Notifications
You must be signed in to change notification settings - Fork 1
/
SearchActivity.cs
138 lines (123 loc) · 5.26 KB
/
SearchActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using AndroidX.AppCompat.App;
using AndroidX.AppCompat.Widget;
using AndroidX.Core.Widget;
using AndroidX.RecyclerView.Widget;
using com.aa.tvshows.Helper;
namespace com.aa.tvshows
{
[Activity(Label = "Search", ConfigurationChanges = Android.Content.PM.ConfigChanges.ScreenLayout | Android.Content.PM.ConfigChanges.ScreenSize)]
public class SearchActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.search_activity);
AppView.SetActionBarForActivity(FindViewById<Toolbar>(Resource.Id.main_toolbar), this);
SetupSearch();
}
public override bool OnSupportNavigateUp()
{
OnBackPressed();
return base.OnSupportNavigateUp();
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
return AppView.OnOptionsItemSelected(item, this);
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
return AppView.ShowOptionsMenu(menu, this);
}
private void SetupSearch()
{
var editText = FindViewById<SearchView>(Resource.Id.search_text_query);
var emptyView = FindViewById<AppCompatTextView>(Resource.Id.search_empty_text);
var loadingView = FindViewById<ContentLoadingProgressBar>(Resource.Id.search_loading_view);
var list = FindViewById<RecyclerView>(Resource.Id.search_list);
list.ClearOnScrollListeners();
var layoutManager = new CachingLayoutManager(this);
list.SetLayoutManager(layoutManager);
editText.RequestFocus();
editText.QueryTextChange += async (s, e) =>
{
// get data from suggestions
// show in listview as simple items
e.Handled = true;
if (e.NewText.Trim() is string query && query.Length > 2)
{
emptyView.Visibility = ViewStates.Gone;
loadingView.Visibility = ViewStates.Visible;
var suggestions = await WebData.GetTVShowSearchSuggestions(query);
loadingView.Visibility = ViewStates.Gone;
var adapter = new EpisodesAdapter<SearchSuggestionsData>(suggestions);
adapter.ItemClick += (s, e) =>
{
// handle click here
AppView.HandleItemShowEpisodeClick(adapter.GetItem(e), this);
};
list.SetAdapter(adapter);
}
else
{
emptyView.Visibility = ViewStates.Visible;
emptyView.Text = "Search query should be at least 3 characters long";
list.SetAdapter(null);
}
};
editText.QueryTextSubmit += async(s, e) =>
{
// get data from page
// load full image based search results
e.Handled = true;
if (e.NewText.Trim() is string query && query.Length > 2)
{
list.ClearOnScrollListeners();
var scroll = new EndlessScroll(layoutManager);
loadingView.Visibility = ViewStates.Visible;
emptyView.Visibility = ViewStates.Gone;
var searchResults = await WebData.GetTVShowSearchResults(query);
loadingView.Visibility = ViewStates.Gone;
var adapter = new EpisodesAdapter<SearchList>(searchResults);
adapter.ItemClick += (s, e) =>
{
// handle item click
AppView.HandleItemShowEpisodeClick(adapter.GetItem(e), this);
};
list.SetAdapter(adapter);
scroll.LoadMoreTask += async delegate
{
if (searchResults != null && searchResults.LastOrDefault() is SearchList item)
{
if (item.NextPage > 0)
{
loadingView.Visibility = ViewStates.Visible;
var newItems = await WebData.GetTVShowSearchResults(query, item.NextPage).ConfigureAwait(true);
if (newItems != null) adapter.AddItem(newItems.ToArray());
loadingView.Visibility = ViewStates.Gone;
}
}
};
list.AddOnScrollListener(scroll);
}
else
{
emptyView.Visibility = ViewStates.Visible;
emptyView.Text = "Search query should be at least 3 characters long";
list.SetAdapter(null);
}
};
}
}
}