diff --git a/app/build.gradle b/app/build.gradle index 8bf02911..d6196514 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -109,7 +109,7 @@ dependencies { implementation "androidx.work:work-runtime:$work_version" implementation "io.mikael:urlbuilder:2.0.9" implementation "org.codeberg.gitnex-garage:emoji-java:v5.1.2" - implementation "org.codeberg.gitnex:tea4j:1.0.16" + implementation "org.codeberg.gitnex:tea4j:1.0.18" coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:1.1.5" implementation 'androidx.biometric:biometric:1.1.0' implementation 'com.github.chrisvest:stormpot:2.4.2' diff --git a/app/src/main/java/org/mian/gitnex/activities/ProfileActivity.java b/app/src/main/java/org/mian/gitnex/activities/ProfileActivity.java index 93b65de1..f63a2051 100644 --- a/app/src/main/java/org/mian/gitnex/activities/ProfileActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/ProfileActivity.java @@ -3,6 +3,7 @@ package org.mian.gitnex.activities; import android.content.Intent; import android.graphics.Typeface; import android.os.Bundle; +import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -15,23 +16,31 @@ import androidx.viewpager2.adapter.FragmentStateAdapter; import androidx.viewpager2.widget.ViewPager2; import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayoutMediator; +import com.google.gson.JsonElement; import org.mian.gitnex.R; +import org.mian.gitnex.clients.RetrofitClient; +import org.mian.gitnex.fragments.BottomSheetUserProfileFragment; import org.mian.gitnex.fragments.profile.DetailFragment; import org.mian.gitnex.fragments.profile.FollowersFragment; import org.mian.gitnex.fragments.profile.FollowingFragment; import org.mian.gitnex.fragments.profile.OrganizationsFragment; import org.mian.gitnex.fragments.profile.RepositoriesFragment; import org.mian.gitnex.fragments.profile.StarredRepositoriesFragment; +import org.mian.gitnex.helpers.Authorization; import org.mian.gitnex.helpers.Toasty; import java.util.Objects; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; /** * Author M M Arif */ -public class ProfileActivity extends BaseActivity { +public class ProfileActivity extends BaseActivity implements BottomSheetUserProfileFragment.BottomSheetListener { private String username; + private boolean following; @Override public void onCreate(Bundle savedInstanceState) { @@ -94,9 +103,86 @@ public class ProfileActivity extends BaseActivity { ((TextView) tabViewChild).setTypeface(myTypeface); } } + + if(!username.equals(tinyDB.getString("userLogin"))) { + checkFollowStatus(); + } } } + @Override + public void onButtonClicked(String text) { + if(text.equals("follow")) { + followUnfollow(); + } + } + + private void checkFollowStatus() { + RetrofitClient.getApiInterface(this).checkFollowing(Authorization.get(this), username).enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.code() == 204) { + following = true; + } + else if(response.code() == 404) { + following = false; + } + else { + following = false; + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + following = false; + } + }); + } + + private void followUnfollow() { + Call call; + if (following) { + call = RetrofitClient.getApiInterface(this).unfollowUser(Authorization.get(this), username); + } + else { + call = RetrofitClient.getApiInterface(this).followUser(Authorization.get(this), username); + } + + call.enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + following = !following; + if (following) { + Toasty.success(ProfileActivity.this, String.format(getString(R.string.nowFollowUser), username)); + } + else { + Toasty.success(ProfileActivity.this, String.format(getString(R.string.unfollowedUser), username)); + } + } else { + if (following) { + Toasty.error(ProfileActivity.this, getString(R.string.unfollowingFailed)); + } + else { + Toasty.error(ProfileActivity.this, getString(R.string.followingFailed)); + } + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + if (following) { + Toasty.error(ProfileActivity.this, getString(R.string.unfollowingFailed)); + } + else { + Toasty.error(ProfileActivity.this, getString(R.string.followingFailed)); + } + } + }); + } + public class ViewPagerAdapter extends FragmentStateAdapter { public ViewPagerAdapter(@NonNull FragmentActivity fa) { super(fa); } @@ -136,8 +222,21 @@ public class ProfileActivity extends BaseActivity { finish(); return true; } + else if(id == R.id.genericMenu) { + new BottomSheetUserProfileFragment(following).show(getSupportFragmentManager(), "userProfileBottomSheet"); + return true; + } else { return super.onOptionsItemSelected(item); } } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + if(!username.equals(tinyDB.getString("userLogin"))) { + getMenuInflater().inflate(R.menu.generic_nav_dotted_menu, menu); + } + return super.onCreateOptionsMenu(menu); + } + } diff --git a/app/src/main/java/org/mian/gitnex/fragments/BottomSheetUserProfileFragment.java b/app/src/main/java/org/mian/gitnex/fragments/BottomSheetUserProfileFragment.java new file mode 100644 index 00000000..9fb5a3d9 --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/fragments/BottomSheetUserProfileFragment.java @@ -0,0 +1,70 @@ +package org.mian.gitnex.fragments; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.content.res.AppCompatResources; +import com.google.android.material.bottomsheet.BottomSheetDialogFragment; +import org.mian.gitnex.R; +import org.mian.gitnex.databinding.BottomSheetUserProfileBinding; + +/** + * Template Author M M Arif + * @author qwerty287 + */ + +public class BottomSheetUserProfileFragment extends BottomSheetDialogFragment { + + private final boolean following; + + public BottomSheetUserProfileFragment(boolean following) { + this.following = following; + } + + private BottomSheetUserProfileFragment.BottomSheetListener bmListener; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + + BottomSheetUserProfileBinding bottomSheetUserProfileBinding = BottomSheetUserProfileBinding.inflate(inflater, container, false); + + if(following) { + bottomSheetUserProfileBinding.followUnfollowUser.setText(R.string.unfollowUser); + Drawable drawable = AppCompatResources.getDrawable(requireContext(), R.drawable.ic_person_remove); assert drawable != null; + drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); + bottomSheetUserProfileBinding.followUnfollowUser.setCompoundDrawablesRelative(drawable, null, null, null); + } + + bottomSheetUserProfileBinding.followUnfollowUser.setOnClickListener(v1 -> { + + bmListener.onButtonClicked("follow"); + dismiss(); + + }); + + return bottomSheetUserProfileBinding.getRoot(); + } + + public interface BottomSheetListener { + void onButtonClicked(String text); + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + + try { + bmListener = (BottomSheetUserProfileFragment.BottomSheetListener) context; + } + catch (ClassCastException e) { + throw new ClassCastException(context.toString() + " must implement BottomSheetListener"); + } + } + +} diff --git a/app/src/main/res/drawable/ic_person_remove.xml b/app/src/main/res/drawable/ic_person_remove.xml new file mode 100644 index 00000000..5ba9a887 --- /dev/null +++ b/app/src/main/res/drawable/ic_person_remove.xml @@ -0,0 +1,27 @@ + + + + + diff --git a/app/src/main/res/layout/bottom_sheet_user_profile.xml b/app/src/main/res/layout/bottom_sheet_user_profile.xml new file mode 100644 index 00000000..28a9be1e --- /dev/null +++ b/app/src/main/res/layout/bottom_sheet_user_profile.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0779def5..dbb08e82 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -754,4 +754,10 @@ Updated %s Joined + Follow + Unfollow + Unfollowed @%s + You now follow @%s + Couldn\'t unfollow user + Couldn\'t follow user