Implemented watch and unwatch a repository

This commit is contained in:
M M Arif 2019-10-12 14:00:36 +05:00
parent 7f3aed9bae
commit 69745b027e
8 changed files with 288 additions and 2 deletions

View File

@ -149,4 +149,132 @@ public class RepositoryActions {
}
public static void watchRepository(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()
.watchRepository(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() == 200) {
tinyDb.putBoolean("repoCreated", true);
Toasty.info(context, context.getString(R.string.watchRepositorySuccess));
}
}
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 unWatchRepository(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()
.unWatchRepository(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.code() == 204) {
tinyDb.putBoolean("repoCreated", true);
Toasty.info(context, context.getString(R.string.unWatchRepositorySuccess));
}
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());
}
});
}
}

View File

@ -36,6 +36,7 @@ import org.mian.gitnex.fragments.RepoBottomSheetFragment;
import org.mian.gitnex.fragments.RepoInfoFragment;
import org.mian.gitnex.helpers.Authorization;
import org.mian.gitnex.models.UserRepositories;
import org.mian.gitnex.models.WatchRepository;
import org.mian.gitnex.util.AppUtil;
import org.mian.gitnex.util.TinyDB;
import java.util.Objects;
@ -100,6 +101,7 @@ public class RepoDetailActivity extends AppCompatActivity implements RepoBottomS
}
checkRepositoryStarStatus(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName1);
checkRepositoryWatchStatus(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName1);
}
@Override
@ -297,4 +299,40 @@ public class RepoDetailActivity extends AppCompatActivity implements RepoBottomS
}
private void checkRepositoryWatchStatus(String instanceUrl, String instanceToken, final String owner, String repo) {
Call<WatchRepository> call;
call = RetrofitClient
.getInstance(instanceUrl)
.getApiInterface()
.checkRepoWatchStatus(instanceToken, owner, repo);
call.enqueue(new Callback<WatchRepository>() {
@Override
public void onResponse(@NonNull Call<WatchRepository> call, @NonNull retrofit2.Response<WatchRepository> response) {
TinyDB tinyDb = new TinyDB(getApplicationContext());
if(response.code() == 200) {
assert response.body() != null;
if(response.body().getSubscribed()) {
tinyDb.putBoolean("repositoryWatchStatus", true);
}
}
else {
tinyDb.putBoolean("repositoryWatchStatus", false);
}
}
@Override
public void onFailure(@NonNull Call<WatchRepository> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
}
});
}
}

View File

@ -37,6 +37,8 @@ public class RepoBottomSheetFragment extends BottomSheetDialogFragment {
TextView newFile = v.findViewById(R.id.newFile);
TextView starRepository = v.findViewById(R.id.starRepository);
TextView unStarRepository = v.findViewById(R.id.unStarRepository);
TextView watchRepository = v.findViewById(R.id.watchRepository);
TextView unWatchRepository = v.findViewById(R.id.unWatchRepository);
createLabel.setOnClickListener(new View.OnClickListener() {
@Override
@ -109,6 +111,7 @@ public class RepoBottomSheetFragment extends BottomSheetDialogFragment {
public void onClick(View v) {
RepositoryActions.unStarRepository(getContext());
tinyDb.putInt("repositoryStarStatus", 404);
dismiss();
}
@ -124,6 +127,40 @@ public class RepoBottomSheetFragment extends BottomSheetDialogFragment {
public void onClick(View v) {
RepositoryActions.starRepository(getContext());
tinyDb.putInt("repositoryStarStatus", 204);
dismiss();
}
});
}
if(tinyDb.getBoolean("repositoryWatchStatus")) { // watch a repo
watchRepository.setVisibility(View.GONE);
unWatchRepository.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RepositoryActions.unWatchRepository(getContext());
tinyDb.putBoolean("repositoryWatchStatus", false);
dismiss();
}
});
}
else {
unWatchRepository.setVisibility(View.GONE);
watchRepository.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RepositoryActions.watchRepository(getContext());
tinyDb.putBoolean("repositoryWatchStatus", true);
dismiss();
}

