diff options
Diffstat (limited to 'Client')
4 files changed, 111 insertions, 65 deletions
diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ChatActivity.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ChatActivity.kt index 9c489b3..6d8afd6 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ChatActivity.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ChatActivity.kt @@ -24,6 +24,7 @@ import com.example.brzodolokacije.Models.MessageReceive import com.example.brzodolokacije.R import com.example.brzodolokacije.Services.RetrofitHelper import com.example.brzodolokacije.Services.SharedPreferencesHelper +import com.example.brzodolokacije.chat.Notifications import com.example.brzodolokacije.chat.SignalRListener import com.example.brzodolokacije.databinding.ActivityChatBinding import kotlinx.coroutines.MainScope @@ -50,41 +51,9 @@ class ChatActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding= ActivityChatBinding.inflate(layoutInflater) - permissionLauncher=registerForActivityResult( - ActivityResultContracts.RequestPermission() - ) { isGranted: Boolean -> - if (!isGranted) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (Build.VERSION.SDK_INT >= 33) { - //ako je upravo odbijena dozvola na uredjaju na kome je ona neophodna - binding.btnNotifications.setImageResource(R.drawable.bell_off) - } - else{ - //ako je upravo odbijena dozvola na uredjaju na kome nije ona neophodna - binding.btnNotifications.setImageResource(R.drawable.bell_on) - } - } - else{ - //ako je upravo odbijena dozvola na uredjaju na kome nije ona neophodna - binding.btnNotifications.setImageResource(R.drawable.bell_on) - } - } - else{ - //ako je upravo prihvacena dozvola na uredjaju na kome nije ona neophodna - binding.btnNotifications.setImageResource(R.drawable.bell_on) - } - } - //provera da li je dozvoljeno - when { - ContextCompat.checkSelfPermission(this,Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED -> { - binding.btnNotifications.setImageResource(R.drawable.bell_on) - } - else -> { - binding.btnNotifications.setImageResource(R.drawable.bell_off) - } - } - - + Notifications.makeChannel(this) + setPermissionLauncher() + checkPermissions() setContentView(binding.root) dbConnection= DBHelper(this@ChatActivity,null) ws=SignalRListener.getInstance(this@ChatActivity) @@ -106,6 +75,46 @@ class ChatActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener { requestNewMessages() } + fun setPermissionLauncher(){ + permissionLauncher=registerForActivityResult( + ActivityResultContracts.RequestPermission() + ) { isGranted: Boolean -> + if (!isGranted) { + binding.btnNotifications.setImageResource(R.drawable.bell_off) + } + else{ + binding.btnNotifications.setImageResource(R.drawable.bell_on) + } + } + } + + fun checkPermissions(){ + when { + //treba proveriti permisije + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU-> { + if(ContextCompat.checkSelfPermission(this,Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) { + binding.btnNotifications.setImageResource(R.drawable.bell_on) + } + else{ + binding.btnNotifications.setImageResource(R.drawable.bell_off) + } + } + //treba proveriti preference + else-> { + if(SharedPreferencesHelper.getValue("notifications",this)==null){ + SharedPreferencesHelper.addValue("notifications","false",this@ChatActivity) + } + else if (SharedPreferencesHelper.getValue("notifications",this)=="true") { + binding.btnNotifications.setImageResource(R.drawable.bell_on) + } + else{ + binding.btnNotifications.setImageResource(R.drawable.bell_off) + } + } + + } + } + fun launchNotificationPermissionPrompt(){ permissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS) } @@ -131,15 +140,28 @@ class ChatActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener { } findViewById<ImageButton>(R.id.btnNotifications).setOnClickListener { when { - ContextCompat.checkSelfPermission(this,Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + //treba proveriti permisije + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU-> { + if(ContextCompat.checkSelfPermission(this,Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) { revokeSelfPermissionOnKill(Manifest.permission.POST_NOTIFICATIONS) + launchInfoDialog() + } + else{ + launchNotificationPermissionPrompt() } - launchInfoDialog() } - else -> { - launchNotificationPermissionPrompt() + //treba proveriti preference + else-> { + if (SharedPreferencesHelper.getValue("notifications",this)=="true") { + SharedPreferencesHelper.addValue("notifications","false",this@ChatActivity) + binding.btnNotifications.setImageResource(R.drawable.bell_off) + } + else{ + SharedPreferencesHelper.addValue("notifications","true",this@ChatActivity) + binding.btnNotifications.setImageResource(R.drawable.bell_on) + } } + } } } diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ChatActivityConversation.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ChatActivityConversation.kt index 04dd9de..b81867e 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ChatActivityConversation.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ChatActivityConversation.kt @@ -11,6 +11,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.auth0.android.jwt.JWT +import com.bumptech.glide.Glide import com.exam.DBHelper import com.example.brzodolokacije.Adapters.ChatMessagesAdapter import com.example.brzodolokacije.Models.Message @@ -39,6 +40,7 @@ class ChatActivityConversation : AppCompatActivity() { var webSocketConnection:SignalRListener?=null var items:MutableList<Message>?=mutableListOf() var userImage:Bitmap?=null + var userImageId:String?=null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -46,7 +48,12 @@ class ChatActivityConversation : AppCompatActivity() { setContentView(binding.root) userId=intent.extras?.get("userId").toString() receiverUsername=intent.extras?.get("username").toString() - userImage=intent.extras?.get("pfp") as Bitmap? + if(intent.extras?.get("pfp") is Bitmap){ + userImage=intent.extras?.get("pfp") as Bitmap? + } + else{ + userImageId=intent.extras?.get("pfp") as String? + } dbConnection=DBHelper.getInstance(this@ChatActivityConversation) setHeader() setRecyclerView() @@ -172,8 +179,15 @@ class ChatActivityConversation : AppCompatActivity() { binding.btnBack.setOnClickListener { finish() } - if(userImage!=null) + if(userImage!=null){ binding.ivUserImage.setImageBitmap(userImage) + } + else if(userImageId!=null){ + Glide.with(this) + .load(RetrofitHelper.baseUrl + "/api/post/image/compress/" + userImageId!!) + .circleCrop() + .into(binding.ivUserImage) + } } fun setRecyclerView(setParams:Boolean=true){ MainScope().launch { diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/MainActivity.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/MainActivity.kt index acce7b3..b3183b8 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/MainActivity.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/MainActivity.kt @@ -9,7 +9,6 @@ import com.example.brzodolokacije.Activities.ActivityLoginRegister import com.example.brzodolokacije.Activities.NavigationActivity import com.example.brzodolokacije.Services.RetrofitHelper import com.example.brzodolokacije.Services.SharedPreferencesHelper -import com.example.brzodolokacije.chat.Notifications import retrofit2.Call import retrofit2.Response @@ -23,7 +22,6 @@ class MainActivity : AppCompatActivity() { val intent:Intent if(checkLoggedIn()) { - Notifications.makeChannel(this) intent = Intent(this, NavigationActivity::class.java) } else diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/chat/SignalRListener.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/chat/SignalRListener.kt index 35088a5..49d76c9 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/chat/SignalRListener.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/chat/SignalRListener.kt @@ -2,16 +2,20 @@ package com.example.brzodolokacije.chat import android.Manifest import android.app.Activity +import android.app.PendingIntent +import android.content.Intent import android.content.pm.PackageManager import android.graphics.Color import android.util.Log import android.widget.Toast import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat +import androidx.core.app.TaskStackBuilder import androidx.core.content.ContextCompat import com.auth0.android.jwt.JWT import com.exam.DBHelper import com.example.brzodolokacije.Activities.ChatActivity +import com.example.brzodolokacije.Activities.ChatActivityConversation import com.example.brzodolokacije.Models.Message import com.example.brzodolokacije.Models.MessageReceive import com.example.brzodolokacije.Models.UserReceive @@ -84,33 +88,40 @@ class SignalRListener private constructor(val activity: Activity){ } activity.onRefresh() } - when { - ContextCompat.checkSelfPermission(activity, - Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED -> { - //poslati notifikaciju - var api=RetrofitHelper.getInstance() - var token=SharedPreferencesHelper.getValue("jwt",activity) - val request2=api?.getProfileFromId("Bearer "+token, - message.senderId - ) - request2?.enqueue(object : retrofit2.Callback<UserReceive?> { - override fun onResponse(call: Call<UserReceive?>, response: Response<UserReceive?>) { - if(response.isSuccessful()){ - var user=response.body()!! - createNotification(message,user,activity) - } - } - - override fun onFailure(call: Call<UserReceive?>, t: Throwable) { - //TODO("Not yet implemented") + if(ContextCompat.checkSelfPermission(activity, + Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED || SharedPreferencesHelper.getValue("notifications",activity)=="true"){ + //poslati notifikaciju + var api=RetrofitHelper.getInstance() + var token=SharedPreferencesHelper.getValue("jwt",activity) + val request2=api?.getProfileFromId("Bearer "+token, + message.senderId + ) + request2?.enqueue(object : retrofit2.Callback<UserReceive?> { + override fun onResponse(call: Call<UserReceive?>, response: Response<UserReceive?>) { + if(response.isSuccessful()){ + var user=response.body()!! + createNotification(message,user,activity) } - }) } - } + override fun onFailure(call: Call<UserReceive?>, t: Throwable) { + //TODO("Not yet implemented") + } + }) + } } fun createNotification(message: MessageReceive,user: UserReceive,activity: Activity){ + val resultIntent = Intent(activity, ChatActivityConversation::class.java) + resultIntent.putExtra("userId",user._id) + resultIntent.putExtra("username",user.username) + resultIntent.putExtra("pfp",user.pfp?._id) + val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(activity).run { + addNextIntentWithParentStack(resultIntent) + getPendingIntent(0, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) + } + val notificationBuilder = NotificationCompat.Builder(activity, Notifications.CHANNEL_ID) .setSmallIcon(R.drawable.ic_round_chat_24) .setAutoCancel(true) @@ -120,6 +131,7 @@ class SignalRListener private constructor(val activity: Activity){ .setContentTitle(user.username) .setContentText(message.messagge) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setContentIntent(resultPendingIntent) val notificationManager = NotificationManagerCompat.from(activity) val notification = notificationBuilder.build() notificationManager.notify(NotificationID.iD, notification) |