Browse Source

maybe fix something

firestoreRecent
oogee 6 years ago
parent
commit
cf7fae9880
  1. 69
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  2. 6
      app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java
  3. 5
      app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java
  4. 2
      build.gradle

69
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java

@ -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,44 +72,29 @@ 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()
.collection("recent_posts")
.document("recent");
Timber.d("I'm here");
docRef.get().addOnCompleteListener(task -> {
Timber.d("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);
recentItems.add(recentItem);
}
});
} else {
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show();
}
});
return null;
}
}.execute();
progressBar.setVisibility(ProgressBar.VISIBLE);
}
Timber.d("onActivityCreated");
DocumentReference docRef = FirebaseFirestore.getInstance()
.collection("recent_posts")
.document("recent");
Timber.i("I'm here");
docRef.get().addOnCompleteListener(task -> {
Timber.i("I'm there");
progressBar.setVisibility(ProgressBar.INVISIBLE);
if (task.isSuccessful()) {
DocumentSnapshot recentDocument = task.getResult();
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();
}
});
progressBar.setVisibility(ProgressBar.VISIBLE);
}

6
app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java

@ -53,7 +53,6 @@ public class BaseApplication extends Application {
//Firebase Analytics
private FirebaseAnalytics firebaseAnalytics;
private FirebaseFirestore firestoredb;
//Client & SessionManager
private OkHttpClient client;
@ -95,7 +94,6 @@ public class BaseApplication extends Application {
Timber.i("Starting app with Analytics enabled.");
else
Timber.i("Starting app with Analytics disabled.");
firestoredb = FirebaseFirestore.getInstance();
SharedPrefsCookiePersistor sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(getApplicationContext());
PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor);
@ -184,10 +182,6 @@ public class BaseApplication extends Application {
return client;
}
public FirebaseFirestore getFirestoredb() {
return firestoredb;
}
public SessionManager getSessionManager() {
return sessionManager;
}

5
app/src/main/java/gr/thmmy/mthmmy/model/RecentItem.java

@ -1,6 +1,7 @@
package gr.thmmy.mthmmy.model;
import java.util.Date;
import java.util.HashMap;
public class RecentItem {
private int boardId, postId, topicId, posterId;
@ -19,6 +20,10 @@ public class RecentItem {
this.timestamp = new Date(timestamp);
}
public RecentItem(HashMap<String, Object> keymap) {
}
public Date getTimestamp() {
return timestamp;
}

2
build.gradle

@ -8,7 +8,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.2.0'
classpath 'io.fabric.tools:gradle:1.26.1'
classpath 'org.ajoberstar.grgit:grgit-core:3.0.0' // Also change in app/gradle/grgit.gradle

Loading…
Cancel
Save