View File

@ -28,6 +28,7 @@ import org.mian.gitnex.models.UserOrganizations;
import org.mian.gitnex.models.UserRepositories;
import org.mian.gitnex.models.UserSearch;
import org.mian.gitnex.models.UserTokens;
import org.mian.gitnex.models.WatchRepository;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
@ -237,4 +238,13 @@ public interface ApiInterface {
@DELETE("user/starred/{owner}/{repo}") // un star a repository
Call<JsonElement> unStarRepository(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName);
@GET("repos/{owner}/{repo}/subscription") // check watch status of a repository
Call<WatchRepository> checkRepoWatchStatus(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName);
@PUT("repos/{owner}/{repo}/subscription") // watch a repository
Call<JsonElement> watchRepository(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName);
@DELETE("repos/{owner}/{repo}/subscription") // un watch a repository
Call<JsonElement> unWatchRepository(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName);
}

View File

@ -0,0 +1,39 @@
package org.mian.gitnex.models;
/**
* Author M M Arif
*/
public class WatchRepository {
private Boolean subscribed;
private Boolean ignored;
private String reason;
private String created_at;
private String url;
private String repository_url;
public Boolean getSubscribed() {
return subscribed;
}
public Boolean getIgnored() {
return ignored;
}
public String getReason() {
return reason;
}
public String getCreated_at() {
return created_at;
}
public String getUrl() {
return url;
}
public String getRepository_url() {
return repository_url;
}
}

View File

@ -0,0 +1,6 @@
<vector android:autoMirrored="true" android:height="24dp"
android:viewportHeight="511.999" android:viewportWidth="511.999"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFFFF" android:pathData="M508.745,246.041c-4.574,-6.257 -113.557,-153.206 -252.748,-153.206S7.818,239.784 3.249,246.035c-4.332,5.936 -4.332,13.987 0,19.923c4.569,6.257 113.557,153.206 252.748,153.206s248.174,-146.95 252.748,-153.201C513.083,260.028 513.083,251.971 508.745,246.041zM255.997,385.406c-102.529,0 -191.33,-97.533 -217.617,-129.418c26.253,-31.913 114.868,-129.395 217.617,-129.395c102.524,0 191.319,97.516 217.617,129.418C447.361,287.923 358.746,385.406 255.997,385.406z"/>
<path android:fillColor="#FFFFFFFF" android:pathData="M255.997,154.725c-55.842,0 -101.275,45.433 -101.275,101.275s45.433,101.275 101.275,101.275s101.275,-45.433 101.275,-101.275S311.839,154.725 255.997,154.725zM255.997,323.516c-37.23,0 -67.516,-30.287 -67.516,-67.516s30.287,-67.516 67.516,-67.516s67.516,30.287 67.516,67.516S293.227,323.516 255.997,323.516z"/>
</vector>

View File

@ -115,6 +115,30 @@
android:textSize="16sp"
android:padding="16dp" />
<TextView
android:id="@+id/watchRepository"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/watchRepository"
android:drawableStart="@drawable/ic_unwatch"
android:drawablePadding="24dp"
android:textColor="@color/white"
android:textSize="16sp"
android:padding="16dp" />
<TextView
android:id="@+id/unWatchRepository"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/unWatchRepository"
android:drawableStart="@drawable/ic_watchers"
android:drawablePadding="24dp"
android:textColor="@color/white"
android:textSize="16sp"
android:padding="16dp" />
<View style="@style/lineDividerHorizontal" />
<TextView

View File

@ -506,7 +506,11 @@
<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>
<string name="starRepositorySuccess">Repository added to starred list</string>
<string name="unStarRepositorySuccess">Repository removed from starred list</string>
<string name="watchRepository">Watch Repository</string>
<string name="unWatchRepository">Unwatch Repository</string>
<string name="watchRepositorySuccess">Repository added to watch list</string>
<string name="unWatchRepositorySuccess">Repository removed from watch list</string>
</resources>