Ticker

6/recent/ticker-posts

Header Ads Widget

JustMarkets

 // PostRepository.kt

package com.search2learn.s2l
import android.net.Uri
import com.google.firebase.firestore.ListenerRegistration

import com.google.firebase.firestore.FieldValue
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.storage.FirebaseStorage
import kotlinx.coroutines.tasks.await
import android.util.Log
import com.google.firebase.ktx.Firebase
import com.google.firebase.storage.ktx.storage
import java.util.UUID

class PostRepository {
private val db = FirebaseFirestore.getInstance()
private val storage = FirebaseStorage.getInstance()
private val postsCollection = db.collection("posts")

suspend fun getPosts(): List<Post> {
return postsCollection
.orderBy("postTime", Query.Direction.DESCENDING)
.get()
.await()
.toObjects(Post::class.java)
.map { it.copy(id = it.id) }
}

// للحصول على منشورات المتابَعين
suspend fun getFollowingPosts(userId: String): List<Post> {
// تحتاج إلى تنفيذ منطق المتابعة
// مثال مبسط:
val following = db.collection("users")
.document(userId)
.get()
.await()
.get("following") as? List<String> ?: emptyList()

return postsCollection
.whereIn("userId", following)
.orderBy("postTime", Query.Direction.DESCENDING)
.get()
.await()
.toObjects(Post::class.java)
}




// للمنشورات الرائجة
suspend fun getTrendingPosts(): List<Post> {
return postsCollection
.orderBy("likes", Query.Direction.DESCENDING)
.limit(50)
.get()
.await()
.toObjects(Post::class.java)
}
fun getPostsRealTime(callback: (List<Post>) -> Unit) {
postsCollection
.orderBy("postTime", Query.Direction.DESCENDING)
.addSnapshotListener { snapshot, error ->
error?.let {
callback(emptyList())
return@addSnapshotListener
}

snapshot?.let {
val posts = it.toObjects(Post::class.java).mapIndexed { index, post ->
post.copy(id = it.documents[index].id)
}
callback(posts)
}
}
}


// PostRepository.kt
suspend fun isUserAdmin(userId: String): Boolean {
return try {
val adminDoc = db.collection("admins").document(userId).get().await()
adminDoc.exists()
} catch (e: Exception) {
false
}
}

suspend fun addPost(post: Post): String {
val docRef = postsCollection.add(post).await()
val postId = docRef.id

// تحديث الحقل "id" داخل المستند
postsCollection.document(postId).update("id", postId).await()

return postId
}

// أضف هذه الدوال إلى PostRepository.kt

suspend fun addComment(postId: String, comment: Comment): String {
val docRef = db.collection("posts")
.document(postId)
.collection("comments")
.add(comment)
.await()

// تحديث عدد التعليقات
db.collection("posts").document(postId)
.update("commentsCount", FieldValue.increment(1))
.await()

return docRef.id
}

fun getCommentsRealTime(postId: String, callback: (List<Comment>) -> Unit): ListenerRegistration {
return db.collection("posts")
.document(postId)
.collection("comments")
.orderBy("timestamp", Query.Direction.ASCENDING)
.addSnapshotListener { snapshot, error ->
error?.let {
callback(emptyList())
return@addSnapshotListener
}

snapshot?.let {
val comments = it.toObjects(Comment::class.java)
callback(comments)
}
}
}
suspend fun likePost(postId: String, userId: String) {
db.runTransaction { transaction ->
val postRef = postsCollection.document(postId)
val post = transaction.get(
postRef).toObject(Post::class.java)

post?.let {
val likes = it.likes.toMutableMap()
if (likes.containsKey(userId)) {
likes.remove(userId)
} else {
likes[userId] = true
}
transaction.update(postRef, "likes", likes)
}
}.await()
}

suspend fun incrementCommentsCount(postId: String) {
postsCollection.document(postId)
.update("commentsCount", FieldValue.increment(1))
.await()
}

suspend fun incrementSharesCount(postId: String) {
postsCollection.document(postId)
.update("sharesCount", FieldValue.increment(1))
.await()
}

suspend fun uploadImage(uri: Uri): String {
return try {
val storageRef = Firebase.storage.reference
val imageRef = storageRef.child("posts/${UUID.randomUUID()}.jpg") // أضف امتداد الملف

// أضف سجل لبدء الرفع
Log.d("UploadImage", "Starting upload for URI: $uri")

// رفع الصورة مع إضافة مراقبة للتقدم
val uploadTask = imageRef.putFile(uri)
.addOnProgressListener { taskSnapshot ->
val progress = (100.0 * taskSnapshot.bytesTransferred / taskSnapshot.totalByteCount)
Log.d("UploadImage", "Upload is $progress% done")
}

val taskResult = uploadTask.await()
val downloadUrl = taskResult.storage.downloadUrl.await()

// أضف سجل لانتهاء الرفع
Log.d("UploadImage", "Upload completed. URL: $downloadUrl")

downloadUrl.toString()
} catch (e: Exception) {
Log.e("UploadImage", "Upload failed", e)
throw e
}
}
suspend fun deletePost(postId: String) {
postsCollection.document(postId).delete().await()
}
}




