Работа со Storage,RecycleView и Firestore в приложении
Всех приветствую. Пытаюсь решить проблему, со Storage в своём Android приложении. Являюсь новичком, не очень понимаю как правильно писать архитектуру такой программы, поэтому буду благодарен за любые подсказки. В коде я получаю все записи и сортирую их по количеству кликов. После чего получаю запись по её id и пытаюсь вывести каждую в RecycleView, получая название записи и ссылку на баннер в Storage. Но по итогу у меня получается так, что у меня выводится лишь первая запись из бд. Буду благодарен любой помощи. К сообщению прикрепил ссылки на пастбин, скрины приложения и логи.
package com.lovejazz.kyle;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
public class PasswordsFragment extends Fragment {
private static final String TAG = "PasswordsFragment";
private FirebaseFirestore fstore;
private FirebaseAuth mAuth;
private FirebaseStorage storage;
private List<Map.Entry<String, Integer>> maxCountOfClicks;
private String[] mostPopularAccountsNames;
private String currentName;
private String[] bannerReferences;
private String currentBannerReference;
private String currentAppName;
private View view;
private int position;
private int imagePosition;
@Override
public void onStart() {
super.onStart();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_passwords, container, false);
//Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
//Initializing fstore
fstore = FirebaseFirestore.getInstance();
storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference();
maxCountOfClicks = new ArrayList<>();
//Getting maxCountOfClicks
fstore.collection("users").
document(mAuth.getCurrentUser().getUid()).collection("accounts").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
int countOfClicks = Integer.
parseInt(document.get("countOfClicks").toString());
Log.d(TAG, countOfClicks + " - count of clicks " + document.getId());
if (maxCountOfClicks.size() < 6) {
maxCountOfClicks.add(new AbstractMap.SimpleEntry<>
(document.getId(), countOfClicks));
} else {
for (int i = 0; i < maxCountOfClicks.size(); i++) {
if (countOfClicks > maxCountOfClicks.get(i).getValue()) {
maxCountOfClicks.add(new AbstractMap.SimpleEntry<>
(maxCountOfClicks.get(i).getKey(),
countOfClicks));
}
}
}
//Sorting hashMap
Collections.sort(maxCountOfClicks, new Comparator<Map.Entry<String,
Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.
Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
Collections.reverse(maxCountOfClicks);
}
Log.d(TAG, maxCountOfClicks + " - maxCountOfClicks");
createPopularAccountsRecyclerView();
}
}
});
// //Connecting credit card`s RecyclerView
// RecyclerView accountRecycler = view.findViewById(R.id.account_recycler);
// String[] accountsName = new String[Account.accounts.length];
// for (int i = 0; i < accountsName.length; i++) {
// accountsName[i] = Account.accounts[i].getName();
// }
// String[] accountsLogins = new String[Account.accounts.length];
// for (int i = 0; i < accountsLogins.length; i++) {
// accountsLogins[i] = Account.accounts[i].getLogin();
// }
// int[] imageId = new int[Account.accounts.length];
// for (int i = 0; i < imageId.length; i++) {
// imageId[i] = Account.accounts[i].getImageId();
// }
// AccountAdapter accountAdapter = new AccountAdapter(accountsName, accountsLogins, imageId);
// accountRecycler.setAdapter(accountAdapter);
// LinearLayoutManager accountManager = new LinearLayoutManager(view.getContext(), LinearLayoutManager.VERTICAL, false);
// accountRecycler.setLayoutManager(accountManager);
return view;
}
private void createPopularAccountsRecyclerView() {
if (maxCountOfClicks.size() == 0) {
Log.d(TAG, " - maxCountOfClicks == 0");
} else {
Log.d(TAG, " - maxCountOfClicks != 0");
mostPopularAccountsNames = new String[maxCountOfClicks.size()];
bannerReferences = new String[maxCountOfClicks.size()];
for (int i = 0; i < mostPopularAccountsNames.length; i++) {
position = 0;
findUserById();
}
}
}
private void findUserById() {
fstore.collection("users").document(mAuth.getCurrentUser().getUid())
.collection("accounts").
whereEqualTo("id", maxCountOfClicks.get(position).getKey()).get().
addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
Log.d(TAG, maxCountOfClicks.get(position).getKey() + " - current id");
getRecordInfo(task);
}
});
}
private void getRecordInfo(@NonNull Task<QuerySnapshot> task) {
Log.d(TAG, " - getting record info");
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getString("id") + " - id of record");
Log.d(TAG, position + " - position");
currentName = document.getString("name");
Log.d(TAG, currentName + " - currentName");
mostPopularAccountsNames[position] = currentName;
currentAppName = document.getString("appName");
Log.d(TAG, currentAppName + " - currentAppName");
Log.d(TAG, "banners/" + currentAppName.toLowerCase() + "Banner.png");
fstore.collection("apps").whereEqualTo("name", currentAppName).
get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
Log.d(TAG, imagePosition + " - imagePosition");
getBanner(task);
imagePosition++;
}
});
}
position++;
}
}
private void getBanner(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
Log.d(TAG, " - getting banner");
for (QueryDocumentSnapshot document : task.getResult()) {
currentBannerReference = document.getString("banner");
bannerReferences[imagePosition] = currentBannerReference;
Log.d(TAG, currentBannerReference + " - currentBannerReference");
if (imagePosition == mostPopularAccountsNames.length - 1) {
setRecycler();
}
}
}
}
private void setRecycler() {
Log.d(TAG, Arrays.toString(bannerReferences) + " - bannerReferences");
Log.d(TAG, Arrays.toString(mostPopularAccountsNames) + " - mostPopularAccountsNames");
Log.d(TAG, "Creating recycler");
RecyclerView creditRecycler = view.findViewById(R.id.credit_card_recycler);
MostPopularAccountsAdapter creditCardAdapter = new MostPopularAccountsAdapter
(mostPopularAccountsNames, bannerReferences, getContext());
creditRecycler.setAdapter(creditCardAdapter);
LinearLayoutManager cardManager = new LinearLayoutManager(view.getContext(),
LinearLayoutManager.HORIZONTAL, false);
creditRecycler.setLayoutManager(cardManager);
}
}
Logcat:
2021-03-22 13:47:59.729 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 0 - count of clicks VK2wvFS0kcy8DyoXqrWh
2021-03-22 13:47:59.730 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 0 - count of clicks d4tyNcgXrrk7ntZsBHy6
2021-03-22 13:47:59.730 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 1 - count of clicks tkGso4XSjZPnmVCH9MVs
2021-03-22 13:47:59.730 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 0 - count of clicks w13Q9oKlB1g1rgcpClbm
2021-03-22 13:47:59.730 5600-5600/com.lovejazz.kyle D/PasswordsFragment: [tkGso4XSjZPnmVCH9MVs=1, w13Q9oKlB1g1rgcpClbm=0, d4tyNcgXrrk7ntZsBHy6=0, VK2wvFS0kcy8DyoXqrWh=0] - maxCountOfClicks
2021-03-22 13:47:59.730 5600-5600/com.lovejazz.kyle D/PasswordsFragment: - maxCountOfClicks != 0
2021-03-22 13:48:00.011 5600-5600/com.lovejazz.kyle D/PasswordsFragment: tkGso4XSjZPnmVCH9MVs - current id
2021-03-22 13:48:00.011 5600-5600/com.lovejazz.kyle D/PasswordsFragment: - getting record info
2021-03-22 13:48:00.011 5600-5600/com.lovejazz.kyle D/PasswordsFragment: tkGso4XSjZPnmVCH9MVs - id of record
2021-03-22 13:48:00.012 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 0 - position
2021-03-22 13:48:00.012 5600-5600/com.lovejazz.kyle D/PasswordsFragment: example123 - currentName
2021-03-22 13:48:00.012 5600-5600/com.lovejazz.kyle D/PasswordsFragment: Telegram - currentAppName
2021-03-22 13:48:00.012 5600-5600/com.lovejazz.kyle D/PasswordsFragment: banners/telegramBanner.png
2021-03-22 13:48:00.013 5600-5600/com.lovejazz.kyle D/PasswordsFragment: w13Q9oKlB1g1rgcpClbm - current id
2021-03-22 13:48:00.013 5600-5600/com.lovejazz.kyle D/PasswordsFragment: - getting record info
2021-03-22 13:48:00.013 5600-5600/com.lovejazz.kyle D/PasswordsFragment: tkGso4XSjZPnmVCH9MVs - id of record
2021-03-22 13:48:00.013 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 1 - position
2021-03-22 13:48:00.013 5600-5600/com.lovejazz.kyle D/PasswordsFragment: example123 - currentName
2021-03-22 13:48:00.014 5600-5600/com.lovejazz.kyle D/PasswordsFragment: Telegram - currentAppName
2021-03-22 13:48:00.014 5600-5600/com.lovejazz.kyle D/PasswordsFragment: banners/telegramBanner.png
2021-03-22 13:48:00.014 5600-5600/com.lovejazz.kyle D/PasswordsFragment: d4tyNcgXrrk7ntZsBHy6 - current id
2021-03-22 13:48:00.014 5600-5600/com.lovejazz.kyle D/PasswordsFragment: - getting record info
2021-03-22 13:48:00.014 5600-5600/com.lovejazz.kyle D/PasswordsFragment: tkGso4XSjZPnmVCH9MVs - id of record
2021-03-22 13:48:00.014 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 2 - position
2021-03-22 13:48:00.015 5600-5600/com.lovejazz.kyle D/PasswordsFragment: example123 - currentName
2021-03-22 13:48:00.015 5600-5600/com.lovejazz.kyle D/PasswordsFragment: Telegram - currentAppName
2021-03-22 13:48:00.015 5600-5600/com.lovejazz.kyle D/PasswordsFragment: banners/telegramBanner.png
2021-03-22 13:48:00.016 5600-5600/com.lovejazz.kyle D/PasswordsFragment: VK2wvFS0kcy8DyoXqrWh - current id
2021-03-22 13:48:00.016 5600-5600/com.lovejazz.kyle D/PasswordsFragment: - getting record info
2021-03-22 13:48:00.016 5600-5600/com.lovejazz.kyle D/PasswordsFragment: tkGso4XSjZPnmVCH9MVs - id of record
2021-03-22 13:48:00.016 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 3 - position
2021-03-22 13:48:00.017 5600-5600/com.lovejazz.kyle D/PasswordsFragment: example123 - currentName
2021-03-22 13:48:00.017 5600-5600/com.lovejazz.kyle D/PasswordsFragment: Telegram - currentAppName
2021-03-22 13:48:00.017 5600-5600/com.lovejazz.kyle D/PasswordsFragment: banners/telegramBanner.png
2021-03-22 13:48:00.270 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 0 - imagePosition
2021-03-22 13:48:00.270 5600-5600/com.lovejazz.kyle D/PasswordsFragment: - getting banner
2021-03-22 13:48:00.278 5600-5600/com.lovejazz.kyle D/PasswordsFragment: https://firebasestorage.googleapis.com/v0/b/kyle-ec562.appspot.com/o/banners%2FtgBanner.png?alt=media&token=cdff7dd6-9837-4393-b935-23bcaba5a06a - currentBannerReference
2021-03-22 13:48:00.278 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 1 - imagePosition
2021-03-22 13:48:00.278 5600-5600/com.lovejazz.kyle D/PasswordsFragment: - getting banner
2021-03-22 13:48:00.278 5600-5600/com.lovejazz.kyle D/PasswordsFragment: https://firebasestorage.googleapis.com/v0/b/kyle-ec562.appspot.com/o/banners%2FtgBanner.png?alt=media&token=cdff7dd6-9837-4393-b935-23bcaba5a06a - currentBannerReference
2021-03-22 13:48:00.279 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 2 - imagePosition
2021-03-22 13:48:00.279 5600-5600/com.lovejazz.kyle D/PasswordsFragment: - getting banner
2021-03-22 13:48:00.279 5600-5600/com.lovejazz.kyle D/PasswordsFragment: https://firebasestorage.googleapis.com/v0/b/kyle-ec562.appspot.com/o/banners%2FtgBanner.png?alt=media&token=cdff7dd6-9837-4393-b935-23bcaba5a06a - currentBannerReference
2021-03-22 13:48:00.279 5600-5600/com.lovejazz.kyle D/PasswordsFragment: 3 - imagePosition
2021-03-22 13:48:00.279 5600-5600/com.lovejazz.kyle D/PasswordsFragment: - getting banner
2021-03-22 13:48:00.281 5600-5600/com.lovejazz.kyle D/PasswordsFragment: https://firebasestorage.googleapis.com/v0/b/kyle-ec562.appspot.com/o/banners%2FtgBanner.png?alt=media&token=cdff7dd6-9837-4393-b935-23bcaba5a06a - currentBannerReference
2021-03-22 13:48:00.281 5600-5600/com.lovejazz.kyle D/PasswordsFragment: [https://firebasestorage.googleapis.com/v0/b/kyle-ec562.appspot.com/o/banners%2FtgBanner.png?alt=media&token=cdff7dd6-9837-4393-b935-23bcaba5a06a, https://firebasestorage.googleapis.com/v0/b/kyle-ec562.appspot.com/o/banners%2FtgBanner.png?alt=media&token=cdff7dd6-9837-4393-b935-23bcaba5a06a, https://firebasestorage.googleapis.com/v0/b/kyle-ec562.appspot.com/o/banners%2FtgBanner.png?alt=media&token=cdff7dd6-9837-4393-b935-23bcaba5a06a, https://firebasestorage.googleapis.com/v0/b/kyle-ec562.appspot.com/o/banners%2FtgBanner.png?alt=media&token=cdff7dd6-9837-4393-b935-23bcaba5a06a] - bannerReferences
2021-03-22 13:48:00.281 5600-5600/com.lovejazz.kyle D/PasswordsFragment: [example123, example123, example123, example123] - mostPopularAccountsNames
2021-03-22 13:48:00.281 5600-5600/com.lovejazz.kyle D/PasswordsFragment: Creating recycler
