Implemented star/unstar a repository
This commit is contained in:
parent
2189bd10bd
commit
4c0fd3c286
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;
|
package org.mian.gitnex.activities;
|
||||||
|
|
||||||
import com.google.android.material.tabs.TabLayout;
|
import com.google.android.material.tabs.TabLayout;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
@ -97,6 +98,8 @@ public class RepoDetailActivity extends AppCompatActivity implements RepoBottomS
|
|||||||
openIssueTabView.setTextColor(textColor);
|
openIssueTabView.setTextColor(textColor);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkRepositoryStarStatus(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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 android.widget.TextView;
|
||||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
|
||||||
import org.mian.gitnex.R;
|
import org.mian.gitnex.R;
|
||||||
|
import org.mian.gitnex.actions.RepositoryActions;
|
||||||
import org.mian.gitnex.util.TinyDB;
|
import org.mian.gitnex.util.TinyDB;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
@ -34,6 +35,8 @@ public class RepoBottomSheetFragment extends BottomSheetDialogFragment {
|
|||||||
TextView createRelease = v.findViewById(R.id.createRelease);
|
TextView createRelease = v.findViewById(R.id.createRelease);
|
||||||
TextView openWebRepo = v.findViewById(R.id.openWebRepo);
|
TextView openWebRepo = v.findViewById(R.id.openWebRepo);
|
||||||
TextView newFile = v.findViewById(R.id.newFile);
|
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() {
|
createLabel.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@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;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import android.content.ClipboardManager;
|
import android.content.ClipboardManager;
|
||||||
import android.content.ClipData;
|
import android.content.ClipData;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author M M Arif
|
* Author M M Arif
|
||||||
@ -97,7 +98,7 @@ public class SingleIssueBottomSheetFragment extends BottomSheetDialogFragment {
|
|||||||
String issueUrl = instanceUrlWithProtocol + "/" + repoFullName + "/issues/" + tinyDB.getString("issueNumber");
|
String issueUrl = instanceUrlWithProtocol + "/" + repoFullName + "/issues/" + tinyDB.getString("issueNumber");
|
||||||
|
|
||||||
// copy to clipboard
|
// 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);
|
ClipData clip = ClipData.newPlainText("issueUrl", issueUrl);
|
||||||
clipboard.setPrimaryClip(clip);
|
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
|
@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);
|
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);
|
||||||
}
|
}
|
||||||
|
5
app/src/main/res/drawable/ic_star_border_24dp.xml
Normal file
5
app/src/main/res/drawable/ic_star_border_24dp.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<vector android:height="24dp" android:tint="#FFFFFF"
|
||||||
|
android:viewportHeight="24.0" android:viewportWidth="24.0"
|
||||||
|
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<path android:fillColor="#FF000000" android:pathData="M22,9.24l-7.19,-0.62L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21 12,17.27 18.18,21l-1.63,-7.03L22,9.24zM12,15.4l-3.76,2.27 1,-4.28 -3.32,-2.88 4.38,-0.38L12,6.1l1.71,4.04 4.38,0.38 -3.32,2.88 1,4.28L12,15.4z"/>
|
||||||
|
</vector>
|
@ -91,6 +91,30 @@
|
|||||||
android:textSize="16sp"
|
android:textSize="16sp"
|
||||||
android:padding="16dp" />
|
android:padding="16dp" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/starRepository"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_vertical"
|
||||||
|
android:text="@string/starRepository"
|
||||||
|
android:drawableStart="@drawable/ic_star_border_24dp"
|
||||||
|
android:drawablePadding="24dp"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:padding="16dp" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/unStarRepository"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_vertical"
|
||||||
|
android:text="@string/unStarRepository"
|
||||||
|
android:drawableStart="@drawable/ic_star"
|
||||||
|
android:drawablePadding="24dp"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:padding="16dp" />
|
||||||
|
|
||||||
<View style="@style/lineDividerHorizontal" />
|
<View style="@style/lineDividerHorizontal" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
@ -504,5 +504,9 @@
|
|||||||
|
|
||||||
<string name="translateText">Translate GitNex with Crowdin</string>
|
<string name="translateText">Translate GitNex with Crowdin</string>
|
||||||
<string name="exploreTextBoxHint">Explore repositories</string>
|
<string name="exploreTextBoxHint">Explore repositories</string>
|
||||||
|
<string name="starRepository">Star Repository</string>
|
||||||
|
<string name="unStarRepository">Unstar Repository</string>
|
||||||
|
<string name="starRepositorySuccess">Repository starred</string>
|
||||||
|
<string name="unStarRepositorySuccess">Repository unstarred</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user