Merge branch '130-star-a-repo' of gitnex/GitNex into master
This commit is contained in:
		
							
								
								
									
										152
									
								
								app/src/main/java/org/mian/gitnex/actions/RepositoryActions.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										152
									
								
								app/src/main/java/org/mian/gitnex/actions/RepositoryActions.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,152 @@
 | 
			
		||||
package org.mian.gitnex.actions;
 | 
			
		||||
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import com.google.gson.JsonElement;
 | 
			
		||||
import org.mian.gitnex.R;
 | 
			
		||||
import org.mian.gitnex.clients.RetrofitClient;
 | 
			
		||||
import org.mian.gitnex.helpers.AlertDialogs;
 | 
			
		||||
import org.mian.gitnex.helpers.Authorization;
 | 
			
		||||
import org.mian.gitnex.helpers.Toasty;
 | 
			
		||||
import org.mian.gitnex.util.TinyDB;
 | 
			
		||||
import retrofit2.Call;
 | 
			
		||||
import retrofit2.Callback;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Author M M Arif
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
public class RepositoryActions {
 | 
			
		||||
 | 
			
		||||
    public static void starRepository(final Context context) {
 | 
			
		||||
 | 
			
		||||
        final TinyDB tinyDb = new TinyDB(context);
 | 
			
		||||
        final String instanceUrl = tinyDb.getString("instanceUrl");
 | 
			
		||||
        final String loginUid = tinyDb.getString("loginUid");
 | 
			
		||||
        final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
 | 
			
		||||
        String repoFullName = tinyDb.getString("repoFullName");
 | 
			
		||||
        String[] parts = repoFullName.split("/");
 | 
			
		||||
        final String repoOwner = parts[0];
 | 
			
		||||
        final String repoName = parts[1];
 | 
			
		||||
 | 
			
		||||
        Call<JsonElement> call;
 | 
			
		||||
 | 
			
		||||
        call = RetrofitClient
 | 
			
		||||
                .getInstance(instanceUrl)
 | 
			
		||||
                .getApiInterface()
 | 
			
		||||
                .starRepository(Authorization.returnAuthentication(context, loginUid, instanceToken), repoOwner, repoName);
 | 
			
		||||
 | 
			
		||||
        call.enqueue(new Callback<JsonElement>() {
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
 | 
			
		||||
 | 
			
		||||
                if(response.isSuccessful()) {
 | 
			
		||||
                    if(response.code() == 204) {
 | 
			
		||||
 | 
			
		||||
                        tinyDb.putBoolean("repoCreated", true);
 | 
			
		||||
                        Toasty.info(context, context.getString(R.string.starRepositorySuccess));
 | 
			
		||||
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                else if(response.code() == 401) {
 | 
			
		||||
 | 
			
		||||
                    AlertDialogs.authorizationTokenRevokedDialog(context, context.getResources().getString(R.string.alertDialogTokenRevokedTitle),
 | 
			
		||||
                            context.getResources().getString(R.string.alertDialogTokenRevokedMessage),
 | 
			
		||||
                            context.getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton),
 | 
			
		||||
                            context.getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else if(response.code() == 403) {
 | 
			
		||||
 | 
			
		||||
                    Toasty.info(context, context.getString(R.string.authorizeError));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else if(response.code() == 404) {
 | 
			
		||||
 | 
			
		||||
                    Toasty.info(context, context.getString(R.string.apiNotFound));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
 | 
			
		||||
                    Toasty.info(context, context.getString(R.string.genericError));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
 | 
			
		||||
                Log.e("onFailure", t.toString());
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void unStarRepository(final Context context) {
 | 
			
		||||
 | 
			
		||||
        final TinyDB tinyDb = new TinyDB(context);
 | 
			
		||||
        final String instanceUrl = tinyDb.getString("instanceUrl");
 | 
			
		||||
        final String loginUid = tinyDb.getString("loginUid");
 | 
			
		||||
        final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
 | 
			
		||||
        String repoFullName = tinyDb.getString("repoFullName");
 | 
			
		||||
        String[] parts = repoFullName.split("/");
 | 
			
		||||
        final String repoOwner = parts[0];
 | 
			
		||||
        final String repoName = parts[1];
 | 
			
		||||
 | 
			
		||||
        Call<JsonElement> call;
 | 
			
		||||
 | 
			
		||||
        call = RetrofitClient
 | 
			
		||||
                .getInstance(instanceUrl)
 | 
			
		||||
                .getApiInterface()
 | 
			
		||||
                .unStarRepository(Authorization.returnAuthentication(context, loginUid, instanceToken), repoOwner, repoName);
 | 
			
		||||
 | 
			
		||||
        call.enqueue(new Callback<JsonElement>() {
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
 | 
			
		||||
 | 
			
		||||
                if(response.isSuccessful()) {
 | 
			
		||||
                    if(response.code() == 204) {
 | 
			
		||||
 | 
			
		||||
                        tinyDb.putBoolean("repoCreated", true);
 | 
			
		||||
                        Toasty.info(context, context.getString(R.string.unStarRepositorySuccess));
 | 
			
		||||
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                else if(response.code() == 401) {
 | 
			
		||||
 | 
			
		||||
                    AlertDialogs.authorizationTokenRevokedDialog(context, context.getResources().getString(R.string.alertDialogTokenRevokedTitle),
 | 
			
		||||
                            context.getResources().getString(R.string.alertDialogTokenRevokedMessage),
 | 
			
		||||
                            context.getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton),
 | 
			
		||||
                            context.getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else if(response.code() == 403) {
 | 
			
		||||
 | 
			
		||||
                    Toasty.info(context, context.getString(R.string.authorizeError));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else if(response.code() == 404) {
 | 
			
		||||
 | 
			
		||||
                    Toasty.info(context, context.getString(R.string.apiNotFound));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
 | 
			
		||||
                    Toasty.info(context, context.getString(R.string.genericError));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
 | 
			
		||||
                Log.e("onFailure", t.toString());
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package org.mian.gitnex.activities;
 | 
			
		||||
 | 
			
		||||
import com.google.android.material.tabs.TabLayout;
 | 
			
		||||
import com.google.gson.JsonElement;
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.appcompat.app.AppCompatActivity;
 | 
			
		||||
import androidx.appcompat.widget.Toolbar;
 | 
			
		||||
@@ -97,6 +98,8 @@ public class RepoDetailActivity extends AppCompatActivity implements RepoBottomS
 | 
			
		||||
            openIssueTabView.setTextColor(textColor);
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        checkRepositoryStarStatus(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -267,4 +270,31 @@ public class RepoDetailActivity extends AppCompatActivity implements RepoBottomS
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void checkRepositoryStarStatus(String instanceUrl, String instanceToken, final String owner, String repo) {
 | 
			
		||||
 | 
			
		||||
        Call<JsonElement> call;
 | 
			
		||||
 | 
			
		||||
        call = RetrofitClient
 | 
			
		||||
                .getInstance(instanceUrl)
 | 
			
		||||
                .getApiInterface()
 | 
			
		||||
                .checkRepoStarStatus(instanceToken, owner, repo);
 | 
			
		||||
 | 
			
		||||
        call.enqueue(new Callback<JsonElement>() {
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
 | 
			
		||||
 | 
			
		||||
                TinyDB tinyDb = new TinyDB(getApplicationContext());
 | 
			
		||||
                tinyDb.putInt("repositoryStarStatus", response.code());
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
 | 
			
		||||
                Log.e("onFailure", t.toString());
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ import android.view.ViewGroup;
 | 
			
		||||
import android.widget.TextView;
 | 
			
		||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
 | 
			
		||||
import org.mian.gitnex.R;
 | 
			
		||||
import org.mian.gitnex.actions.RepositoryActions;
 | 
			
		||||
import org.mian.gitnex.util.TinyDB;
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.annotation.Nullable;
 | 
			
		||||
@@ -34,6 +35,8 @@ public class RepoBottomSheetFragment extends BottomSheetDialogFragment {
 | 
			
		||||
        TextView createRelease = v.findViewById(R.id.createRelease);
 | 
			
		||||
        TextView openWebRepo = v.findViewById(R.id.openWebRepo);
 | 
			
		||||
        TextView newFile = v.findViewById(R.id.newFile);
 | 
			
		||||
        TextView starRepository = v.findViewById(R.id.starRepository);
 | 
			
		||||
        TextView unStarRepository = v.findViewById(R.id.unStarRepository);
 | 
			
		||||
 | 
			
		||||
        createLabel.setOnClickListener(new View.OnClickListener() {
 | 
			
		||||
            @Override
 | 
			
		||||
@@ -97,6 +100,37 @@ public class RepoBottomSheetFragment extends BottomSheetDialogFragment {
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        if(tinyDb.getInt("repositoryStarStatus") == 204) { // star a repo
 | 
			
		||||
 | 
			
		||||
            starRepository.setVisibility(View.GONE);
 | 
			
		||||
 | 
			
		||||
            unStarRepository.setOnClickListener(new View.OnClickListener() {
 | 
			
		||||
                @Override
 | 
			
		||||
                public void onClick(View v) {
 | 
			
		||||
 | 
			
		||||
                    RepositoryActions.unStarRepository(getContext());
 | 
			
		||||
                    dismiss();
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        else if(tinyDb.getInt("repositoryStarStatus") == 404) {
 | 
			
		||||
 | 
			
		||||
            unStarRepository.setVisibility(View.GONE);
 | 
			
		||||
 | 
			
		||||
            starRepository.setOnClickListener(new View.OnClickListener() {
 | 
			
		||||
                @Override
 | 
			
		||||
                public void onClick(View v) {
 | 
			
		||||
 | 
			
		||||
                    RepositoryActions.starRepository(getContext());
 | 
			
		||||
                    dismiss();
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return v;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,7 @@ import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.annotation.Nullable;
 | 
			
		||||
import android.content.ClipboardManager;
 | 
			
		||||
import android.content.ClipData;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Author M M Arif
 | 
			
		||||
@@ -97,7 +98,7 @@ public class SingleIssueBottomSheetFragment extends BottomSheetDialogFragment {
 | 
			
		||||
                String issueUrl = instanceUrlWithProtocol + "/" + repoFullName + "/issues/" + tinyDB.getString("issueNumber");
 | 
			
		||||
 | 
			
		||||
                // copy to clipboard
 | 
			
		||||
                ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(android.content.Context.CLIPBOARD_SERVICE);
 | 
			
		||||
                ClipboardManager clipboard = (ClipboardManager) Objects.requireNonNull(getContext()).getSystemService(android.content.Context.CLIPBOARD_SERVICE);
 | 
			
		||||
                ClipData clip = ClipData.newPlainText("issueUrl", issueUrl);
 | 
			
		||||
                clipboard.setPrimaryClip(clip);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -228,4 +228,13 @@ public interface ApiInterface {
 | 
			
		||||
 | 
			
		||||
    @GET("repos/{owner}/{repo}/contents/{fileDir}") // get all the sub files and dirs of a repository
 | 
			
		||||
    Call<List<Files>> getDirFiles(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("fileDir") String fileDir);
 | 
			
		||||
 | 
			
		||||
    @GET("user/starred/{owner}/{repo}") // check star status of a repository
 | 
			
		||||
    Call<JsonElement> checkRepoStarStatus(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName);
 | 
			
		||||
 | 
			
		||||
    @PUT("user/starred/{owner}/{repo}") // star a repository
 | 
			
		||||
    Call<JsonElement> starRepository(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName);
 | 
			
		||||
 | 
			
		||||
    @DELETE("user/starred/{owner}/{repo}") // un star a repository
 | 
			
		||||
    Call<JsonElement> unStarRepository(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user