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
11 changes: 10 additions & 1 deletion app/src/main/java/com/mobile/komyusagip/CreatePost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,20 @@ class CreatePost : AppCompatActivity() {
val description = addDescription.text?.toString()?.trim()
val typeOfCrime = spinnerSelectedValueTextView.text.toString()

if (description.isNullOrEmpty()) {
if (typeOfCrime.isNullOrEmpty() && description.isNullOrEmpty()) {
spinnerSelectedValueTextView.error = "Please select the type of crime"
addDescription.error = "Add description"
return
} else if (typeOfCrime.isNullOrEmpty()) {
spinnerSelectedValueTextView.error = "Please select the type of crime"
return
} else if (description.isNullOrEmpty()) {
addDescription.error = "Add description"
return
}



// Get current user ID
val userId = auth.currentUser?.uid
userId?.let { uid ->
Expand Down
68 changes: 56 additions & 12 deletions app/src/main/java/com/mobile/komyusagip/EditProfile.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.mobile.komyusagip

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.textfield.TextInputEditText
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.storage.FirebaseStorage
import de.hdodenhof.circleimageview.CircleImageView

class EditProfile : AppCompatActivity() {
Expand All @@ -19,11 +23,32 @@ class EditProfile : AppCompatActivity() {
private lateinit var profileLocation: TextView
private val auth = FirebaseAuth.getInstance()
private val db = FirebaseFirestore.getInstance()
private val storage = FirebaseStorage.getInstance()
private var imageUrl: String? = null

private val getContent = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
val selectedImageUri: Uri? = data?.data
selectedImageUri?.let {
// Handle the selected image URI here
uploadImageToStorage(selectedImageUri)
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_edit_profile)

profilePicture = findViewById(R.id.uploadImage)
profilePicture.setOnClickListener {
// Open gallery to select an image
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
getContent.launch(intent)
}

val backToProfileClick = findViewById<ImageButton>(R.id.backToProfile)
backToProfileClick.setOnClickListener {
finish()
Expand All @@ -33,9 +58,26 @@ class EditProfile : AppCompatActivity() {
saveProfileClick.setOnClickListener {
validateAndSubmit()
}

}

private fun uploadImageToStorage(imageUri: Uri) {
val userId = auth.currentUser?.uid
userId?.let {
val storageRef = storage.reference.child("profileImages").child(userId).child("profile.jpg")
val uploadTask = storageRef.putFile(imageUri)
uploadTask.addOnSuccessListener { taskSnapshot ->
val downloadUrlTask = taskSnapshot.storage.downloadUrl
downloadUrlTask.addOnSuccessListener { uri ->
imageUrl = uri.toString()
profilePicture.setImageURI(imageUri)
}.addOnFailureListener {
Toast.makeText(this, "Failed to upload image: ${it.message}", Toast.LENGTH_SHORT).show()
}
}.addOnFailureListener {
Toast.makeText(this, "Failed to upload image: ${it.message}", Toast.LENGTH_SHORT).show()
}
}
}

private fun validateAndSubmit() {
val entryFirstName = findViewById<TextInputEditText>(R.id.entry_first_name)
Expand Down Expand Up @@ -63,17 +105,20 @@ class EditProfile : AppCompatActivity() {
if (!profileQuerySnapshot.isEmpty) {
val profileDocument = profileQuerySnapshot.documents[0]

// Update the fields in the profile document
profileDocument.reference.update(mapOf(
// Update the fields in the profile document including imageUrl
val updateMap = mutableMapOf<String, Any>(
"username" to username,
"location" to location,
//"imageUrl" to imageUrl
)).addOnSuccessListener {
// Navigate back to the profile screen after updating
finish()
}.addOnFailureListener { e ->
Toast.makeText(this, "Failed to update profile: ${e.message}", Toast.LENGTH_SHORT).show()
}
"location" to location
)
imageUrl?.let { updateMap["imageUrl"] = it }

profileDocument.reference.update(updateMap)
.addOnSuccessListener {
// Navigate back to the profile screen after updating
finish()
}.addOnFailureListener { e ->
Toast.makeText(this, "Failed to update profile: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}.addOnFailureListener { e ->
Toast.makeText(this, "Failed to fetch profile: ${e.message}", Toast.LENGTH_SHORT).show()
Expand All @@ -87,4 +132,3 @@ class EditProfile : AppCompatActivity() {
}
}
}

113 changes: 89 additions & 24 deletions app/src/main/java/com/mobile/komyusagip/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ package com.mobile.komyusagip

import CrimeReport
import CrimeReportAdapter
import android.content.ContentValues.TAG

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.FirebaseFirestore

class HomeFragment : Fragment() {

private lateinit var recyclerView: RecyclerView
private lateinit var adapter: CrimeReportAdapter

private val auth = FirebaseAuth.getInstance()
private val db = FirebaseFirestore.getInstance()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand Down Expand Up @@ -67,33 +75,90 @@ class HomeFragment : Fragment() {
recyclerView.adapter = adapter
}


private fun loadMostRecentCrimeReports() {
// Implement logic to load most recent crime reports
val mostRecentCrimeReports = listOf(
CrimeReport("Theft", "Description of theft incident"),
CrimeReport("Assault", "Description of assault incident")
// Add more crime reports as needed
)
adapter.setData(mostRecentCrimeReports)
// Get the current user's ID
val userId = auth.currentUser?.uid

// Check if the user is authenticated
userId?.let { uid ->
// Reference to the user's "post" subcollection
val userPostRef = db.collection("user").document(uid).collection("post")

// Query Firestore to fetch crime reports in the user's area
userPostRef
.get()
.addOnSuccessListener { documents ->
val crimeReports = mutableListOf<CrimeReport>()

for (document in documents) {
val typeOfCrime = document.getString("typeOfCrime")
val description = document.getString("description")

typeOfCrime?.let { crimeType ->
description?.let { desc ->
val crimeReport = CrimeReport(crimeType, desc)
crimeReports.add(crimeReport)
}
}
}

// Set data to the adapter after populating the crimeReports list
adapter.setData(crimeReports)
}
.addOnFailureListener { exception ->
// Handle any errors
Log.e(TAG, "Error fetching crime reports: $exception")
}
}
}

private fun loadInYourAreaCrimeReports() {
// Implement logic to load crime reports in user's area
val inYourAreaCrimeReports = listOf(
CrimeReport("Vandalism", "Description of vandalism incident"),
CrimeReport("Burglary", "Description of burglary incident")
// Add more crime reports as needed
)
adapter.setData(inYourAreaCrimeReports)
}
// Get the current user's ID
val userId = auth.currentUser?.uid

// Check if the user is authenticated
userId?.let { uid ->
// Reference to the user's "post" subcollection
val userPostRef = db.collection("user").document(uid).collection("post")
val userProfileRef = db.collection("user").document(uid).collection("profiles")

// Query Firestore to fetch crime reports in the user's area
userPostRef
.get()
.addOnSuccessListener { documents ->
val crimeReports = mutableListOf<CrimeReport>()

for (document in documents) {
val typeOfCrime = document.getString("typeOfCrime")
val description = document.getString("description")

typeOfCrime?.let { crimeType ->
description?.let { desc ->
val crimeReport = CrimeReport(crimeType, desc)
crimeReports.add(crimeReport)
}
}
}

// Set data to the adapter after populating the crimeReports list
adapter.setData(crimeReports)
}
.addOnFailureListener { exception ->
// Handle any errors
Log.e(TAG, "Error fetching crime reports: $exception")
}
}
}

private fun loadCommunityWatchCrimeReports() {
// Implement logic to load crime reports for community watch
val communityWatchCrimeReports = listOf(
CrimeReport("Drug Trafficking", "Description of drug trafficking incident"),
CrimeReport("Hit and Run", "Description of hit and run incident")
// Add more crime reports as needed
)
adapter.setData(communityWatchCrimeReports)

private fun loadCommunityWatchCrimeReports() {
// Implement logic to load crime reports for community watch
val communityWatchCrimeReports = listOf(
CrimeReport("Drug Trafficking", "Description of drug trafficking incident"),
CrimeReport("Hit and Run", "Description of hit and run incident")
// Add more crime reports as needed
)
adapter.setData(communityWatchCrimeReports)
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/mobile/komyusagip/ProfileFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class ProfileFragment : Fragment() {
}

val userId = auth.currentUser?.uid
userId?.let {
db.collection("user").document(userId).get()
userId?.let { uid ->
db.collection("user").document(uid).get()
.addOnSuccessListener { userDocument ->
if (userDocument != null) {
val firstName = userDocument.getString("firstName") ?: ""
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_createpost.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/TypeOfCrime" />
app:layout_constraintTop_toBottomOf="@+id/TypeOfCrime"
android:dropDownVerticalOffset="39dp"/>

<TextView
android:id="@+id/selectedSpinnerTypeOfCrime"
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/layout/activity_edit_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>

<ImageButton
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/uploadImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -56,7 +56,7 @@
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayoutProfile"
app:srcCompat="@drawable/image_upload" />
android:src="@drawable/image_upload" />

<TextView
android:id="@+id/editPhoto"
Expand Down Expand Up @@ -128,7 +128,7 @@
android:backgroundTintMode="add"
android:ems="10"
android:hint="@string/entry_updatedUsername"
android:inputType="textPassword"
android:inputType="textEmailAddress"
android:paddingBottom="10dp"
android:textColor="@color/white"
android:textColorHint="@color/hint_color"
Expand Down Expand Up @@ -210,7 +210,7 @@
android:backgroundTintMode="add"
android:ems="10"
android:hint="@string/entry_updatedLastName"
android:inputType="textPassword"
android:inputType="textEmailAddress"
android:paddingBottom="10dp"
android:textColor="@color/white"
android:textColorHint="@color/hint_color"
Expand Down Expand Up @@ -249,7 +249,7 @@
android:backgroundTintMode="add"
android:ems="10"
android:hint="@string/entry_updatedLocation"
android:inputType="textPassword"
android:inputType="textEmailAddress"
android:paddingBottom="10dp"
android:textColor="@color/white"
android:textColorHint="@color/hint_color"
Expand Down
Loading