Add options to follow and unfollow users (#971)

### Describe what your pull request does and which issue you’re targeting
<!-- Create a new issue, if it doesn't exist yet -->
Adds options to follow and unfollow users. Closes #968
<br><br>

<!-- Make sure you are targeting the "main" branch, pull requests on release branches are only allowed for bug fixes. -->

- [X] I carefully read the [contribution guidelines](https://codeberg.org/GitNex/GitNex/src/branch/main/CONTRIBUTING.md).
- [X] I'm following the code standards as defined [here](https://codeberg.org/gitnex/GitNex/wiki/Code-Standards).
- [X] By submitting this pull request, I permit GitNex to license my work under the [GNU General Public License v3](https://codeberg.org/GitNex/GitNex/src/branch/main/LICENSE).

Co-authored-by: qwerty287 <ndev@web.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/971
Reviewed-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
This commit is contained in:
qwerty287 2021-08-31 09:33:26 +02:00 committed by M M Arif
parent 244dc12fc3
commit 3a38e4d7d5
6 changed files with 244 additions and 2 deletions

View File

@ -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'

View File

@ -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<JsonElement>() {
@Override
public void onResponse(@NonNull Call<JsonElement> call, @NonNull Response<JsonElement> response) {
if(response.code() == 204) {
following = true;
}
else if(response.code() == 404) {
following = false;
}
else {
following = false;
}
}
@Override
public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
following = false;
}
});
}
private void followUnfollow() {
Call<JsonElement> 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<JsonElement>() {
@Override
public void onResponse(@NonNull Call<JsonElement> call, @NonNull Response<JsonElement> 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<JsonElement> 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);
}
}

View File

@ -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");
}
}
}

View File

@ -0,0 +1,27 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M16,21v-2a4,4 0,0 0,-4 -4H5a4,4 0,0 0,-4 4v2"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="?attr/iconsColor"
android:strokeLineCap="round"/>
<path
android:pathData="M8.5,7m-4,0a4,4 0,1 1,8 0a4,4 0,1 1,-8 0"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="?attr/iconsColor"
android:strokeLineCap="round"/>
<path
android:pathData="M23,11L17,11"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="?attr/iconsColor"
android:strokeLineCap="round"/>
</vector>

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="6dp"
android:paddingBottom="12dp"
android:background="?attr/primaryBackgroundColor">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/followUnfollowUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:drawablePadding="24dp"
android:padding="12dp"
android:text="@string/userFollow"
android:textColor="?attr/primaryTextColor"
android:textSize="16sp"
app:drawableStartCompat="@drawable/ic_person_add" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>

View File

@ -754,4 +754,10 @@
<string name="lastUpdatedAt">Updated %s</string>
<string name="joined">Joined</string>
<string name="userFollow">Follow</string>
<string name="unfollowUser">Unfollow</string>
<string name="unfollowedUser">Unfollowed @%s</string>
<string name="nowFollowUser">You now follow @%s</string>
<string name="unfollowingFailed">Couldn\'t unfollow user</string>
<string name="followingFailed">Couldn\'t follow user</string>
</resources>