From 69745b027e151bebe58271782aef61ec1229d4a6 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Sat, 12 Oct 2019 14:00:36 +0500 Subject: [PATCH] Implemented watch and unwatch a repository --- .../gitnex/actions/RepositoryActions.java | 128 ++++++++++++++++++ .../gitnex/activities/RepoDetailActivity.java | 38 ++++++ .../fragments/RepoBottomSheetFragment.java | 37 +++++ .../mian/gitnex/interfaces/ApiInterface.java | 10 ++ .../mian/gitnex/models/WatchRepository.java | 39 ++++++ app/src/main/res/drawable/ic_unwatch.xml | 6 + .../res/layout/repo_bottom_sheet_layout.xml | 24 ++++ app/src/main/res/values/strings.xml | 8 +- 8 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/mian/gitnex/models/WatchRepository.java create mode 100644 app/src/main/res/drawable/ic_unwatch.xml diff --git a/app/src/main/java/org/mian/gitnex/actions/RepositoryActions.java b/app/src/main/java/org/mian/gitnex/actions/RepositoryActions.java index 76d338c2..01a9e3cf 100644 --- a/app/src/main/java/org/mian/gitnex/actions/RepositoryActions.java +++ b/app/src/main/java/org/mian/gitnex/actions/RepositoryActions.java @@ -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 call; + + call = RetrofitClient + .getInstance(instanceUrl) + .getApiInterface() + .watchRepository(Authorization.returnAuthentication(context, loginUid, instanceToken), repoOwner, repoName); + + call.enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response 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 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 call; + + call = RetrofitClient + .getInstance(instanceUrl) + .getApiInterface() + .unWatchRepository(Authorization.returnAuthentication(context, loginUid, instanceToken), repoOwner, repoName); + + call.enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response 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 call, @NonNull Throwable t) { + Log.e("onFailure", t.toString()); + } + }); + + } + } diff --git a/app/src/main/java/org/mian/gitnex/activities/RepoDetailActivity.java b/app/src/main/java/org/mian/gitnex/activities/RepoDetailActivity.java index edacedf1..5edb5c36 100644 --- a/app/src/main/java/org/mian/gitnex/activities/RepoDetailActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/RepoDetailActivity.java @@ -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 call; + + call = RetrofitClient + .getInstance(instanceUrl) + .getApiInterface() + .checkRepoWatchStatus(instanceToken, owner, repo); + + call.enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response 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 call, @NonNull Throwable t) { + Log.e("onFailure", t.toString()); + } + }); + + } + } diff --git a/app/src/main/java/org/mian/gitnex/fragments/RepoBottomSheetFragment.java b/app/src/main/java/org/mian/gitnex/fragments/RepoBottomSheetFragment.java index 7ca1f4fb..23ad2fde 100644 --- a/app/src/main/java/org/mian/gitnex/fragments/RepoBottomSheetFragment.java +++ b/app/src/main/java/org/mian/gitnex/fragments/RepoBottomSheetFragment.java @@ -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(); } diff --git a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java index 8d41eb35..74ac11bc 100644 --- a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java +++ b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java @@ -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 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 checkRepoWatchStatus(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName); + + @PUT("repos/{owner}/{repo}/subscription") // watch a repository + Call watchRepository(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName); + + @DELETE("repos/{owner}/{repo}/subscription") // un watch a repository + Call unWatchRepository(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName); } diff --git a/app/src/main/java/org/mian/gitnex/models/WatchRepository.java b/app/src/main/java/org/mian/gitnex/models/WatchRepository.java new file mode 100644 index 00000000..0258f8ff --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/models/WatchRepository.java @@ -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; + } +} diff --git a/app/src/main/res/drawable/ic_unwatch.xml b/app/src/main/res/drawable/ic_unwatch.xml new file mode 100644 index 00000000..3d2f50d8 --- /dev/null +++ b/app/src/main/res/drawable/ic_unwatch.xml @@ -0,0 +1,6 @@ + + + + diff --git a/app/src/main/res/layout/repo_bottom_sheet_layout.xml b/app/src/main/res/layout/repo_bottom_sheet_layout.xml index 18f1b0ef..6ebc1747 100644 --- a/app/src/main/res/layout/repo_bottom_sheet_layout.xml +++ b/app/src/main/res/layout/repo_bottom_sheet_layout.xml @@ -115,6 +115,30 @@ android:textSize="16sp" android:padding="16dp" /> + + + + Explore repositories Star Repository Unstar Repository - Repository starred - Repository unstarred + Repository added to starred list + Repository removed from starred list + Watch Repository + Unwatch Repository + Repository added to watch list + Repository removed from watch list