Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies {
implementation libs.play.services.location
implementation libs.volley
implementation 'androidx.databinding:library:3.2.0-alpha11'
implementation 'com.google.firebase:firebase-firestore-ktx:24.8.1'
testImplementation libs.junit
androidTestImplementation libs.androidx.test.ext.junit
androidTestImplementation libs.espresso.core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.avianwatch.R
import com.example.avianwatch.data.BirdObservation
import com.example.avianwatch.objects.Image
import com.example.avianwatch.data.ObservationItem

class ObservationAdapter(private val observation: List<ObservationItem>) : RecyclerView.Adapter<ObservationAdapter.ObservationViewHolder>() {

class ObservationAdapter(private val observations: List<BirdObservation>) : RecyclerView.Adapter<ObservationAdapter.ObservationViewHolder>() {
inner class ObservationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

val name: TextView = itemView.findViewById(R.id.tvBirdName)
val date: TextView = itemView.findViewById(R.id.tvDateObserved)
val bird_image: ImageView = itemView.findViewById((R.id.imgBirdImage))
val location: TextView = itemView.findViewById(R.id.txtLocation)

fun bind(observation: ObservationItem) {
name.text = observation.birdName
date.text = observation.date.toString()
location.text = observation.location
if (observation.image.equals("")) {
bird_image.visibility = View.INVISIBLE
fun bind(observation: BirdObservation) {
name.text = observation.birdSpecies
date.text = observation.dateTime.toString()
//location.text = observation.hotspot.toString()
//have to implement location
location.text = observation.hotspot.locName
// Check if the image is null or empty, and hide the image view if it is
if (observation.birdImage.isNullOrEmpty()) {
bird_image.visibility = View.GONE
} else {
bird_image.visibility = View.VISIBLE
Image.setBase64Image(observation.image,bird_image)
Image.setBase64Image(observation.birdImage, bird_image)
}
}

Expand All @@ -36,11 +38,11 @@ class ObservationAdapter(private val observation: List<ObservationItem>) : Recyc
itemView.setOnClickListener {
val position = adapterPosition
if (position != RecyclerView.NO_POSITION) {
val bird = observation[position]
// Call the onItemClick method of the listener with the clicked observation
val bird = observations[position]
onItemClickListener?.onItemClick(bird)
}
}

}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ObservationViewHolder {
Expand All @@ -49,25 +51,19 @@ class ObservationAdapter(private val observation: List<ObservationItem>) : Recyc
}

override fun onBindViewHolder(holder: ObservationViewHolder, position: Int) {
val observation = observation[position]
val observation = observations[position]
holder.bind(observation)
}


override fun getItemCount(): Int {
return observation.size
}

fun updateObservationList(observationList: MutableList<ObservationItem>) {
var list: MutableList<ObservationItem> = observationList.toMutableList()
list.clear()
list.addAll(observationList)
notifyDataSetChanged()
return observations.size
}

// Define an interface for item click handling
interface OnItemClickListener {
fun onItemClick(bird: ObservationItem)
fun onItemClick(bird: BirdObservation)
}

private var onItemClickListener: OnItemClickListener? = null

// Setter for the click listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ data class BirdObservation(
val birdImage: String?,
val dateTime: String?,
val hotspot: Hotspot
)
){
constructor() : this("", "", "", "", null, null, Hotspot())
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/example/avianwatch/data/Hotspot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ data class Hotspot(
val comName: String,
val lat: Double,
val lng: Double,
)
){
constructor() : this("", "", "", 0.0, 0.0)
}
13 changes: 7 additions & 6 deletions app/src/main/java/com/example/avianwatch/data/ObservationItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package com.example.avianwatch.data
import java.util.Date

data class ObservationItem(
var uid: String?,
val birdName: String,
val date: String?,
val location: String,
val notes: String,
val image: String?
var uid: String? = null,
val birdName: String = "",
val date: String? = null,
val location: String = "",
val notes: String = "",
val image: String? = null,
val hotspot: Hotspot? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ class ObservationFragment : Fragment(), OnMapReadyCallback {
UUID.randomUUID().toString(),
getCityAndSuburbNameFromLatLng(latLng),
binding.etBirdName.text.toString(),
0.0,//location.latitude,
0.0//location.longitude
latLng.latitude,
latLng.longitude

)

val observation = BirdObservation(
Global.currentUser?.uid.toString(), //Store UID to create relationship
oid,
Expand Down Expand Up @@ -177,7 +179,7 @@ class ObservationFragment : Fragment(), OnMapReadyCallback {
}
}
}
//}


} else {
// Request location permission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,77 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.avianwatch.MainActivity
import com.example.avianwatch.R
import com.example.avianwatch.adapters.ObservationAdapter
import com.example.avianwatch.objects.Image
import com.example.avianwatch.data.ObservationItem
import com.example.avianwatch.data.BirdObservation
import com.example.avianwatch.databinding.FragmentObservationListBinding
import com.google.firebase.auth.FirebaseAuth
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import com.google.firebase.database.*


class ObservationListFragment : Fragment(), ObservationAdapter.OnItemClickListener {
private lateinit var auth: FirebaseAuth
lateinit var userBirdObservations: MutableList<ObservationItem>
private lateinit var userId: String
private lateinit var userBirdObservations: MutableList<BirdObservation>
private lateinit var adapter: ObservationAdapter


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
auth = FirebaseAuth.getInstance()
userBirdObservations = mutableListOf()
adapter = ObservationAdapter(userBirdObservations)

// Get the current date
val calendar = Calendar.getInstance()

// Format the date to display in your desired format (e.g., "dd/MM/yyyy")
val dateFormat = SimpleDateFormat("dd MMMM yyyy HH:mm", Locale.getDefault())
val formattedDate = dateFormat.format(calendar.time)
// Get the current user's UID and store it in userId
val firebaseUser = auth.currentUser
userId = firebaseUser?.uid ?: ""

auth = FirebaseAuth.getInstance()
Log.d("ObservationListFragment", "User ID: $userId")
// Load user's observations from Firebase
loadUserObservations()
}
private fun loadUserObservations() {
// Get the current user's UID
val firebaseUser = auth.currentUser
val uid = firebaseUser?.uid

userBirdObservations = mutableListOf(

ObservationItem(
uid,
"Flamingo",
formattedDate,
"Halfway House, Midrand",
"A pair of Flamingos, they looked liked they were mating.",
Image.drawableToBase64(ContextCompat.getDrawable(requireContext(), R.mipmap.flamingo_pair)!!)

)
)
if (uid != null) {
// Initialize the Realtime Database reference
val databaseReference: DatabaseReference = FirebaseDatabase.getInstance().reference.child("BirdObservation")

databaseReference.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val userObservations: MutableList<BirdObservation> = mutableListOf()

for (snapshot in dataSnapshot.children) {
val birdObservation = snapshot.getValue(BirdObservation::class.java)

// Check if the "userID" matches the current user's UID
if (birdObservation != null && birdObservation.userID == uid) {
Log.d("ObservationListFragment", "Additional Notes: ${birdObservation.additionalNotes}")
userObservations.add(birdObservation)
}
}

// Log the size of userObservations
Log.d("ObservationListFragment", "User Observations Size: ${userObservations.size}")

// Notify the adapter of the data change and pass the filtered list
userBirdObservations.clear()
userBirdObservations.addAll(userObservations)
adapter.notifyDataSetChanged()
}

override fun onCancelled(databaseError: DatabaseError) {
Log.e("ObservationListFragment", "Database Error: ${databaseError.message}")
}
})
} else {
Log.e("ObservationListFragment", "User is not authenticated.")
}
}