// DiscoverActivity.kt
package com.search2learn.s2l
import android.util.Log
import androidx.activity.result.ActivityResultLauncher
import android.app.Activity
import android.content.Intent
import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton

import android.net.Uri
import android.os.Bundle
import android.widget.ImageButton
import android.widget.EditText
import android.widget.Button
import android.widget.ImageView
import android.view.View
import android.content.Context
import com.bumptech.glide.Glide

import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.tabs.TabLayout
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase

import kotlinx.coroutines.launch
import com.google.firebase.firestore.FirebaseFirestore

import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.messaging.ktx.messaging

import com.google.firebase.storage.ktx.storage

class DiscoverActivity : AppCompatActivity(), PostAdapter.OnPostClickListener {



private lateinit var recyclerViewPosts: RecyclerView
private lateinit var postAdapter: PostAdapter
private lateinit var swipeRefreshLayout: SwipeRefreshLayout
private lateinit var postRepository: PostRepository
private lateinit var auth: FirebaseAuth
private var currentUserId: String = ""
private var selectedImageUri: Uri? = null
private var currentProfileImageView: ImageView? = null
// في MainActivity أو Application class:

private val pickImageLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.data?.let { uri ->
selectedImageUri = uri
}
}
}

// في DiscoverActivity.kt، تأكد أن onCommentClick يعمل هكذا:

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


val btnProfileSetup = findViewById<ShapeableImageView>(R.id.btnProfileSetup)
btnProfileSetup.setOnClickListener {
showProfileSetupDialog()
}



val btnNotifications: ShapeableImageView = findViewById(R.id.btnNotifications)
btnNotifications.setOnClickListener {
startActivity(Intent(this, NotificationsActivity::class.java))
}

// استعادة أي صلاحيات URI محفوظة
val sharedPref = getSharedPreferences("user_profile", Context.MODE_PRIVATE)
sharedPref.getString("user_image_uri", null)?.let { uriString ->
try {
val uri = Uri.parse(uriString)
contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
} catch (e: Exception) {
Log.e("DiscoverActivity", "Error restoring URI permissions", e)
}
}

// 1. تهيئة Firebase
auth = Firebase.auth
currentUserId = auth.currentUser?.uid ?: ""
postRepository = PostRepository() // التهيئة هنا مهمة

// 2. تهيئة الواجهة
initViews()
setupAdapter()
setupSwipeRefresh()
setupTabs()
setupFabButton()

// 3. تحميل البيانات الأولية
loadPosts()

// حفظ FCM Token
saveFCMToken()


// 4. تفعيل التحديثات الفورية
setupRealTimeUpdates() // <-- هنا
}


private fun initViews() {
recyclerViewPosts = findViewById(R.id.recyclerViewPosts)
swipeRefreshLayout = findViewById(R.id.swipeRefresh)
recyclerViewPosts.layoutManager = LinearLayoutManager(this)
}


private val profileImagePickerLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.data?.let { uri ->
selectedImageUri = uri
currentProfileImageView?.let { imageView ->
Glide.with(this@DiscoverActivity).load(uri).into(imageView)
}
}
}
}

