diff options
author | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-11-28 22:41:49 +0100 |
---|---|---|
committer | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-11-28 22:41:49 +0100 |
commit | 6c44a9844b79aeb69befb277e3bd3daec44bc059 (patch) | |
tree | 8ea32aaf46edcd1ddc274b78e07a1b1861a586c4 | |
parent | 68b86d96142eb1f9e42ba2d87a4277d014759c46 (diff) | |
parent | f2e816ab9e625b0575f6e81fffb0ee7fceddc84e (diff) |
Merge branch 'develop' of http://gitlab.pmf.kg.ac.rs/BrzoDoLokacije2022/odyssey/brzodolokacije into develop
6 files changed, 216 insertions, 45 deletions
diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs index ada0f35..cc45737 100644 --- a/Backend/Api/Api/Controllers/UserController.cs +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -110,5 +110,11 @@ namespace Api.Controllers return Ok(await _userService.CheckIfAlreadyFollow(id)); } + [HttpGet("{id}/unfollow")] + [Authorize(Roles = "User")] + public async Task<ActionResult<Boolean>> Unfollow(string id) + { + return Ok(await _userService.Unfollow(id)); + } } } diff --git a/Backend/Api/Api/Interfaces/IUserService.cs b/Backend/Api/Api/Interfaces/IUserService.cs index 21dcdc0..5f99733 100644 --- a/Backend/Api/Api/Interfaces/IUserService.cs +++ b/Backend/Api/Api/Interfaces/IUserService.cs @@ -33,5 +33,7 @@ namespace Api.Interfaces Task<List<UserSend>> GetMyFollowings(); Task<Boolean> CheckIfAlreadyFollow(string id); + Task<Boolean> Unfollow(string id); + } } diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index 7bfc24a..cc75533 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -6,6 +6,7 @@ using System.Security.Claims; using MimeKit; using MailKit.Net.Smtp; using DnsClient; +using MongoDB.Bson; namespace Api.Services { @@ -380,6 +381,18 @@ namespace Api.Services return tosend; } + public async Task<Boolean> updateUserFollowerFollowingCount(List<string> followers,List <string> followings,string userId) + { + User u = await _users.Find(user => user._id == userId).FirstOrDefaultAsync(); + if(u!= null) + { + u.followersCount = followers.Count(); + u.followingCount = followings.Count(); + return true; + } + return false; + } + public async Task<Boolean> AddFollower(string followerId) { string id = null; @@ -392,14 +405,29 @@ namespace Api.Services if (id != null && followerId!=null) { - if (u.followers == null) - u.followers = new List<string>(); - u.followers.Add(followerId); - if (f.following == null) - f.following = new List<string>(); - f.following.Add(id); + if (f.followers == null) + { + f.followers = new List<string>(); + f.followersCount = 0; + } + f.followers.Add(id); + f.followersCount =f.followers.Count(); + + + if (u.following == null) + { + u.following = new List<string>(); + u.followingCount = 0; + } + u.following.Add(followerId); + u.followingCount =u.following.Count(); + _users.ReplaceOne(user=>user._id==id, u); _users.ReplaceOne(user => user._id == followerId, f); + + // updateUserFollowerFollowingCount(u.followers, u.following, u._id); + //updateUserFollowerFollowingCount(f.followers, f.following, f._id); + return true; } @@ -543,5 +571,42 @@ namespace Api.Services return false; } - } + + public async Task<bool> Unfollow(string id) + { + string myId = null; + + if (_httpContext.HttpContext.User.FindFirstValue("id") != null) + { + myId = _httpContext.HttpContext.User.FindFirstValue("id").ToString(); + } + + User u = await _users.Find(user => user._id == myId).FirstOrDefaultAsync(); + User f = await _users.Find(user => user._id == id).FirstOrDefaultAsync(); + + if (u != null) + { + if (u.following != null && f.followers!=null) + { + u.following.Remove(f._id); + u.followingCount=u.following.Count(); + u.followersCount = u.followers.Count(); + + + f.followers.Remove(u._id); + f.followersCount =f.followers.Count(); + f.followingCount =f.following.Count(); + + _users.ReplaceOne(user => user._id == myId, u); + _users.ReplaceOne(user => user._id == id, f); + + //updateUserFollowerFollowingCount(u.followers, u.following, u._id); + //updateUserFollowerFollowingCount(f.followers, f.following, f._id); + return true; + } + + } + return false; + } + } } 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<UserReceive> private lateinit var followingList: MutableList<UserReceive> + 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<Boolean> { + override fun onResponse( + call: Call<Boolean>, + response: Response<Boolean> + ) { + 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<Boolean>, 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<Boolean> { + override fun onResponse( + call: Call<Boolean>, + response: Response<Boolean> + ) { + 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<Boolean>, 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<Boolean> { - override fun onResponse( - call: Call<Boolean>, - response: Response<Boolean> - ) { - Toast.makeText( - this@ActivityUserProfile, "PRATITE KORISNIKA", Toast.LENGTH_LONG - ).show(); - } - override fun onFailure(call: Call<Boolean>, 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<UserReceive> { + override fun onResponse( + call: Call<UserReceive>, + response: Response<UserReceive> + ) { + 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<UserReceive>, 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 dedd0bf..f2ca608 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<Boolean> + @GET("/api/user/{id}/unfollow") + fun unfollow(@Header("Authorization") authHeader:String,@Path("id") id:String):Call<Boolean> + }
\ 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 @@ <Button android:id="@+id/btnActivityUserProfileFollow" - android:layout_width="0dp" + android:layout_width="280dp" android:layout_height="30dp" android:layout_marginStart="16dp" - android:layout_marginEnd="8dp" + android:clickable="true" android:drawableLeft="@drawable/ic_outline_person_add_alt_24" - android:text="PRATI" android:insetTop="0dp" android:insetBottom="0dp" + android:backgroundTint="@color/button_main" + android:text="PRATI" + android:visibility="visible" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="0.13" app:shapeAppearanceOverlay="@style/Circular" + tools:ignore="TouchTargetSizeCheck" /> + + <Button + + android:id="@+id/btnActivityUserProfileUnFollow" + android:layout_width="280dp" + android:layout_height="30dp" + android:layout_marginStart="16dp" + android:clickable="false" + android:drawableLeft="@drawable/ic_outline_person_remove_24" + android:insetTop="0dp" + android:insetBottom="0dp" + android:text="NE PRATI više" + android:backgroundTint="@color/unfollow" + app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toStartOf="@+id/activityUserProfileOpenChat" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.13" + app:shapeAppearanceOverlay="@style/Circular" tools:ignore="TouchTargetSizeCheck" /> + <ImageButton android:id="@+id/activityUserProfileOpenChat" android:layout_width="35dp" @@ -252,11 +274,10 @@ android:layout_marginEnd="16dp" android:height="40dp" android:background="@drawable/rounded_button" - android:backgroundTint="#4DB6AC" + android:backgroundTint="@color/button_main" android:src="@drawable/ic_round_message_24" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintStart_toEndOf="@+id/btnActivityUserProfileFollow" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" app:shapeAppearanceOverlay="@style/Circular" |