override fun onCreateView(
Expand Down Expand Up @@ -85,27 +111,20 @@ class ObservationListFragment : Fragment(), ObservationAdapter.OnItemClickListen
val plantLayoutManager = LinearLayoutManager(requireContext())
lstBirds.layoutManager = plantLayoutManager

try{
// Create an instance of PlantAdapter and pass the OnItemClickListener
val adapter = ObservationAdapter(userBirdObservations)
adapter.setOnItemClickListener(this)
// Set the adapter to the RecyclerView
lstBirds.adapter = adapter
}catch (e:Exception){
Toast.makeText(activity,e.message,Toast.LENGTH_SHORT).show()
Log.d(ContentValues.TAG, e.message.toString())
}
// Set the adapter to the RecyclerView
lstBirds.adapter = adapter

}

override fun onItemClick(bird: ObservationItem) {
override fun onItemClick(bird: BirdObservation) {
// Handle the click event and navigate to a different fragment
//Add data to bundle
val bundle = Bundle()
bundle.putString("bird_name", bird.birdName)
bundle.putString("date", bird.date.toString())
bundle.putString("location", bird.location)
bundle.putString("notes", bird.notes)
bundle.putString("imageData", bird.image)
bundle.putString("bird_name", bird.birdSpecies)
bundle.putString("date", bird.dateTime.toString())
bundle.putString("location", bird.hotspot.toString())
bundle.putString("notes", bird.additionalNotes)
bundle.putString("imageData", bird.birdImage)

try{
val fragment = ObservationListFragment()
Expand Down