|
|
@ -1,6 +1,5 @@ |
|
|
|
package gr.thmmy.mthmmy.activities.main.recent; |
|
|
|
|
|
|
|
import android.os.AsyncTask; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.View; |
|
|
@ -9,19 +8,17 @@ import android.widget.ProgressBar; |
|
|
|
import android.widget.RelativeLayout; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
import com.google.android.gms.tasks.Task; |
|
|
|
import com.google.android.gms.tasks.Tasks; |
|
|
|
import com.google.firebase.firestore.DocumentReference; |
|
|
|
import com.google.firebase.firestore.DocumentSnapshot; |
|
|
|
import com.google.firebase.firestore.FirebaseFirestore; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration; |
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager; |
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; |
|
|
|
import gr.thmmy.mthmmy.R; |
|
|
|
import gr.thmmy.mthmmy.base.BaseApplication; |
|
|
|
import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
|
import gr.thmmy.mthmmy.model.RecentItem; |
|
|
|
import gr.thmmy.mthmmy.utils.CustomRecyclerView; |
|
|
@ -48,7 +45,8 @@ public class RecentFragment extends BaseFragment { |
|
|
|
private ArrayList<RecentItem> recentItems = new ArrayList<>(); |
|
|
|
|
|
|
|
// Required empty public constructor
|
|
|
|
public RecentFragment() {} |
|
|
|
public RecentFragment() { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
@ -74,45 +72,30 @@ public class RecentFragment extends BaseFragment { |
|
|
|
@Override |
|
|
|
public void onActivityCreated(Bundle savedInstanceState) { |
|
|
|
super.onActivityCreated(savedInstanceState); |
|
|
|
if (recentItems == null) { |
|
|
|
Timber.d("I'm ere"); |
|
|
|
new AsyncTask<Void, Void, Void>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
protected Void doInBackground(Void... voids) { |
|
|
|
DocumentReference docRef = BaseApplication.getInstance().getFirestoredb() |
|
|
|
Timber.d("onActivityCreated"); |
|
|
|
|
|
|
|
DocumentReference docRef = FirebaseFirestore.getInstance() |
|
|
|
.collection("recent_posts") |
|
|
|
.document("recent"); |
|
|
|
Timber.d("I'm here"); |
|
|
|
Timber.i("I'm here"); |
|
|
|
docRef.get().addOnCompleteListener(task -> { |
|
|
|
Timber.d("I'm there"); |
|
|
|
Timber.i("I'm there"); |
|
|
|
progressBar.setVisibility(ProgressBar.INVISIBLE); |
|
|
|
if (task.isSuccessful()) { |
|
|
|
DocumentSnapshot recentDocument = task.getResult(); |
|
|
|
List<DocumentReference> posts = (List<DocumentReference>) recentDocument.get("posts"); |
|
|
|
List<Task<DocumentSnapshot>> tasks = new ArrayList<>(); |
|
|
|
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); |
|
|
|
Timber.i("Type: " + recentDocument.get("posts").getClass().getName()); |
|
|
|
ArrayList<HashMap<String, Object>> posts = (ArrayList<HashMap<String, Object>>) recentDocument.get("posts"); |
|
|
|
for (HashMap<String, Object> map : posts) { |
|
|
|
RecentItem recentItem = new RecentItem(map); |
|
|
|
recentItems.add(recentItem); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else { |
|
|
|
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); |
|
|
|
} |
|
|
|
}); |
|
|
|
return null; |
|
|
|
} |
|
|
|
}.execute(); |
|
|
|
progressBar.setVisibility(ProgressBar.VISIBLE); |
|
|
|
} |
|
|
|
Timber.d("onActivityCreated"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|