-
Notifications
You must be signed in to change notification settings - Fork 0
/
editprofilescreen.kt
209 lines (181 loc) · 8.15 KB
/
editprofilescreen.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.muhammadahmad.i210790
import BaseActivity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.*
import java.io.ByteArrayOutputStream
class editprofilescreen : BaseActivity() {
private lateinit var editTextName: EditText
private lateinit var editTextEmail: EditText
private lateinit var editTextPhoneNumber: EditText
private lateinit var editTextCountry: EditText
private lateinit var editTextCity: EditText
private lateinit var btnUploadPicture: TextView
private lateinit var btnUploadVideo: ImageView
private lateinit var firebaseAuth: FirebaseAuth
private lateinit var firebaseDatabase: FirebaseDatabase
private lateinit var databaseReference: DatabaseReference
private var sImage: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_editprofilescreen)
// Initialize EditText fields
editTextName = findViewById(R.id.editprofile_name)
editTextEmail = findViewById(R.id.editprofile_email)
editTextPhoneNumber = findViewById(R.id.editprofile_phonenumber)
editTextCountry = findViewById(R.id.editprofile_country)
editTextCity = findViewById(R.id.editprofile_city)
btnUploadPicture = findViewById(R.id.btnUploadPicture)
btnUploadVideo = findViewById(R.id.btnUploadVideo)
btnUploadVideo.visibility = View.GONE // Hide the video upload button
// Initialize Firebase instances
firebaseAuth = FirebaseAuth.getInstance()
firebaseDatabase = FirebaseDatabase.getInstance()
databaseReference = firebaseDatabase.reference.child("users")
// Fetch and display user data
fetchUserDataFromDatabase()
// Set click listener for the Update Profile button
findViewById<TextView>(R.id.textView7).setOnClickListener {
updateProfile()
val userName = findViewById<EditText>(R.id.editprofile_name)
intent.putExtra("name", userName.text)
}
findViewById<TextView>(R.id.textView15).setOnClickListener{
val userNam = intent.getStringExtra("name")
// Navigate back to the aboutme screen with the user's name
val intent = Intent(this, mainmenu::class.java).apply {
putExtra("name", userNam)
}
startActivity(intent)
}
// Set click listener for the upload picture button
btnUploadPicture.setOnClickListener {
val myfileintent = Intent(Intent.ACTION_GET_CONTENT)
myfileintent.type = "image/*"
ActivityResultLauncher.launch(myfileintent)
}
}
private val ActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val uri = result.data?.data
try {
val inputStream = contentResolver.openInputStream(uri!!)
val myBitmap = BitmapFactory.decodeStream(inputStream)
val stream = ByteArrayOutputStream()
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
val bytes = stream.toByteArray()
sImage = android.util.Base64.encodeToString(bytes, android.util.Base64.DEFAULT)
// Display the selected image in the ImageView
// In the onActivityResult method, set the selected bitmap to the ImageView
btnUploadVideo.setImageBitmap(myBitmap)
inputStream?.close()
Toast.makeText(this, "Image Selected", Toast.LENGTH_SHORT).show()
} catch (ex: Exception) {
Toast.makeText(this, ex.message.toString(), Toast.LENGTH_SHORT).show()
}
}
}
private fun fetchUserDataFromDatabase() {
val user = firebaseAuth.currentUser
user?.let { currentUser ->
val uid = currentUser.uid
val query = databaseReference.child(uid)
query.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists()) {
val userData = dataSnapshot.getValue(UserData::class.java)
userData?.let { user ->
editTextName.setText(user.name)
editTextEmail.setText(user.email)
editTextPhoneNumber.setText(user.number)
editTextCountry.setText(user.country)
editTextCity.setText(user.city)
}
} else {
Toast.makeText(
this@editprofilescreen,
"User data not found",
Toast.LENGTH_SHORT
).show()
}
}
override fun onCancelled(databaseError: DatabaseError) {
Toast.makeText(
this@editprofilescreen,
"Error fetching user data",
Toast.LENGTH_SHORT
).show()
}
})
}
}
private fun updateProfile() {
// Get updated profile data from EditText fields
val name = editTextName.text.toString().trim()
val email = editTextEmail.text.toString().trim()
val contactNumber = editTextPhoneNumber.text.toString().trim()
val country = editTextCountry.text.toString().trim()
val city = editTextCity.text.toString().trim()
// Update the user's profile data in the database
val user = firebaseAuth.currentUser
user?.let {
val userData = mapOf(
"name" to name,
"email" to email,
"contactNumber" to contactNumber,
"country" to country,
"city" to city,
"profileImage" to sImage // Add profile image data to the user data
)
databaseReference.child(it.uid).updateChildren(userData)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(
this@editprofilescreen,
"Profile updated successfully",
Toast.LENGTH_SHORT
).show()
// Navigate to the main menu screen
val intent = Intent(this@editprofilescreen, mainmenu::class.java)
intent.putExtra("updatedName", name)
startActivity(intent)
finish() // Close the current activity to prevent going back to it when pressing back button
} else {
Toast.makeText(
this@editprofilescreen,
"Failed to update profile",
Toast.LENGTH_SHORT
).show()
}
}
}
}
fun back_editprofilescreen(view: View)
{
val userNam = intent.getStringExtra("name")
// Navigate back to the aboutme screen with the user's name
val intent = Intent(this, mainmenu::class.java).apply {
putExtra("name", userNam)
}
startActivity(intent)
}
fun bookedsessions(view: View)
{
val userNam = intent.getStringExtra("name")
// Navigate back to the aboutme screen with the user's name
val intent = Intent(this, bookedsessionscreen::class.java).apply {
putExtra("name", userNam)
}
startActivity(intent)
}
}