private fun saveFCMToken() {
Firebase.messaging.token.addOnCompleteListener { task ->
if (task.isSuccessful) {
val token = task.result
val currentUser = FirebaseAuth.getInstance().currentUser ?: return@addOnCompleteListener

Firebase.firestore.collection("users")
.document(currentUser.uid)
.update("fcmToken", token)
.addOnFailureListener { e ->
Log.e("FCM", "Error saving token", e)
}
}
}
}





private fun setupAdapter() {
postAdapter = PostAdapter(this, currentUserId, this)
recyclerViewPosts.adapter = postAdapter
}

private fun loadPosts() {
swipeRefreshLayout.isRefreshing = true
lifecycleScope.launch {
try {
val posts = postRepository.getPosts()
postAdapter.setPosts(posts)
} catch (e: Exception) {
Toast.makeText(this@DiscoverActivity,
"Error loading posts: ${e.message}", Toast.LENGTH_SHORT).show()
} finally {
swipeRefreshLayout.isRefreshing = false
}
}
}

private fun setupRealTimeUpdates() {
// تأكد من أن postRepository مهيأ
if (!::postRepository.isInitialized) {
postRepository = PostRepository()
}

postRepository.getPostsRealTime { posts ->
runOnUiThread {
// تحديث واجهة المستخدم على الخيط الرئيسي
postAdapter.setPosts(posts)

// إيقاف أي تحميل إذا كان نشطاً
if (swipeRefreshLayout.isRefreshing) {
swipeRefreshLayout.isRefreshing = false
}

// تسجيل للفحص (اختياري)
Log.d("DiscoverActivity", "Received ${posts.size} posts in realtime")
}
}
}


private fun setupSwipeRefresh() {
swipeRefreshLayout.setOnRefreshListener {
loadPosts()
}
}

private fun setupTabs() {
val tabLayout: TabLayout = findViewById(R.id.tabLayout)
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
when (tab?.position) {
0 -> filterPosts("all")
1 -> filterPosts("following")
2 -> filterPosts("trending")
}
}

override fun onTabUnselected(tab: TabLayout.Tab?) {}
override fun onTabReselected(tab: TabLayout.Tab?) {}
})
}

private fun filterPosts(filterType: String) {
lifecycleScope.launch {
try {
val posts = when (filterType) {
"following" -> postRepository.getFollowingPosts(currentUserId)
"trending" -> postRepository.getTrendingPosts()
else -> postRepository.getPosts() // "all" or "for you"
}
postAdapter.setPosts(posts)
} catch (e: Exception) {
Toast.makeText(this@DiscoverActivity,
"Error filtering posts", Toast.LENGTH_SHORT).show()
}
}
}

private fun setupFabButton() {
val fab: ExtendedFloatingActionButton = findViewById(R.id.fabCreatePost)
fab.setOnClickListener {
showCreatePostDialog()
}
}

