/
MainFragment.java
232 lines (202 loc) · 9.17 KB
/
MainFragment.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package in.cubestack.material.androidmaterial.fragment;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import in.cubestack.android.lib.storm.SortOrder;
import in.cubestack.android.lib.storm.criteria.Order;
import in.cubestack.android.lib.storm.criteria.Restriction;
import in.cubestack.android.lib.storm.criteria.Restrictions;
import in.cubestack.android.lib.storm.criteria.StormRestrictions;
import in.cubestack.android.lib.storm.service.asyc.StormCallBack;
import in.cubestack.material.androidmaterial.R;
import in.cubestack.material.androidmaterial.adapter.WordListAdapter;
import in.cubestack.material.androidmaterial.model.WordList;
import in.cubestack.material.androidmaterial.ui.AbstractCubeStackActivity;
import in.cubestack.material.androidmaterial.ui.MainActivity;
import in.cubestack.material.androidmaterial.ui.MainApplication;
import in.cubestack.material.androidmaterial.util.DividerDecoration;
import in.cubestack.material.androidmaterial.util.EndlessRecyclerOnScrollListener;
import in.cubestack.material.androidmaterial.util.LogUtils;
import in.cubestack.material.androidmaterial.util.UiUtils;
/**
* Sample application for Storm ORM.
*
* Check on Google play: https://play.google.com/store/apps/developer?id=Cube+Stack
* Storm on GIT: https://github.com/supald/storm
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
public class MainFragment extends AbstractFragment {
private static final String TAG = LogUtils.makeLogTag(MainFragment.class);
int lastSelectedIndex = 0;
@Bind(R.id.alphabet_scroller)
LinearLayout alphabetScrollerLayout;
@Bind(R.id.alphabet_rv)
RecyclerView recyclerView;
WordListAdapter wordListAdapter;
public static MainFragment newInstance() {
MainFragment f = new MainFragment();
Bundle args = new Bundle();
f.setArguments(args);
f.setTitle("MAIN");
return f;
}
public void updateData() {
loadData(UiUtils.ALPHABETS.get(lastSelectedIndex), true);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_main, container, false);
ButterKnife.bind(this, rootView);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupViews();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
private void setupViews() {
TextView alphaView;
LinearLayout.LayoutParams lp;
Resources res = getActivity().getResources();
int idx = 0;
for (final String alpha : UiUtils.ALPHABETS) {
alphaView = new TextView(getActivity());
alphaView.setText(alpha);
//alphaView.setTextSize(16, TypedValue.COMPLEX_UNIT_SP);
alphaView.setGravity(Gravity.CENTER);
lp = new LinearLayout.LayoutParams(UiUtils.dpToPx(48),
UiUtils.dpToPx(48));
lp.setMargins(0, 0, 50, 0);
if (idx == 0) {
alphaView.setTextColor(Color.WHITE);
alphaView.setBackgroundResource(R.drawable.alphabet_pressed);
} else {
alphaView.setBackgroundResource(R.drawable.alphabet_selector);
alphaView.setTextColor(res.getColor(R.color.primary));
}
final int index = idx;
alphaView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSelect(index, alpha);
}
});
alphabetScrollerLayout.addView(alphaView, lp);
idx++;
}
recyclerView.addItemDecoration(new DividerDecoration(getActivity()));
recyclerView.setHasFixedSize(true);
LinearLayoutManager alphabetLayoutManager = new LinearLayoutManager(getActivity());
alphabetLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(alphabetLayoutManager);
wordListAdapter = new WordListAdapter(getActivity());
recyclerView.setAdapter(wordListAdapter);
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener((LinearLayoutManager)
recyclerView.getLayoutManager()) {
@Override
public void onLoadMore(int current_page) {
loadData(UiUtils.ALPHABETS.get(lastSelectedIndex), false);
}
});
loadData(UiUtils.ALL, false);
}
int pageNumber = 0;
private void onSelect(int idx, String word) {
if (lastSelectedIndex != idx) {
Resources res = getActivity().getResources();
TextView lv = (TextView) alphabetScrollerLayout.getChildAt(lastSelectedIndex);
lv.setBackgroundResource(R.drawable.alphabet_selector);
lv.setTextColor(res.getColor(R.color.primary));
TextView v = (TextView) alphabetScrollerLayout.getChildAt(idx);
v.setBackgroundResource(R.drawable.alphabet_pressed);
v.setTextColor(Color.WHITE);
pageNumber = 0;
lastSelectedIndex = idx;
// Reset Endless Scroller
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener((LinearLayoutManager)
recyclerView.getLayoutManager()) {
@Override
public void onLoadMore(int current_page) {
loadData(UiUtils.ALPHABETS.get(lastSelectedIndex), false);
}
});
loadData(word, true);
}
}
private void loadData(final String word, final boolean triggeredFromAlphabetFilter) {
try {
if (triggeredFromAlphabetFilter) {
pageNumber = 0;
}
int pageNo = ++pageNumber;
Restrictions restrictions = StormRestrictions.restrictionsFor(WordList.class);
Restriction restriction = null;
if (UiUtils.ALL.equals(word)) {
restriction = restrictions.forAll();
} else {
restriction = restrictions.like("word", word.toLowerCase() + "%");
}
final long s = System.currentTimeMillis();
final AbstractCubeStackActivity activity = (AbstractCubeStackActivity) getActivity();
MainApplication.service().find(WordList.class, restriction.page(pageNo), Order.orderFor(WordList.class, new String[]{"word"}, SortOrder.ASC),
new StormCallBack<WordList>() {
@Override
public void onResults(final List<WordList> results) {
final long e = System.currentTimeMillis();
if (results != null && !results.isEmpty()) {
((MainActivity) activity).showSnackBar("Fetched " + results.size() + " records in " + (e - s) + " ms");
if (triggeredFromAlphabetFilter) {
wordListAdapter.clearAndAddNewItems(results);
recyclerView.smoothScrollToPosition(0);
} else {
wordListAdapter.addNewItems(results);
}
}
}
@Override
public void onError(Throwable throwable) {
super.onError(throwable);
}
});
} catch (Exception e) {
LogUtils.LOGE(TAG, "", e);
}
}
}