From f2e816ab9e625b0575f6e81fffb0ee7fceddc84e Mon Sep 17 00:00:00 2001 From: TAMARA JERINIC Date: Mon, 28 Nov 2022 22:26:51 +0100 Subject: Omogućeno praćenje i prestanak praćenja korisnika na front-u i back-u. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Activities/ActivityUserProfile.kt | 138 ++++++++++++++++----- .../brzodolokacije/Interfaces/IBackendApi.kt | 3 + .../src/main/res/layout/activity_user_profile.xml | 33 ++++- 3 files changed, 136 insertions(+), 38 deletions(-) (limited to 'Client') diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityUserProfile.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityUserProfile.kt index c03dbd3..4f48dc7 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityUserProfile.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityUserProfile.kt @@ -1,19 +1,15 @@ package com.example.brzodolokacije.Activities import android.annotation.SuppressLint -import android.graphics.Color -import android.graphics.PorterDuff -import android.graphics.drawable.Drawable import android.os.Bundle import android.util.Log import android.widget.* import androidx.appcompat.app.AppCompatActivity -import androidx.core.content.ContextCompat +import androidx.core.view.isVisible import androidx.fragment.app.Fragment import com.bumptech.glide.Glide import com.example.brzodolokacije.Fragments.FragmentShowUserPosts import com.example.brzodolokacije.Models.UserReceive -import com.example.brzodolokacije.R import com.example.brzodolokacije.R.* import com.example.brzodolokacije.Services.RetrofitHelper import com.example.brzodolokacije.Services.SharedPreferencesHelper @@ -21,7 +17,6 @@ import com.google.gson.Gson import retrofit2.Call import retrofit2.Callback import retrofit2.Response -import java.security.AccessController.getContext class ActivityUserProfile : AppCompatActivity() { @@ -35,9 +30,10 @@ class ActivityUserProfile : AppCompatActivity() { private lateinit var fragmentContainer: FrameLayout private lateinit var userObject:UserReceive private lateinit var openChat:ImageButton - + private lateinit var unfollowUser:Button private lateinit var followersList: MutableList private lateinit var followingList: MutableList + private var follow:Boolean=false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -49,6 +45,7 @@ class ActivityUserProfile : AppCompatActivity() { followingNumber=findViewById(id.tvActivityUserProfileFollowNo) profilePicture=findViewById(id.tvActivityProfileProfilePicture) followUser=findViewById(id.btnActivityUserProfileFollow) + unfollowUser=findViewById(id.btnActivityUserProfileUnFollow) showUserPosts=findViewById(id.btnActivityUserProfileShowPosts) fragmentContainer=findViewById(id.flActivityProfileFragmentContainer) openChat=findViewById(id.activityUserProfileOpenChat) @@ -74,31 +71,68 @@ class ActivityUserProfile : AppCompatActivity() { } } + checkIfAlreadyFollow() + updateUserData() followUser.setOnClickListener{ + val api = RetrofitHelper.getInstance() + val token = SharedPreferencesHelper.getValue("jwt", this@ActivityUserProfile) + var data = api.addFollower("Bearer " + token, userObject._id); + data.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + unfollowUser.isVisible=true + unfollowUser.isClickable=true + unfollowUser.isEnabled=true + followUser.isVisible=false + followUser.isClickable=false + followUser.isEnabled=false + updateUserData() + + Toast.makeText( + this@ActivityUserProfile, "PRATITE KORISNIKA", Toast.LENGTH_LONG + ).show(); + } + + override fun onFailure(call: Call, t: Throwable) { + Toast.makeText( + this@ActivityUserProfile, t.toString(), Toast.LENGTH_LONG + ).show(); + } + }) - checkIfAlreadyFollow() - + } + unfollowUser.setOnClickListener { + val api = RetrofitHelper.getInstance() + val token = SharedPreferencesHelper.getValue("jwt", this@ActivityUserProfile) + var data = api.unfollow("Bearer " + token, userObject._id); + data.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + unfollowUser.isVisible=false + unfollowUser.isClickable=false + unfollowUser.isEnabled=false + followUser.isVisible=true + followUser.isClickable=true + followUser.isEnabled=true + updateUserData() + Toast.makeText( + this@ActivityUserProfile, "VIŠE NE PRATITE KORISNIKA", Toast.LENGTH_LONG + ).show(); + } + + override fun onFailure(call: Call, t: Throwable) { + Toast.makeText( + this@ActivityUserProfile, t.toString(), Toast.LENGTH_LONG + ).show(); + } + }) - val api = RetrofitHelper.getInstance() - val token= SharedPreferencesHelper.getValue("jwt", this@ActivityUserProfile) - var data=api.addFollower("Bearer "+token,userObject._id); - data.enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - Toast.makeText( - this@ActivityUserProfile, "PRATITE KORISNIKA", Toast.LENGTH_LONG - ).show(); - } - override fun onFailure(call: Call, t: Throwable) { - Toast.makeText( - this@ActivityUserProfile, t.toString(), Toast.LENGTH_LONG - ).show(); - } - }) } @@ -132,15 +166,34 @@ class ActivityUserProfile : AppCompatActivity() { } var follow = response.body()!! if(follow){ + Log.d("success","follow") - followUser.setCompoundDrawablesWithIntrinsicBounds(drawable.ic_outline_person_remove_24,0,0,0) + /*followUser.setCompoundDrawablesWithIntrinsicBounds(drawable.ic_outline_person_remove_24,0,0,0) followUser.text="Ne prati više" - + follow=false + */ + unfollowUser.isVisible=true + unfollowUser.isClickable=true + unfollowUser.isEnabled=true + followUser.isVisible=false + followUser.isClickable=false + followUser.isEnabled=false } - if(!follow){ + else{ Log.d("success","not follow") - followUser.setCompoundDrawablesWithIntrinsicBounds(drawable.ic_outline_person_add_alt_24,0,0,0) + /*followUser.setCompoundDrawablesWithIntrinsicBounds(drawable.ic_outline_person_add_alt_24,0,0,0) followUser.text="Prati" + follow=true + */ + + unfollowUser.isVisible=false + unfollowUser.isClickable=false + unfollowUser.isEnabled=false + followUser.isVisible=true + followUser.isClickable=true + followUser.isEnabled=true + + } } @@ -148,7 +201,28 @@ class ActivityUserProfile : AppCompatActivity() { } - fun checkIfAlreadyFollowChangeButton(){ + fun updateUserData(){ + val api = RetrofitHelper.getInstance() + val token = SharedPreferencesHelper.getValue("jwt", this@ActivityUserProfile) + var data = api.getProfileFromId("Bearer " + token, userObject._id); + data.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + var userData=response.body()!! + name.text=userData.name + postsNumber.text=userData.postNumber.toString() + followersNumber.text=userData.followersCount.toString() + followingNumber.text=userData.followingCount.toString() + } + override fun onFailure(call: Call, t: Throwable) { + Toast.makeText( + this@ActivityUserProfile, t.toString(), Toast.LENGTH_LONG + ).show(); + } + }) } + } \ No newline at end of file diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt index ce8d7e3..bc8c7ae 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt @@ -101,4 +101,7 @@ interface IBackendApi { @GET("/api/user/{id}/checkIfAlreadyFollow") fun checkIfAlreadyFollow(@Header("Authorization") authHeader:String,@Path("id") id:String):Call + @GET("/api/user/{id}/unfollow") + fun unfollow(@Header("Authorization") authHeader:String,@Path("id") id:String):Call + } \ No newline at end of file diff --git a/Client/BrzoDoLokacije/app/src/main/res/layout/activity_user_profile.xml b/Client/BrzoDoLokacije/app/src/main/res/layout/activity_user_profile.xml index a446f9a..0e90fc7 100644 --- a/Client/BrzoDoLokacije/app/src/main/res/layout/activity_user_profile.xml +++ b/Client/BrzoDoLokacije/app/src/main/res/layout/activity_user_profile.xml @@ -228,22 +228,44 @@