Skip to content

Commit

Permalink
Mfw 5am commits:
Browse files Browse the repository at this point in the history
Added delay report system
Users can report bus delays or cancel their reported delay
Reports automatically get cleared everyday
Bumped version
  • Loading branch information
50t0r25 committed May 14, 2022
1 parent d3b8612 commit 53a54dc
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "dz.notacompany.el_cous"
minSdk 21
targetSdk 32
versionCode 1
versionName '0.1'
versionCode 2
versionName '0.9'

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
140 changes: 135 additions & 5 deletions app/src/main/java/dz/notacompany/el_cous/DetailsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@ package dz.notacompany.el_cous

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.firestore.QueryDocumentSnapshot
import com.google.firebase.firestore.SetOptions
import com.google.firebase.firestore.Source
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_details.*
import java.util.*
import kotlin.collections.ArrayList

class DetailsFragment(private val documentID : String) : Fragment(R.layout.fragment_details) {

private val db = Firebase.firestore
private lateinit var auth: FirebaseAuth
private lateinit var mainAct : MainActivity

private lateinit var source: Source

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

auth = Firebase.auth

mainAct = activity as MainActivity // Reference to MainActivity

mainAct.currentDocument = documentID // Used to delete Routes if admin is user
Expand All @@ -34,6 +44,98 @@ class DetailsFragment(private val documentID : String) : Fragment(R.layout.fragm

val schedulesList = mutableListOf<ScheduleItem>()

// Function will be passed to the adapter to run stuff that can't be run inside it otherwise
fun onScheduleClick(position : Int, textView : TextView) {

var dialogMessage = getString(R.string.add_report)
var isRemoving = false
if (schedulesList[position].userHasReported) {
dialogMessage = getString(R.string.remove_report)
isRemoving = true
}

MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.confirm))
.setMessage(dialogMessage)
.setNeutralButton(getString(R.string.cancel)) { dialog, _ ->
dialog.dismiss()
}
.setPositiveButton(getString(R.string.confirm)) { dialog, _ ->
dialog.dismiss()

val scheduleRef = db.collection("trajets").document(documentID).collection("horaires").document(schedulesList[position].itemID)
var reportsSize = 0

if (isRemoving) {

db.runTransaction { transaction ->
val thisSchedule = transaction.get(scheduleRef)
var newReports : ArrayList<String>? = arrayListOf()

val reports = thisSchedule.data?.get("retards") as ArrayList<String>?

reports?.let { newReports!!.addAll(it) }
newReports!!.remove(auth.uid.toString())

reportsSize = newReports.size

val cal = Calendar.getInstance()
var newDate : String? = "${cal.get(Calendar.MONTH)}/${cal.get(Calendar.DAY_OF_MONTH)}"

if (newReports.size == 0) {
newReports = null
newDate = null
}

val newData = hashMapOf(
"retards" to newReports,
"lastReport" to newDate
)

transaction.set(scheduleRef, newData, SetOptions.merge())

null
}.addOnSuccessListener {
textView.visibility = View.VISIBLE
schedulesList[position].userHasReported = false
textView.text = "${getString(R.string.reported_delays0)} $reportsSize ${getString(R.string.reported_delays1)}"
if (reportsSize == 0) textView.visibility = View.GONE
}

} else {

db.runTransaction { transaction ->
val thisSchedule = transaction.get(scheduleRef)
val newReports : ArrayList<String> = arrayListOf()

val reports = thisSchedule.data?.get("retards") as ArrayList<String>?

reports?.let { newReports.addAll(it) }
newReports.add(auth.uid.toString())

reportsSize = newReports.size

val cal = Calendar.getInstance()

val newData = hashMapOf(
"retards" to newReports,
"lastReport" to "${cal.get(Calendar.MONTH)}/${cal.get(Calendar.DAY_OF_MONTH)}"
)

transaction.set(scheduleRef, newData, SetOptions.merge())

null
}.addOnSuccessListener {
textView.visibility = View.VISIBLE
schedulesList[position].userHasReported = true
textView.text = "${getString(R.string.reported_delays0)} $reportsSize ${getString(R.string.reported_delays1)}"
}

}
}
.show()
}

mainAct.createLoadingDialog()