private fun showCreatePostDialog() {
val dialog = BottomSheetDialog(this)
val view = layoutInflater.inflate(R.layout.dialog_create_post, null)
dialog.setContentView(view)


val etContent = view.findViewById<EditText>(R.id.etPostContent)
val btnCoin = view.findViewById<Button>(R.id.btnAddCoin)
val btnPost = view.findViewById<Button>(R.id.btnPost)
val imagePreview = view.findViewById<ImageView>(R.id.imagePreview)
val etImageUrl = view.findViewById<EditText>(R.id.etImageUrl)

var coinSymbol: String? = null
var coinPrice: Double? = null



btnCoin.setOnClickListener {
// يمكنك استبدال هذا باختيار عملة حقيقي
coinSymbol = "BTC"
coinPrice = 50000.0
btnCoin.text = "BTC"
}

btnPost.setOnClickListener {
val content = etContent.text.toString().trim()
val imageUrl = etImageUrl.text.toString().trim() // احصل على رابط الصورة من الحقل



if (imageUrl.isNotEmpty()) {
// لا نتحقق من الامتداد، نقبل الرابط كما هو
// يمكن لاحقاً عرض الصورة وتجربة تحميلها وإذا فشل العرض نبلغ المستخدم
}



// التحقق من أن المحتوى ليس فارغًا
if (content.isEmpty()) {
Toast.makeText(this, "Please enter the post content", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}

// التحقق من طول المحتوى
if (content.length < 10) {
Toast.makeText(this, "The post is too short", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}

val wordCount = content.trim().split("\\s+".toRegex()).size
if (wordCount > 77) {
Toast.makeText(this, "The post must not exceed 77 words", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}


// التحقق من الحروف العربية أو الإنجليزية فقط


// التحقق من الرموز الغريبة أو الإيموجي



// التحقق من الروابط داخل النص
// كلمات خطيرة داخل النص أو الرابط
val dangerousKeywords = listOf("script", "base64", "data:", "<", ">", "onerror", "alert")

// قائمة الدومينات الممنوعة (يمكنك توسيعها لاحقًا)
val bannedDomains = listOf("porn", "xvideos", "xnxx", "phishing", "sex", "adult")

val lowerContent = content.lowercase()

val hasDangerousCode = dangerousKeywords.any { lowerContent.contains(it) }
val hasBannedDomain = bannedDomains.any { lowerContent.contains(it) }

if (hasDangerousCode || hasBannedDomain) {
Toast.makeText(this, "Your post contains unsafe or inappropriate content", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}

//التحقق من التكرار
if (content.all { it in "!?.-_" }) {
Toast.makeText(this, "The post must contain meaningful text", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}







// التحقق من الكلمات الممنوعة
val badWordsDetected = BadWordsFilter.detectBadWords(content)
if (badWordsDetected.isNotEmpty()) {
val message = "The post contains prohibited words: ${badWordsDetected.joinToString()}"
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
return@setOnClickListener
}



getUserProfile { name, imageUri ->
lifecycleScope.launch {
try {
var finalImageUrl: String? = null

// 1. معالجة رابط الصورة المدخل يدويًا
if (imageUrl.isNotEmpty()) {
if (isValidImageUrl(imageUrl)) {
finalImageUrl = imageUrl
Log.d("PostImage", "Image URL set: $finalImageUrl") // للتتبع
} else {
Toast.makeText(this@DiscoverActivity, "Invalid image URL format", Toast.LENGTH_SHORT).show()
return@launch
}
}

// 2. إنشاء كائن المنشور مع التأكد من تعيين postImage
val post = Post(
userId = auth.currentUser?.uid ?: "",
userName = name ?: "Anonymous",
userProfileImage = imageUri?.toString() ?: "",
postContent = content,
coinSymbol = coinSymbol,
coinPrice = coinPrice,
postImage = finalImageUrl // هذا هو الحقل المهم
)

// 3. طباعة للتحقق (يمكن حذفها لاحقًا)
Log.d("PostDebug", "Post to be saved: $post")

// 4. حفظ المنشور
val postId = postRepository.addPost(post)
Log.d("PostImage", "Post saved with ID: $postId, Image URL: ${post.postImage}")

// 5. إغلاق النافذة وإعادة التحميل
loadPosts()
dialog.dismiss()
selectedImageUri = null
Toast.makeText(this@DiscoverActivity, "Post created successfully", Toast.LENGTH_SHORT).show()

} catch (e: Exception) {
Log.e("PostError", "Failed to create post", e)
Toast.makeText(
this@DiscoverActivity,
"Error: ${e.localizedMessage}",
Toast.LENGTH_SHORT
).show()
}
}
}
}




// عرض معاينة الصورة إذا تم اختيارها
selectedImageUri?.let {
imagePreview.visibility = View.VISIBLE
Glide.with(this).load(it).into(imagePreview)
} ?: run {
imagePreview.visibility = View.GONE
}

dialog.show()
}

override fun onLikeClick(postId: String) {
lifecycleScope.launch {
try {
postRepository.likePost(postId, currentUserId)
} catch (e: Exception) {
Toast.makeText(this@DiscoverActivity,
"Error liking post: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}

override fun onCommentClick(postId: String) {
lifecycleScope.launch {
try {
postRepository.incrementCommentsCount(postId)
// افتح شاشة التعليقات
val intent = Intent(this@DiscoverActivity, CommentsActivity::class.java)
intent.putExtra("postId", postId)
startActivity(intent)
} catch (e: Exception) {
Toast.makeText(this@DiscoverActivity,
"Error adding comment: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}

override fun onShareClick(postId: String) {
lifecycleScope.launch {
try {
postRepository.incrementSharesCount(postId)
// مشاركة المنشور
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "https://t.me/S2LCoin/16773!")
type = "text/plain"
}
startActivity(Intent.createChooser(shareIntent, "Share post via"))
} catch (e: Exception) {
Toast.makeText(this@DiscoverActivity,
"Error sharing post: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}

override fun onOptionsClick(post: Post, position: Int) {
showPostOptionsDialog(post, position)
}

private fun showPostOptionsDialog(post: Post, position: Int) {
val options = arrayOf("Delete Post", "Report", "Cancel")

val builder = androidx.appcompat.app.AlertDialog.Builder(this)
builder.setTitle("Post Options")
builder.setItems(options) { _, which ->
when (which) {
0 -> deletePost(post, position)
1 -> reportPost(post)
// 2 is Cancel
}
}
builder.show()
}
// داخل DiscoverActivity
private fun saveUserProfile(name: String, imageUri: Uri?) {
val user = auth.currentUser ?: return

if (imageUri != null) {
// تحقق إذا كان الرابط بعيدًا (يبدأ بـ http أو https)
if (imageUri.scheme?.startsWith("http") == true) {
// إذا كان رابطًا بعيدًا، احفظه مباشرة دون محاولة قراءته كمحلي
saveToSharedPrefsAndFirestore(name, imageUri.toString())
} else {
// إذا كان URI محليًا، استخدم الكود الحالي لرفعه
try {
contentResolver.takePersistableUriPermission(
imageUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)

val storageRef = Firebase.storage.reference
.child("profile_images/${user.uid}")

val inputStream = contentResolver.openInputStream(imageUri)
inputStream?.let { stream ->
val uploadTask = storageRef.putStream(stream)

uploadTask.continueWithTask { task ->
if (!task.isSuccessful) {
task.exception?.let { throw it }
}
storageRef.downloadUrl
}.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
saveToSharedPrefsAndFirestore(name, downloadUri.toString())
} else {
Log.e("Profile", "Error uploading image", task.exception)
saveToSharedPrefsAndFirestore(name, null)
Toast.makeText(this, "Failed to upload image", Toast.LENGTH_SHORT).show()
}
}
} ?: run {
saveToSharedPrefsAndFirestore(name, null)
Toast.makeText(this, "Could not read image file", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
Log.e("Profile", "Error handling image", e)
saveToSharedPrefsAndFirestore(name, null)
Toast.makeText(this, "Error processing image: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
} else {
saveToSharedPrefsAndFirestore(name, null)
}
}

private fun saveToSharedPrefsAndFirestore(name: String, imageUrl: String?) {
val user = auth.currentUser ?: return
val sharedPref = getSharedPreferences("user_profile", Context.MODE_PRIVATE)

with(sharedPref.edit()) {
putString("user_name", name)
imageUrl?.let { putString("user_image", it) }
apply()
}

// إنشاء بيانات المستخدم مع التأكد من أن imageUrl ليس null
val userData = hashMapOf<String, Any>(
"name" to name
).apply {
if (!imageUrl.isNullOrEmpty()) {
put("imageUrl", imageUrl!!)
} else {
put("imageUrl", "")
}
}

Firebase.firestore.collection("user_profiles")
.document(user.uid)
.set(userData)
.addOnSuccessListener {
Log.d("Profile", "Profile saved")
Toast.makeText(this, "Profile saved successfully", Toast.LENGTH_SHORT).show()
}
.addOnFailureListener { e ->
Log.e("Profile", "Error saving profile", e)
Toast.makeText(this, "Error saving profile", Toast.LENGTH_SHORT).show()
}
}

private fun getUserProfile(callback: (String?, Uri?) -> Unit) {
val user = auth.currentUser ?: run {
callback(null, null)
return
}

// حاول جلب من الذاكرة المحلية أولاً
val sharedPref = getSharedPreferences("user_profile", Context.MODE_PRIVATE)
val savedName = sharedPref.getString("user_name", null)
val savedImage = sharedPref.getString("user_image", null)?.let { Uri.parse(it) }

if (savedName != null && savedImage != null) {
callback(savedName, savedImage)
return
}

// إذا لم توجد في الذاكرة المحلية، جلب من Firestore
FirebaseFirestore.getInstance().collection("user_profiles")
.document(user.uid)
.get()
.addOnSuccessListener { document ->
val name = document.getString("name")
val imageUrl = document.getString("imageUrl")?.let { Uri.parse(it) }
callback(name, imageUrl)
}
.addOnFailureListener {
callback(null, null)
}
}

// داخل DiscoverActivity
private fun showProfileSetupDialog() {
val dialog = BottomSheetDialog(this)
val view = layoutInflater.inflate(R.layout.dialog_setup_profile, null)
dialog.setContentView(view)

val etName = view.findViewById<EditText>(R.id.etName)
val etImageUrl = view.findViewById<EditText>(R.id.etImageUrl)
val imagePreview = view.findViewById<ImageView>(R.id.imagePreview)
val btnSave = view.findViewById<Button>(R.id.btnSave)

var selectedImageUri: Uri? = null

// تحميل البيانات الحالية
getUserProfile { name, uri ->
name?.let { etName.setText(it) }
uri?.let {
selectedImageUri = it
etImageUrl.setText(it.toString())
Glide.with(this).load(it).into(imagePreview)
}
}

etImageUrl.setOnFocusChangeListener { _, hasFocus ->
if (!hasFocus) {
val url = etImageUrl.text.toString().trim()
if (url.isNotEmpty()) {
try {
// إنشاء URI من الرابط
val uri = Uri.parse(url)
selectedImageUri = uri
Glide.with(this).load(url).into(imagePreview)
} catch (e: Exception) {
Toast.makeText(this, "Invalid URL", Toast.LENGTH_SHORT).show()
}
}
}
}

btnSave.setOnClickListener {
val name = etName.text.toString().trim()
if (name.isEmpty()) {
Toast.makeText(this, "Please enter your name", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}

val imageUrl = etImageUrl.text.toString().trim()
if (imageUrl.isNotEmpty()) {
try {
selectedImageUri = Uri.parse(imageUrl)
saveUserProfile(name, selectedImageUri)
} catch (e: Exception) {
Toast.makeText(this, "Invalid image URL", Toast.LENGTH_SHORT).show()
}
} else {
saveUserProfile(name, null)
}

dialog.dismiss()
}

dialog.show()
}


// يمكنك إضافة هذا في onCreate() في Application class أو في MainActivity

// DiscoverActivity.kt
private fun deletePost(post: Post, position: Int) {
Log.d("DELETE_POST", "Attempting to delete post: ${post.id}")

lifecycleScope.launch {
try {
val isAdmin = postRepository.isUserAdmin(currentUserId)
Log.d("DELETE_POST", "User is admin: $isAdmin, Current user: $currentUserId, Post owner: ${post.userId}")

if (post.userId == currentUserId || isAdmin) {
Log.d("DELETE_POST", "User has permission to delete")

postRepository.deletePost(post.id)
Log.d("DELETE_POST", "Post deleted from repository")

val adapterPosition = postAdapter.findPostPosition(post.id)
Log.d("DELETE_POST", "Adapter position: $adapterPosition")

if (adapterPosition != -1) {
postAdapter.removePost(adapterPosition)
Log.d("DELETE_POST", "Post removed from adapter")
Toast.makeText(this@DiscoverActivity, "Post deleted", Toast.LENGTH_SHORT).show()
}
} else {
Log.d("DELETE_POST", "User doesn't have permission")
Toast.makeText(this@DiscoverActivity,
"You don't have permission to delete this post",
Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
Log.e("DELETE_POST", "Error deleting post", e)
Toast.makeText(this@DiscoverActivity,
"Error deleting post: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}

private fun isValidImageUrl(url: String): Boolean {
return url.isNotBlank()
}


private fun reportPost(post: Post) {
Toast.makeText(this, "Post reported", Toast.LENGTH_SHORT).show()
}
}

إرسال تعليق

0 تعليقات