Browse Source

maybe fix something

firestoreRecent
oogee 6 years ago
parent
commit
cf7fae9880
  1. 43
      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

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

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

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

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

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

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

2
build.gradle

@ -8,7 +8,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { 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 'com.google.gms:google-services:4.2.0'
classpath 'io.fabric.tools:gradle:1.26.1' classpath 'io.fabric.tools:gradle:1.26.1'
classpath 'org.ajoberstar.grgit:grgit-core:3.0.0' // Also change in app/gradle/grgit.gradle classpath 'org.ajoberstar.grgit:grgit-core:3.0.0' // Also change in app/gradle/grgit.gradle

Loading…
Cancel
Save