public class BrowseStoryRecyclerFragment extends Fragment {
private ProgressBar progressBar;
// private Context mContext;
//private BrowseAdapter adapter;
//private ListView multiList;
private CopyOnWriteArrayList<Student> arrayBrowseList = new CopyOnWriteArrayList<Student>();
private List<ParseObject> newList;
private String searchStringResult;
private String fromButton;
/*private int currentScrollState;
private int currentVisibleItemCount;*/
private int skipParam = 0;
private boolean flag_loading = false;
// For Location Cloud call - in aysnc task
// Memory cache
// private LruCache<String, Bitmap> mMemoryCache;
RecyclerView recyclerView;
com.studystory.adapters.RecyclerViewAdapter mAdapter;
int pastVisiblesItems, visibleItemCount, totalItemCount;
LinearLayoutManager mLayoutManager;
Bitmap noImage = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_browse_story_recycler, container, false);
((HomeActivityTabbed) getActivity())
.setActionBarTitle("Browse Stories");
// 1. get a reference to recyclerView
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
noImage = BitmapFactory.decodeResource(getResources(), R.drawable.no_image);
noImage = ImageController.resizeToRoundEdgeSquare(noImage, getActivity().getApplicationContext());
mLayoutManager = new LinearLayoutManager(getActivity());
// 2. set layoutManger
recyclerView.setLayoutManager(mLayoutManager);
// 3. create an adapter
mAdapter = new com.studystory.adapters.RecyclerViewAdapter(getActivity().getApplicationContext(), arrayBrowseList);
// 4. set adapter
recyclerView.setAdapter(mAdapter);
recyclerView.addItemDecoration(new com.studystory.utilities.DividerItemDecoration
(getActivity(), com.studystory.utilities.DividerItemDecoration.VERTICAL_LIST));
recyclerView.addOnItemTouchListener(
new com.studystory.utilities.RecyclerItemClickListener(getActivity(), new com.studystory.utilities.RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.e("position", ""+position);
try {
final Student s = (Student) mAdapter.getObjectAt(position);
final Intent i = new Intent(getActivity().getApplicationContext(), ViewStory.class);
final int pos = position;
if (fromButton.equalsIgnoreCase("browseStoriesButton")) {
i.putExtra("Button", "browseStoriesButton");
} else {
i.putExtra("Button", "notbrowseStoriesButton");
}
String dateOfBirthStr = "";
try {
dateOfBirthStr = TimeSplitterController.generateAge(s.getDateOfBirth().toString());
i.putExtra("dateOfBirthStr", dateOfBirthStr);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
final String dateOfBirthString = dateOfBirthStr;
if (s.getByteArray() == null) {
/* try {
//We assume they have no idea associated.
Bitmap tempBitmap = null;
tempBitmap = ImageController.resizeToHighResolutionCircle(tempBitmap, getApplicationContext());
String bitmapStr = ImageController.bitmapToStringOld(tempBitmap, getApplicationContext());
i.putExtra("bitmapStr", bitmapStr);
tempBitmap.recycle();
tempBitmap = null;
} catch (Exception e) {
Log.e("Exception",e.toString());
}*/
} else {
//They have an image.
Bitmap tempBitmap = ImageController.BitmapCompress(s.getByteArray());
if (tempBitmap != null) {
tempBitmap = ImageController.resizeToHighResolutionCircle(tempBitmap, getActivity().getApplicationContext());
} else {
tempBitmap = ImageController.resizeToCircle(tempBitmap, getActivity().getApplicationContext());
}
String bitmapStr = ImageController.bitmapToStringOld(tempBitmap, getActivity().getApplicationContext());
i.putExtra("bitmapStr", bitmapStr);
tempBitmap.recycle();
tempBitmap = null;
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
i.putExtra("studentObject", s);
startActivity(i);
}
}, 50);
Log.e("Clicked", "" + position);
}
catch (Exception e){
Log.e("List issue", e.toString());
}
}
})
);
Bundle extras = getActivity().getIntent().getExtras();
if (extras == null) {
}
if (extras != null) {
searchStringResult = extras.getString("searchString");
fromButton = extras.getString("Button");
}
/* noImg = getResources().getDrawable(R.drawable.no_image);
reshapedDrawable = ImageController.drawableToBitmap(noImg);
reshapedDrawable = ImageController.resizeToCircle(reshapedDrawable);*/
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
// checking which list to populate
// if (fromButton.equalsIgnoreCase("browseStoriesButton")) {
callCloudFunctionBrowseStories();
/* } else {
}*/
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
int currentState = RecyclerView.SCROLL_STATE_IDLE;
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (pastVisiblesItems + visibleItemCount == totalItemCount && totalItemCount != 0) {
if (flag_loading == false) {
flag_loading = true;
skipParam = skipParam + 10;
System.gc();
callCloudFunctionBrowseStories();
}
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (currentState == RecyclerView.SCROLL_STATE_SETTLING
&& newState == RecyclerView.SCROLL_STATE_IDLE) {
}
else {
}
currentState = newState;
}
});
return rootView;
}
class AddStringTask extends AsyncTask<Void, Student, Void> {
List<Student> random;
ProgressDialog pd;
@Override
protected void onPreExecute(){
}
@Override
protected Void doInBackground(Void... unused){
flag_loading = false;
for (int i = 0; i < newList.size(); i++) {
final Student student = new Student();
// Abhideep:to check list is empty or not to avoid null pointer
if (newList.size() > 0) {
student.setStudentId(newList.get(i).getObjectId());
student.setFirstName(newList.get(i).getString("firstName"));
student.setLastName(newList.get(i).getString("lastName"));
student.setCreatedAt(newList.get(i).getCreatedAt());
student.setDateOfBirth(newList.get(i).getDate("dateOfBirth"));
}
if (newList.get(i).getParseFile("profilePhoto") == null) {
}
if (newList.get(i).getParseFile("profilePhoto") != null){
student.setHasProfileImage(true);
newList.get(i).getParseFile("profilePhoto").getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] arg0, ParseException arg1) {
if (arg1 == null) {
if (arg0 != null) {
try {
student.setByteArray(arg0);
mAdapter.notifyDataSetChanged();
} catch (IllegalArgumentException e) {
} catch (OutOfMemoryError e) {
System.gc();
}
}
}
}
});
student.setProfilePhotoUrl(newList.get(i).getParseFile("profilePhoto").getUrl());
} else {
student.setHasProfileImage(false);
student.setBitmapProfile(noImage);
}
// Abhideep:to check list is empty or not to avoid null pointer
if (newList.size() > 0) {
student.setCountryFrom(newList.get(i).getString("countryFrom"));
}
// Time to get the college information
// Abhideep:to check list is empty or not to avoid null pointer
if (newList.size() > 0) {
if (newList.get(i).getParseObject("college") != null)
newList.get(i).getParseObject("college").fetchIfNeededInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException arg1){
if (arg1 == null) {
if (object.getString("studentsName") == null || object.getString("studentsName").contentEquals("null")) {
student.setCollegeName(object.getString("name"));
} else {
student.setCollegeName(object.getString("studentsName"));
}
mAdapter.notifyDataSetChanged();
}
}
});
}
//saving featuredDate and featuredRating of users
if (newList.size() > 0) {
if (newList.get(i).getDate("featuredDate") != null) {
student.setFeaturedDateLong(newList.get(i).getDate("featuredDate").getTime());
} else {
student.setFeaturedDateLong(0L);
}
student.setFeaturedRating(newList.get(i).getInt("featuredRating"));
}
publishProgress(student);
SystemClock.sleep(100);
}
return (null);
}
@Override
protected void onProgressUpdate(Student... student){
progressBar.setVisibility(View.INVISIBLE);
arrayBrowseList.add(student[0]);
mAdapter.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Void unused){
}
}
public void populateList(){
new AddStringTask().execute();
}
public void callCloudFunctionBrowseStories(){
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("skip", skipParam);
ParseCloud.callFunctionInBackground("automaticMatchFeaturedBrowse", params, new FunctionCallback<List<ParseObject>>() {
@Override
public void done(List<ParseObject> arg0, com.parse.ParseException arg1) {
if (arg0 == null) {
} else {
newList = arg0;
populateList();
}
}
});
}
}