|
@ -24,10 +24,7 @@ import gr.thmmy.mthmmy.R; |
|
|
import gr.thmmy.mthmmy.base.BaseApplication; |
|
|
import gr.thmmy.mthmmy.base.BaseApplication; |
|
|
import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
import gr.thmmy.mthmmy.model.RecentItem; |
|
|
import gr.thmmy.mthmmy.model.RecentItem; |
|
|
import gr.thmmy.mthmmy.model.TopicSummary; |
|
|
|
|
|
import gr.thmmy.mthmmy.session.SessionManager; |
|
|
|
|
|
import gr.thmmy.mthmmy.utils.CustomRecyclerView; |
|
|
import gr.thmmy.mthmmy.utils.CustomRecyclerView; |
|
|
import gr.thmmy.mthmmy.utils.NetworkResultCodes; |
|
|
|
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
import timber.log.Timber; |
|
|
import timber.log.Timber; |
|
|
|
|
|
|
|
@ -48,7 +45,7 @@ public class RecentFragment extends BaseFragment { |
|
|
private SwipeRefreshLayout swipeRefreshLayout; |
|
|
private SwipeRefreshLayout swipeRefreshLayout; |
|
|
private RecentAdapter recentAdapter; |
|
|
private RecentAdapter recentAdapter; |
|
|
|
|
|
|
|
|
private ArrayList<RecentItem> recentItems; |
|
|
private ArrayList<RecentItem> recentItems = new ArrayList<>(); |
|
|
|
|
|
|
|
|
// Required empty public constructor
|
|
|
// Required empty public constructor
|
|
|
public RecentFragment() {} |
|
|
public RecentFragment() {} |
|
@ -78,27 +75,41 @@ public class RecentFragment extends BaseFragment { |
|
|
public void onActivityCreated(Bundle savedInstanceState) { |
|
|
public void onActivityCreated(Bundle savedInstanceState) { |
|
|
super.onActivityCreated(savedInstanceState); |
|
|
super.onActivityCreated(savedInstanceState); |
|
|
if (recentItems == null) { |
|
|
if (recentItems == null) { |
|
|
DocumentReference docRef = BaseApplication.getInstance().getFirestoredb() |
|
|
Timber.d("I'm ere"); |
|
|
.collection("recent_posts") |
|
|
new AsyncTask<Void, Void, Void>() { |
|
|
.document("recent"); |
|
|
|
|
|
docRef.get().addOnCompleteListener(task -> { |
|
|
@Override |
|
|
if (task.isSuccessful()) { |
|
|
protected Void doInBackground(Void... voids) { |
|
|
DocumentSnapshot recentDocument = task.getResult(); |
|
|
DocumentReference docRef = BaseApplication.getInstance().getFirestoredb() |
|
|
List<DocumentReference> posts = (List<DocumentReference>) recentDocument.get("posts"); |
|
|
.collection("recent_posts") |
|
|
List<Task<DocumentSnapshot>> tasks = new ArrayList<>(); |
|
|
.document("recent"); |
|
|
for (DocumentReference documentReference : posts) { |
|
|
Timber.d("I'm here"); |
|
|
Task<DocumentSnapshot> documentSnapshotTask = documentReference.get(); |
|
|
docRef.get().addOnCompleteListener(task -> { |
|
|
tasks.add(documentSnapshotTask); |
|
|
Timber.d("I'm there"); |
|
|
} |
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
Tasks.whenAllSuccess(tasks).addOnSuccessListener(objects -> { |
|
|
if (task.isSuccessful()) { |
|
|
ArrayList<RecentItem> recentItems = new ArrayList<>(); |
|
|
DocumentSnapshot recentDocument = task.getResult(); |
|
|
for (Object object : objects) { |
|
|
List<DocumentReference> posts = (List<DocumentReference>) recentDocument.get("posts"); |
|
|
RecentItem recentItem = ((DocumentSnapshot) object).toObject(RecentItem.class); |
|
|
List<Task<DocumentSnapshot>> tasks = new ArrayList<>(); |
|
|
recentItems.add(recentItem); |
|
|
for (DocumentReference documentReference : posts) { |
|
|
|
|
|
Task<DocumentSnapshot> documentSnapshotTask = documentReference.get(); |
|
|
|
|
|
tasks.add(documentSnapshotTask); |
|
|
|
|
|
} |
|
|
|
|
|
Tasks.whenAllSuccess(tasks).addOnSuccessListener(objects -> { |
|
|
|
|
|
ArrayList<RecentItem> recentItems = new ArrayList<>(); |
|
|
|
|
|
for (Object object : objects) { |
|
|
|
|
|
RecentItem recentItem = ((DocumentSnapshot) object).toObject(RecentItem.class); |
|
|
|
|
|
recentItems.add(recentItem); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
return null; |
|
|
} |
|
|
} |
|
|
}); |
|
|
}.execute(); |
|
|
|
|
|
progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
} |
|
|
} |
|
|
Timber.d("onActivityCreated"); |
|
|
Timber.d("onActivityCreated"); |
|
|
} |
|
|
} |
|
@ -126,47 +137,13 @@ public class RecentFragment extends BaseFragment { |
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh); |
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh); |
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); |
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); |
|
|
swipeRefreshLayout.setColorSchemeResources(R.color.accent); |
|
|
swipeRefreshLayout.setColorSchemeResources(R.color.accent); |
|
|
swipeRefreshLayout.setOnRefreshListener(() -> { |
|
|
|
|
|
if (!recentTask.isRunning()) { |
|
|
|
|
|
recentTask = new RecentTask(this::onRecentTaskStarted, this::onRecentTaskFinished); |
|
|
|
|
|
recentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, SessionManager.indexUrl.toString()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return rootView; |
|
|
return rootView; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onDestroy() { |
|
|
|
|
|
super.onDestroy(); |
|
|
|
|
|
if (recentTask.isRunning()) |
|
|
|
|
|
recentTask.cancel(true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface RecentFragmentInteractionListener extends FragmentInteractionListener { |
|
|
public interface RecentFragmentInteractionListener extends FragmentInteractionListener { |
|
|
void onRecentFragmentInteraction(RecentItem topicSummary); |
|
|
void onRecentFragmentInteraction(RecentItem topicSummary); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void onRecentTaskStarted() { |
|
|
|
|
|
progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void onRecentTaskFinished(int resultCode, ArrayList<TopicSummary> fetchedRecent) { |
|
|
|
|
|
if (resultCode == NetworkResultCodes.SUCCESSFUL) { |
|
|
|
|
|
topicSummaries.clear(); |
|
|
|
|
|
topicSummaries.addAll(fetchedRecent); |
|
|
|
|
|
recentAdapter.notifyDataSetChanged(); |
|
|
|
|
|
} else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { |
|
|
|
|
|
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
} else { |
|
|
|
|
|
Toast.makeText(getContext(), "Unexpected error," + |
|
|
|
|
|
" please contact the developers with the details", Toast.LENGTH_LONG).show(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
|
|
|
swipeRefreshLayout.setRefreshing(false); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|