// If user doesn't have internet access, fetch data from cache
Expand All @@ -50,14 +152,34 @@ class DetailsFragment(private val documentID : String) : Fragment(R.layout.fragm
db.collection("trajets").document(documentID).collection("horaires").get(source)
.addOnSuccessListener { horaires ->

// Adds each Schudule object from the DB to our list
// Adds each schedule object from the DB to our list
for (horaire in horaires) {
addScheduleToList(horaire,schedulesList)

val cal = Calendar.getInstance()
val currentDate = "${cal.get(Calendar.MONTH)}/${cal.get(Calendar.DAY_OF_MONTH)}"

if (horaire.data["lastReport"] != null && horaire.data["lastReport"].toString() != currentDate) {
db.runBatch { batch ->

val horaireRef = db.collection("trajets").document(documentID).collection("horaires").document(horaire.id)
val newData = hashMapOf(
"lastReport" to null,
"retards" to null
)
batch.set(horaireRef, newData, SetOptions.merge())

addScheduleToList(horaire,schedulesList,true)
}
} else {
addScheduleToList(horaire,schedulesList,false)
}


}
schedulesList.sortBy { it.itemOrder } // Sorts the list by the itemOrder variable

// Initializes the RecyclerView with the adapter
schedulesRecyclerView.adapter = SchedulesAdapter(schedulesList)
schedulesRecyclerView.adapter = SchedulesAdapter(requireContext(), schedulesList, { position, textView -> onScheduleClick(position, textView)})
schedulesRecyclerView.layoutManager = LinearLayoutManager(context)

mainAct.dismissLoadingDialog()
Expand All @@ -67,12 +189,20 @@ class DetailsFragment(private val documentID : String) : Fragment(R.layout.fragm
}

// Function adds a schedule from the received object from the DB to a list of ScheduleItem
private fun addScheduleToList(horaire: QueryDocumentSnapshot, list: MutableList<ScheduleItem>) {
private fun addScheduleToList(horaire: QueryDocumentSnapshot, list: MutableList<ScheduleItem>, gotCleared : Boolean) {
val id = horaire.id
val order = horaire.data["ordre"].toString().toInt()
val departure = horaire.data["depart"].toString()
val arrival = horaire.data["arrive"].toString()

list.add(ScheduleItem(id,order,departure,arrival))
val retards = horaire.data["retards"] as ArrayList<String>?
var delays = 0
var userHasReported = false
if (retards != null && !gotCleared) {
delays = retards.size
if (retards.contains(auth.uid.toString())) userHasReported = true
}

list.add(ScheduleItem(id,order,departure,arrival,delays,userHasReported))
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/dz/notacompany/el_cous/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class MainActivity : AppCompatActivity() {
MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.caution))
.setMessage(getString(R.string.delete_route))
.setNeutralButton("Cancel") { dialog, _ ->
.setNeutralButton(getString(R.string.cancel)) { dialog, _ ->
dialog.dismiss()
}
.setPositiveButton(getString(R.string.confirm)) { dialog, _ ->
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/dz/notacompany/el_cous/ScheduleItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ data class ScheduleItem(
val itemID : String,
val itemOrder : Int,
val scheduleDepartureTime : String,
val scheduleArrivalTime : String
val scheduleArrivalTime : String,
val delays : Int,
var userHasReported : Boolean
)
13 changes: 12 additions & 1 deletion app/src/main/java/dz/notacompany/el_cous/SchedulesAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dz.notacompany.el_cous

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import dz.notacompany.el_cous.databinding.ItemScheduleBinding

class SchedulesAdapter(var schedulesList: List<ScheduleItem>) : RecyclerView.Adapter<SchedulesAdapter.SchedulesViewHolder>() {
class SchedulesAdapter(private val context : Context, private var schedulesList: List<ScheduleItem>, private val onScheduleClick : (position : Int, textView : TextView) -> Unit) : RecyclerView.Adapter<SchedulesAdapter.SchedulesViewHolder>() {

inner class SchedulesViewHolder(val binding: ItemScheduleBinding) : RecyclerView.ViewHolder(binding.root)

Expand All @@ -19,6 +22,14 @@ class SchedulesAdapter(var schedulesList: List<ScheduleItem>) : RecyclerView.Ada
override fun onBindViewHolder(holder: SchedulesViewHolder, position: Int) {
holder.binding.apply {

itemTripCard.setOnClickListener {
onScheduleClick(position, delaysTextView)
}

if (schedulesList[position].delays != 0) {
delaysTextView.visibility = View.VISIBLE //${schedulesList[position].delays}
delaysTextView.text = "${context.getString(R.string.reported_delays0)} ${schedulesList[position].delays} ${context.getString(R.string.reported_delays1)}"
}
cousNumberTextView.text = "Cous N°".plus(schedulesList[position].itemOrder)
departureTimeTextView.text = schedulesList[position].scheduleDepartureTime
arrivalTimeTextView.text = schedulesList[position].scheduleArrivalTime
Expand Down
24 changes: 19 additions & 5 deletions app/src/main/res/layout/item_schedule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
android:clickable="true"
android:focusable="true"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand All @@ -39,24 +38,24 @@
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="5dp"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cousNumberTextView"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/bus"/>
app:srcCompat="@drawable/bus" />

<TextView
android:id="@+id/cousNumberTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="@font/nunito_bold"
android:padding="5dp"
app:layout_constraintHorizontal_weight="1.5"
android:text="Cous N°X"
android:textSize="15sp"
android:fontFamily="@font/nunito_bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1.5"
app:layout_constraintStart_toEndOf="@+id/imageView7"
app:layout_constraintTop_toTopOf="parent" />

Expand Down Expand Up @@ -131,4 +130,19 @@
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

<TextView
android:id="@+id/delaysTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="@font/nunito_medium"
android:textAlignment="center"
android:text="@string/reported_delays0"
android:textColor="@color/second_text"
android:textSize="13sp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/itemTripCard" />

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@
<string name="cancel">Annuler</string>
<string name="caution">Avertissement</string>
<string name="delete_route">Êtes-vous sûr de vouloir definitivement supprimer ce trajet?</string>
<string name="reported_delays0">Retard signalé par</string>
<string name="reported_delays1">étudiant(s) aujourdhui</string>
<string name="add_report">Êtes-vous sûr de vouloir signaler un retard pour cet horaire?</string>
<string name="remove_report">Êtes-vous sûr de vouloir annuler votre signalement de retard pour cet horaire?</string>
</resources>

0 comments on commit 53a54dc

Please sign in to comment.