Guest User

Untitled

a guest
Feb 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.65 KB | None | 0 0
  1. public class MainFragment extends Fragment {
  2.  
  3. private RecyclerView broneListView;
  4. private RecyclerView imagesListView;
  5. private List<BronePost> brone_list;
  6. private List<User> users_list;
  7. private List<Imagens> image_list;
  8. private Button criaAds;
  9.  
  10. private FirebaseFirestore firebaseFirestore;
  11. private BroneRecyclerAdapter broneRecyclerAdapter;
  12. private ImageRecyclerAdapter imageRecyclerAdapter;
  13. private FirebaseAuth firebaseAuth;
  14.  
  15. private DocumentSnapshot lastVisible;
  16. private Boolean isFirstPageFirstLoad = true;
  17.  
  18. public MainFragment() {
  19. // Required empty public constructor
  20. }
  21.  
  22. @Override
  23. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  24. Bundle savedInstanceState) {
  25. // Inflate the layout for this fragment
  26. View view = inflater.inflate(R.layout.fragment_main, container, false);
  27.  
  28. firebaseAuth = FirebaseAuth.getInstance();
  29.  
  30. brone_list = new ArrayList<>();
  31. users_list = new ArrayList<>();
  32. broneListView = view.findViewById(R.id.post_list_view);
  33.  
  34. broneRecyclerAdapter = new BroneRecyclerAdapter(brone_list, users_list);
  35. broneListView.setLayoutManager(new LinearLayoutManager(getActivity()));
  36. broneListView.setAdapter(broneRecyclerAdapter);
  37. broneListView.setHasFixedSize(true);
  38. broneRecyclerAdapter.notifyDataSetChanged();
  39.  
  40. image_list = new ArrayList<>();
  41. imagesListView = view.findViewById(R.id.list_image_view);
  42.  
  43. imageRecyclerAdapter = new ImageRecyclerAdapter(image_list);
  44. imagesListView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
  45. imagesListView.setAdapter(imageRecyclerAdapter);
  46. imagesListView.addItemDecoration(new LinePagerIndicatorDecoration());
  47. imagesListView.setHasFixedSize(true);
  48. imageRecyclerAdapter.notifyDataSetChanged();
  49.  
  50. if (firebaseAuth.getCurrentUser() != null) {
  51.  
  52. firebaseFirestore = FirebaseFirestore.getInstance();
  53.  
  54. broneListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  55. @Override
  56. public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
  57. super.onScrolled(recyclerView, dx, dy);
  58.  
  59. Boolean reachedBottom = !recyclerView.canScrollVertically(-1);
  60.  
  61. if (reachedBottom) {
  62. loadMoreQuery();
  63. }
  64. }
  65. });
  66.  
  67. Query firstQuery = firebaseFirestore.collection("Posts").orderBy("timestamp", Query.Direction.ASCENDING).limit(4);
  68. firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
  69. @Override
  70. public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
  71.  
  72. if (!queryDocumentSnapshots.isEmpty()) {
  73.  
  74. if (isFirstPageFirstLoad) {
  75.  
  76. lastVisible = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1);
  77. brone_list.clear();
  78. users_list.clear();
  79. image_list.clear();
  80. }
  81. for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
  82.  
  83. if (doc.getType() == DocumentChange.Type.ADDED) {
  84.  
  85. final String bronePostId = doc.getDocument().getId();
  86. final BronePost bronePost = doc.getDocument().toObject(BronePost.class).withId(bronePostId);
  87.  
  88. String broneUserId = doc.getDocument().getString("user_id");
  89.  
  90. if (broneUserId != null) {
  91. firebaseFirestore.collection("Users").document(broneUserId).get()
  92. .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
  93. @Override
  94. public void onComplete(final Task<DocumentSnapshot> task) {
  95. if (task.isSuccessful()) {
  96.  
  97. firebaseFirestore.collection("Posts/" + bronePostId + "/Images").addSnapshotListener(new EventListener<QuerySnapshot>() {
  98. @Override
  99. public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
  100. if (!queryDocumentSnapshots.isEmpty()) {
  101.  
  102. for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
  103. if (doc.getType() == DocumentChange.Type.ADDED) {
  104.  
  105. User user = task.getResult().toObject(User.class);
  106.  
  107. Imagens imagens = doc.getDocument().toObject(Imagens.class);
  108.  
  109. if (isFirstPageFirstLoad) {
  110. users_list.add(user);
  111. brone_list.add(bronePost);
  112. image_list.add(imagens);
  113.  
  114. } else {
  115.  
  116. users_list.add(0, user);
  117. brone_list.add(0, bronePost);
  118. image_list.add(0, imagens);
  119. }
  120.  
  121. }
  122. }
  123. }
  124. }
  125. });
  126.  
  127. broneRecyclerAdapter.notifyDataSetChanged();
  128. imageRecyclerAdapter.notifyDataSetChanged();
  129. }
  130. }
  131. });
  132. }
  133. }
  134. }
  135. isFirstPageFirstLoad = false;
  136. }
  137. }
  138. });
  139. }
  140.  
  141. criaAds = view.findViewById(R.id.btn_criar_anuncio);
  142. criaAds.setOnClickListener(new View.OnClickListener() {
  143. @Override
  144. public void onClick(View v) {
  145. onCriarAnuncio();
  146. }
  147. });
  148. return view;
  149. }
  150.  
  151.  
  152. public void onCriarAnuncio(){
  153. //Intent intent = new Intent(MainActivity.this, CriarAnuncioActivity.class);
  154. //ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeCustomAnimation(getApplicationContext(), R.anim.fade_in, R.anim.mover_direita);
  155. //ActivityCompat.startActivity(MainActivity.this, intent, activityOptionsCompat.toBundle());
  156. startActivity(new Intent(getContext(), ZipCodeActivity.class));
  157. getActivity().finish();
  158. }
  159.  
  160. public void loadMoreQuery(){
  161.  
  162. if (firebaseAuth.getCurrentUser() != null) {
  163.  
  164. Query nextQuery = firebaseFirestore.collection("Posts")
  165. .orderBy("timestamp", Query.Direction.ASCENDING)
  166. .startAfter(lastVisible)
  167. .limit(4);
  168.  
  169. nextQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
  170. @Override
  171. public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
  172.  
  173. if (!queryDocumentSnapshots.isEmpty()) {
  174.  
  175. lastVisible = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1);
  176.  
  177. for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
  178.  
  179. if (doc.getType() == DocumentChange.Type.ADDED) {
  180.  
  181. final String bronePostId = doc.getDocument().getId();
  182. final BronePost bronePost = doc.getDocument().toObject(BronePost.class).withId(bronePostId);
  183. String broneUserId = doc.getDocument().getString("user_id");
  184.  
  185. if (broneUserId != null) {
  186. firebaseFirestore.collection("Users").document(broneUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
  187. @Override
  188. public void onComplete(final Task<DocumentSnapshot> task) {
  189. if (task.isSuccessful()){
  190.  
  191. firebaseFirestore.collection("Posts/" + bronePostId + "/Images").addSnapshotListener(new EventListener<QuerySnapshot>() {
  192. @Override
  193. public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
  194.  
  195. for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
  196. if (doc.getType() == DocumentChange.Type.ADDED) {
  197.  
  198. User user = task.getResult().toObject(User.class);
  199. Imagens imagens = doc.getDocument().toObject(Imagens.class);
  200.  
  201.  
  202. users_list.add(user);
  203. brone_list.add(bronePost);
  204.  
  205. image_list.add(imagens);
  206.  
  207. broneRecyclerAdapter.notifyDataSetChanged();
  208. }
  209. }
  210. }
  211. });
  212. }
  213. }
  214. });
  215. }
  216. }
  217. }
  218. }
  219. }
  220. });
  221. }
  222. }
  223. }
  224.  
  225. import android.content.Context;
  226. import android.support.v7.widget.RecyclerView;
  227. import android.view.LayoutInflater;
  228. import android.view.View;
  229. import android.view.ViewGroup;
  230. import android.widget.TextView;
  231.  
  232. import com.brone.brone.R;
  233. import com.brone.brone.model.BronePost;
  234. import com.brone.brone.model.User;
  235. import com.google.firebase.auth.FirebaseAuth;
  236. import com.google.firebase.firestore.FirebaseFirestore;
  237.  
  238. import java.util.List;
  239.  
  240. public class BroneRecyclerAdapter extends RecyclerView.Adapter<BroneRecyclerAdapter.ViewHolder> {
  241.  
  242. public List<BronePost> broneList;
  243. public List<User> user_list;
  244. public Context context;
  245.  
  246. private FirebaseFirestore firebaseFirestore;
  247. private FirebaseAuth firebaseAuth;
  248.  
  249. public BroneRecyclerAdapter(List<BronePost> brone_list, List<User> user_list){
  250. this.broneList = brone_list;
  251. this.user_list = user_list;
  252. }
  253.  
  254. @Override
  255. public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
  256.  
  257. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_list_item, parent, false);
  258.  
  259. context = parent.getContext();
  260. firebaseFirestore = FirebaseFirestore.getInstance();
  261. firebaseAuth = FirebaseAuth.getInstance();
  262. return new ViewHolder(view);
  263. }
  264.  
  265. @Override
  266. public void onBindViewHolder(final ViewHolder holder, final int position) {
  267.  
  268. holder.setIsRecyclable(false);
  269.  
  270. String[] endereco = broneList.get(position).getAddress().split("¬");
  271.  
  272. String type = broneList.get(position).getType();
  273.  
  274. holder.nameView.setText(type+" - "+endereco[3]);
  275.  
  276. }
  277.  
  278. @Override
  279. public int getItemCount() {
  280. return broneList.size();
  281. }
  282.  
  283. public class ViewHolder extends RecyclerView.ViewHolder{
  284.  
  285. private View view;
  286. private TextView rentView, nameView, enderecoView;
  287. // private ImageView broneLikeBtn;
  288. //private CardView cardView;
  289.  
  290. public ViewHolder(View itemView) {
  291. super(itemView);
  292. view = itemView;
  293.  
  294. nameView = view.findViewById(R.id.name_view);
  295.  
  296. }
  297.  
  298. public void setDesText(String descText){
  299. rentView = view.findViewById(R.id.rent_view);
  300. rentView.setText(descText);
  301. }
  302. }
  303. }
  304.  
  305. import android.content.Context;
  306. import android.support.v7.widget.RecyclerView;
  307. import android.view.LayoutInflater;
  308. import android.view.View;
  309. import android.view.ViewGroup;
  310. import android.widget.ImageView;
  311.  
  312. import com.brone.brone.R;
  313. import com.brone.brone.model.Imagens;
  314. import com.bumptech.glide.Glide;
  315.  
  316. import java.util.List;
  317.  
  318. public class ImageRecyclerAdapter extends RecyclerView.Adapter<ImageRecyclerAdapter.ViewHolder> {
  319.  
  320. public List<Imagens> image;
  321. public Context context;
  322.  
  323. public ImageRecyclerAdapter(List<Imagens> image) {
  324. this.image = image;
  325. }
  326.  
  327. @Override
  328. public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
  329.  
  330. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.multiple_list_itens, parent, false);
  331. context = parent.getContext();
  332. return new ViewHolder(view);
  333.  
  334. }
  335.  
  336. @Override
  337. public void onBindViewHolder(final ViewHolder holder, int position) {
  338.  
  339. holder.setIsRecyclable(true);
  340.  
  341. String imageUrl = image.get(position).getImage();
  342.  
  343. holder.setBroneImage(imageUrl);
  344.  
  345. }
  346.  
  347. @Override
  348. public int getItemCount() {
  349. return image.size();
  350. }
  351.  
  352. public class ViewHolder extends RecyclerView.ViewHolder {
  353.  
  354. private View view;
  355. private ImageView imageView, broneLikeBtn;
  356.  
  357. public ViewHolder(View itemView) {
  358. super(itemView);
  359. view = itemView;
  360.  
  361. broneLikeBtn = view.findViewById(R.id.brone_like_btn);
  362.  
  363. }
  364.  
  365. public void setBroneImage(String image_url) {
  366.  
  367. imageView = view.findViewById(R.id.image_view);
  368. Glide.with(context).load(image_url)
  369. .into(imageView);
  370. }
  371. }
  372. }
  373.  
  374. <?xml version="1.0" encoding="utf-8"?>
  375. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  376. xmlns:app="http://schemas.android.com/apk/res-auto"
  377. xmlns:tools="http://schemas.android.com/tools"
  378. android:layout_width="match_parent"
  379. android:layout_height="match_parent"
  380. tools:context=".fragment.MainFragment">
  381.  
  382. <RelativeLayout
  383. android:layout_width="match_parent"
  384. android:layout_height="match_parent">
  385.  
  386. <android.support.v7.widget.RecyclerView
  387. android:id="@+id/post_list_view"
  388. android:layout_width="match_parent"
  389. android:layout_height="match_parent"
  390. android:layout_above="@+id/btn_criar_anuncio"
  391. app:layout_constraintBottom_toTopOf="@id/btn_criar_anuncio" />
  392.  
  393. <Button
  394. android:id="@+id/btn_criar_anuncio"
  395. android:layout_width="match_parent"
  396. android:layout_height="wrap_content"
  397. android:background="@drawable/selector_button"
  398. android:focusable="true"
  399. android:fontFamily="@font/ubuntu"
  400. android:gravity="center"
  401. android:text="Criar anúncio agora"
  402. android:textAllCaps="false"
  403. android:layout_alignParentBottom="true"
  404. android:textColor="@color/colorAccent"
  405. android:visibility="visible" />
  406.  
  407. </RelativeLayout>
  408.  
  409. </FrameLayout>
  410.  
  411. <?xml version="1.0" encoding="utf-8"?>
  412. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  413. xmlns:app="http://schemas.android.com/apk/res-auto"
  414. xmlns:tools="http://schemas.android.com/tools"
  415. android:layout_width="match_parent"
  416. android:layout_height="wrap_content"
  417. android:padding="20dp">
  418.  
  419. <android.support.v7.widget.CardView
  420. android:id="@+id/card_image"
  421. android:layout_width="match_parent"
  422. android:layout_height="match_parent"
  423. android:padding="20dp"
  424. app:cardCornerRadius="2dp"
  425. app:cardElevation="2dp"
  426. app:layout_constraintBottom_toBottomOf="parent"
  427. app:layout_constraintEnd_toEndOf="parent"
  428. app:layout_constraintStart_toStartOf="parent"
  429. app:layout_constraintTop_toTopOf="parent">
  430.  
  431. <android.support.constraint.ConstraintLayout
  432. android:layout_width="match_parent"
  433. android:layout_height="match_parent">
  434.  
  435. <android.support.v7.widget.RecyclerView
  436. android:id="@+id/list_image_view"
  437. android:layout_width="match_parent"
  438. android:layout_height="wrap_content"
  439. app:layout_constraintEnd_toEndOf="parent"
  440. app:layout_constraintStart_toStartOf="parent"
  441. app:layout_constraintTop_toTopOf="parent" />
  442.  
  443. <TextView
  444. android:id="@+id/name_view"
  445. android:layout_width="wrap_content"
  446. android:layout_height="wrap_content"
  447. android:layout_marginStart="8dp"
  448. android:layout_marginTop="8dp"
  449. android:fontFamily="@font/ubuntu"
  450. android:textStyle="bold"
  451. app:layout_constraintStart_toStartOf="parent"
  452. app:layout_constraintTop_toBottomOf="@+id/list_image_view" />
  453.  
  454. <TextView
  455. android:id="@+id/rent_view"
  456. android:layout_width="wrap_content"
  457. android:layout_height="wrap_content"
  458. android:layout_marginTop="8dp"
  459. app:layout_constraintStart_toStartOf="@+id/name_view"
  460. app:layout_constraintTop_toBottomOf="@+id/name_view" />
  461.  
  462. </android.support.constraint.ConstraintLayout>
  463.  
  464. </android.support.v7.widget.CardView>
  465.  
  466. </android.support.constraint.ConstraintLayout>
  467.  
  468. <?xml version="1.0" encoding="utf-8"?>
  469. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  470. android:layout_width="match_parent"
  471. android:layout_height="wrap_content"
  472. xmlns:app="http://schemas.android.com/apk/res-auto">
  473.  
  474. <ImageView
  475. android:id="@+id/image_view"
  476. android:layout_width="match_parent"
  477. android:layout_height="240dp"
  478. android:scaleType="centerCrop"
  479. app:layout_constraintBottom_toBottomOf="parent"
  480. app:layout_constraintEnd_toEndOf="parent"
  481. app:layout_constraintHorizontal_bias="0.0"
  482. app:layout_constraintStart_toStartOf="parent"
  483. app:layout_constraintTop_toTopOf="parent"
  484. app:layout_constraintVertical_bias="0.0"
  485. app:srcCompat="@android:color/darker_gray" />
  486.  
  487. <ImageView
  488. android:id="@+id/brone_like_btn"
  489. android:layout_width="32dp"
  490. android:layout_height="32dp"
  491. android:layout_marginTop="8dp"
  492. android:layout_marginEnd="8dp"
  493. android:contentDescription="Amei"
  494. android:visibility="visible"
  495. app:layout_constraintEnd_toEndOf="parent"
  496. app:layout_constraintTop_toTopOf="@+id/image_view"
  497. app:srcCompat="@mipmap/ic_like_gray" />
  498.  
  499.  
  500. </android.support.constraint.ConstraintLayout>
Add Comment
Please, Sign In to add comment