|
| 1 | +package com.yashkasera.livstoryuploaddata.fragment; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.util.Log; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.AbsListView; |
| 10 | +import android.widget.Toast; |
| 11 | + |
| 12 | +import androidx.annotation.NonNull; |
| 13 | +import androidx.annotation.Nullable; |
| 14 | +import androidx.fragment.app.Fragment; |
| 15 | +import androidx.recyclerview.widget.DividerItemDecoration; |
| 16 | +import androidx.recyclerview.widget.LinearLayoutManager; |
| 17 | +import androidx.recyclerview.widget.RecyclerView; |
| 18 | + |
| 19 | +import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
| 20 | +import com.google.android.material.snackbar.BaseTransientBottomBar; |
| 21 | +import com.google.android.material.snackbar.Snackbar; |
| 22 | +import com.google.firebase.firestore.DocumentSnapshot; |
| 23 | +import com.google.firebase.firestore.FirebaseFirestore; |
| 24 | +import com.google.firebase.firestore.QueryDocumentSnapshot; |
| 25 | +import com.yashkasera.livstoryuploaddata.R; |
| 26 | +import com.yashkasera.livstoryuploaddata.adapter.SoundRecyclerAdapter; |
| 27 | +import com.yashkasera.livstoryuploaddata.model.SoundModel; |
| 28 | +import com.yashkasera.livstoryuploaddata.util.ClickListener; |
| 29 | +import com.yashkasera.livstoryuploaddata.util.RecyclerTouchListener; |
| 30 | + |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | +import java.util.Objects; |
| 35 | + |
| 36 | +import static com.yashkasera.livstoryuploaddata.MainActivity.lastVisibleSound; |
| 37 | +import static com.yashkasera.livstoryuploaddata.MainActivity.playPosition; |
| 38 | +import static com.yashkasera.livstoryuploaddata.MainActivity.soundModelArrayList; |
| 39 | + |
| 40 | +public class PlayAudioFragment extends Fragment { |
| 41 | + private static final String TAG = "PlayAudioFragment"; |
| 42 | + private final int limit = 10; |
| 43 | + SoundRecyclerAdapter soundRecyclerAdapter; |
| 44 | + FirebaseFirestore db = FirebaseFirestore.getInstance(); |
| 45 | + private Context context; |
| 46 | + private boolean isScrolling = false; |
| 47 | + private boolean isLastItemReached = false; |
| 48 | + |
| 49 | + public PlayAudioFragment() { |
| 50 | + } |
| 51 | + |
| 52 | + @Nullable |
| 53 | + @org.jetbrains.annotations.Nullable |
| 54 | + @Override |
| 55 | + public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable ViewGroup container, |
| 56 | + @Nullable Bundle savedInstanceState) { |
| 57 | + context = requireContext(); |
| 58 | + return inflater.inflate(R.layout.fragment_play_audio, container, false); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onViewCreated(@NonNull @NotNull View view, @Nullable Bundle savedInstanceState) { |
| 63 | + super.onViewCreated(view, savedInstanceState); |
| 64 | + RecyclerView recyclerView = view.findViewById(R.id.recyclerView); |
| 65 | + soundRecyclerAdapter = new SoundRecyclerAdapter(context); |
| 66 | + recyclerView.setLayoutManager(new LinearLayoutManager(context)); |
| 67 | + recyclerView.setAdapter(soundRecyclerAdapter); |
| 68 | + DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, |
| 69 | + DividerItemDecoration.VERTICAL); |
| 70 | + recyclerView.addItemDecoration(dividerItemDecoration); |
| 71 | + if (soundModelArrayList.size() == 0) { |
| 72 | + soundRecyclerAdapter.addItem(null); |
| 73 | + db.collection("sounds") |
| 74 | + .orderBy("type") |
| 75 | + .limit(limit) |
| 76 | + .get() |
| 77 | + .addOnCompleteListener(task -> { |
| 78 | + if (task.isSuccessful()) { |
| 79 | + soundRecyclerAdapter.removeItem(null); |
| 80 | + for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) { |
| 81 | + SoundModel soundModel = new SoundModel( |
| 82 | + document.getId(), |
| 83 | + document.getString("type"), |
| 84 | + document.getString("media"), |
| 85 | + (List<String>) document.get("keywords") |
| 86 | + ); |
| 87 | + soundRecyclerAdapter.addItem(soundModel); |
| 88 | + } |
| 89 | + lastVisibleSound = task.getResult().getDocuments().get(task.getResult().size() - 1); |
| 90 | + } else { |
| 91 | + soundRecyclerAdapter.removeItem(null); |
| 92 | + Log.w(TAG, "Error getting documents.", task.getException()); |
| 93 | + if (view != null) |
| 94 | + Snackbar.make(view.findViewById(android.R.id.content), "Unable to fetch records!", |
| 95 | + BaseTransientBottomBar.LENGTH_INDEFINITE).show(); |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() { |
| 100 | + @Override |
| 101 | + public void onScrollStateChanged(@NotNull RecyclerView recyclerView, int newState) { |
| 102 | + super.onScrollStateChanged(recyclerView, newState); |
| 103 | + if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { |
| 104 | + isScrolling = true; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy) { |
| 110 | + super.onScrolled(recyclerView, dx, dy); |
| 111 | + LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager()); |
| 112 | + int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition(); |
| 113 | + int visibleItemCount = linearLayoutManager.getChildCount(); |
| 114 | + int totalItemCount = linearLayoutManager.getItemCount(); |
| 115 | + if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && |
| 116 | + !isLastItemReached && !soundModelArrayList.contains(null)) { |
| 117 | + isScrolling = false; |
| 118 | + soundRecyclerAdapter.addItem(null); |
| 119 | + db.collection("sounds") |
| 120 | + .orderBy("type") |
| 121 | + .startAfter(lastVisibleSound).limit(limit) |
| 122 | + .get() |
| 123 | + .addOnCompleteListener(t -> { |
| 124 | + if (t.isSuccessful()) { |
| 125 | + soundRecyclerAdapter.removeItem(null); |
| 126 | + for (DocumentSnapshot document : Objects.requireNonNull(t.getResult())) { |
| 127 | + SoundModel soundModel = new SoundModel( |
| 128 | + document.getId(), |
| 129 | + document.getString("type"), |
| 130 | + document.getString("media"), |
| 131 | + (List<String>) document.get("keywords") |
| 132 | + ); |
| 133 | + soundRecyclerAdapter.addItem(soundModel); |
| 134 | + } |
| 135 | + lastVisibleSound = t.getResult().getDocuments().get(t.getResult().size() - 1); |
| 136 | + if (t.getResult().size() < limit) { |
| 137 | + isLastItemReached = true; |
| 138 | + } |
| 139 | + } else |
| 140 | + soundRecyclerAdapter.removeItem(null); |
| 141 | + }); |
| 142 | + } |
| 143 | + } |
| 144 | + }; |
| 145 | + recyclerView.addOnScrollListener(onScrollListener); |
| 146 | + recyclerView.addOnItemTouchListener(new RecyclerTouchListener(context, recyclerView, new ClickListener() { |
| 147 | + @Override |
| 148 | + public void onClick(View view, int position) { |
| 149 | + if (soundModelArrayList.get(position) != null) playPosition.setValue(position); |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void onLongClick(View view, int position) { |
| 155 | + if (soundModelArrayList.get(position) != null) |
| 156 | + new MaterialAlertDialogBuilder(context) |
| 157 | + .setTitle("Confirm Delete") |
| 158 | + .setMessage("Are you sure you want to delete this sound? This action cannot be undone.") |
| 159 | + .setPositiveButton("Delete", (dialog, which) -> db.collection("sounds") |
| 160 | + .document(soundModelArrayList.get(position).getId()) |
| 161 | + .delete() |
| 162 | + .addOnCompleteListener(task -> { |
| 163 | + if (task.isSuccessful()) { |
| 164 | + Toast.makeText(context, "Sound deleted Successfully!", Toast.LENGTH_SHORT).show(); |
| 165 | + soundRecyclerAdapter.notifyItemRemoved(position); |
| 166 | + soundModelArrayList.remove(position); |
| 167 | + } else { |
| 168 | + Toast.makeText(context, "Could not delete sound! Please try again later", Toast.LENGTH_SHORT).show(); |
| 169 | + } |
| 170 | + |
| 171 | + })) |
| 172 | + .setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss()) |
| 173 | + .show(); |
| 174 | + } |
| 175 | + })); |
| 176 | + |
| 177 | + |
| 178 | + } |
| 179 | +} |
0 commit comments