From 0caaa63b06164ec2b6cd7fb0ef3456a63f31a6fb Mon Sep 17 00:00:00 2001 From: M M Arif Date: Wed, 8 Apr 2020 23:31:27 +0500 Subject: [PATCH] Close/reopen milestone --- .../mian/gitnex/actions/MilestoneActions.java | 143 ++++++++++++++++++ .../activities/IssueDetailActivity.java | 5 - .../gitnex/adapters/MilestonesAdapter.java | 132 +++++++++------- .../mian/gitnex/interfaces/ApiInterface.java | 3 + .../org/mian/gitnex/models/Milestones.java | 4 + .../bottom_sheet_milestones_in_list.xml | 48 ++++++ app/src/main/res/layout/list_milestones.xml | 52 +++++-- app/src/main/res/values/strings.xml | 6 +- 8 files changed, 326 insertions(+), 67 deletions(-) create mode 100644 app/src/main/java/org/mian/gitnex/actions/MilestoneActions.java create mode 100644 app/src/main/res/layout/bottom_sheet_milestones_in_list.xml diff --git a/app/src/main/java/org/mian/gitnex/actions/MilestoneActions.java b/app/src/main/java/org/mian/gitnex/actions/MilestoneActions.java new file mode 100644 index 00000000..bf7a909e --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/actions/MilestoneActions.java @@ -0,0 +1,143 @@ +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.models.Milestones; +import org.mian.gitnex.util.TinyDB; +import org.mian.gitnex.viewmodels.MilestonesViewModel; +import retrofit2.Call; +import retrofit2.Callback; + +/** + * Author M M Arif + */ + +public class MilestoneActions { + + static final private String TAG = "MilestoneActions : "; + + public static void closeMilestone(final Context ctx, int milestoneId_) { + + final TinyDB tinyDB = new TinyDB(ctx); + + final String instanceUrl = tinyDB.getString("instanceUrl"); + String repoFullName = tinyDB.getString("repoFullName"); + String[] parts = repoFullName.split("/"); + final String repoOwner = parts[0]; + final String repoName = parts[1]; + final String loginUid = tinyDB.getString("loginUid"); + final String token = "token " + tinyDB.getString(loginUid + "-token"); + + Milestones milestoneStateJson = new Milestones("closed"); + Call call; + + call = RetrofitClient + .getInstance(instanceUrl, ctx) + .getApiInterface() + .closeReopenMilestone(token, repoOwner, repoName, milestoneId_, milestoneStateJson); + + call.enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + + if(response.isSuccessful()) { + + Toasty.info(ctx, ctx.getString(R.string.milestoneStatusUpdate)); + MilestonesViewModel.loadMilestonesList(instanceUrl, Authorization.returnAuthentication(ctx, loginUid, token), repoOwner, repoName, "all", ctx); + + } + else if(response.code() == 401) { + + AlertDialogs.authorizationTokenRevokedDialog(ctx, ctx.getResources().getString(R.string.alertDialogTokenRevokedTitle), + ctx.getResources().getString(R.string.alertDialogTokenRevokedMessage), + ctx.getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton), + ctx.getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton)); + + } + else { + + Toasty.info(ctx, ctx.getString(R.string.genericError)); + + } + + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + + Log.e(TAG, t.toString()); + + } + + }); + + + } + + public static void openMilestone(final Context ctx, int milestoneId_) { + + final TinyDB tinyDB = new TinyDB(ctx); + + final String instanceUrl = tinyDB.getString("instanceUrl"); + String repoFullName = tinyDB.getString("repoFullName"); + String[] parts = repoFullName.split("/"); + final String repoOwner = parts[0]; + final String repoName = parts[1]; + final String loginUid = tinyDB.getString("loginUid"); + final String token = "token " + tinyDB.getString(loginUid + "-token"); + + Milestones milestoneStateJson = new Milestones("open"); + Call call; + + call = RetrofitClient + .getInstance(instanceUrl, ctx) + .getApiInterface() + .closeReopenMilestone(token, repoOwner, repoName, milestoneId_, milestoneStateJson); + + call.enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + + if(response.isSuccessful()) { + + Toasty.info(ctx, ctx.getString(R.string.milestoneStatusUpdate)); + MilestonesViewModel.loadMilestonesList(instanceUrl, Authorization.returnAuthentication(ctx, loginUid, token), repoOwner, repoName, "all", ctx); + + } + else if(response.code() == 401) { + + AlertDialogs.authorizationTokenRevokedDialog(ctx, ctx.getResources().getString(R.string.alertDialogTokenRevokedTitle), + ctx.getResources().getString(R.string.alertDialogTokenRevokedMessage), + ctx.getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton), + ctx.getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton)); + + } + else { + + Toasty.info(ctx, ctx.getString(R.string.genericError)); + + } + + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + + Log.e(TAG, t.toString()); + + } + + }); + + } + +} diff --git a/app/src/main/java/org/mian/gitnex/activities/IssueDetailActivity.java b/app/src/main/java/org/mian/gitnex/activities/IssueDetailActivity.java index 13c2406b..70d7a8bf 100644 --- a/app/src/main/java/org/mian/gitnex/activities/IssueDetailActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/IssueDetailActivity.java @@ -68,7 +68,6 @@ import org.mian.gitnex.helpers.RoundedTransformation; import org.mian.gitnex.util.TinyDB; import org.mian.gitnex.helpers.ClickListener; import org.mian.gitnex.viewmodels.IssueCommentsViewModel; -import org.ocpsoft.prettytime.PrettyTime; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Collection; @@ -199,10 +198,6 @@ public class IssueDetailActivity extends BaseActivity { switch(tinyDb.getInt("customFontId")) { - case 0: - myTypeface = Typeface.createFromAsset(Objects.requireNonNull(getApplicationContext()).getAssets(), "fonts/roboto.ttf"); - break; - case 1: myTypeface = Typeface.createFromAsset(Objects.requireNonNull(getApplicationContext()).getAssets(), "fonts/manroperegular.ttf"); break; diff --git a/app/src/main/java/org/mian/gitnex/adapters/MilestonesAdapter.java b/app/src/main/java/org/mian/gitnex/adapters/MilestonesAdapter.java index b1c5af01..79d990c8 100644 --- a/app/src/main/java/org/mian/gitnex/adapters/MilestonesAdapter.java +++ b/app/src/main/java/org/mian/gitnex/adapters/MilestonesAdapter.java @@ -15,8 +15,10 @@ import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import com.amulyakhare.textdrawable.TextDrawable; +import com.google.android.material.bottomsheet.BottomSheetDialog; import com.vdurmont.emoji.EmojiParser; import org.mian.gitnex.R; +import org.mian.gitnex.actions.MilestoneActions; import org.mian.gitnex.helpers.ClickListener; import org.mian.gitnex.helpers.TimeHelper; import org.mian.gitnex.models.Milestones; @@ -32,7 +34,6 @@ import java.util.List; import java.util.Locale; import java.util.Objects; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.recyclerview.widget.RecyclerView; import io.noties.markwon.AbstractMarkwonPlugin; import io.noties.markwon.Markwon; @@ -42,7 +43,6 @@ import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; import io.noties.markwon.ext.tables.TablePlugin; import io.noties.markwon.ext.tasklist.TaskListPlugin; import io.noties.markwon.html.HtmlPlugin; -import io.noties.markwon.image.AsyncDrawable; import io.noties.markwon.image.DefaultMediaDecoder; import io.noties.markwon.image.ImageItem; import io.noties.markwon.image.ImagesPlugin; @@ -63,6 +63,7 @@ public class MilestonesAdapter extends RecyclerView.Adapter { - Context context = v.getContext(); - Log.i("issueNumber", issueNumber.getText().toString()); + Context ctx = v.getContext(); + int milestoneId_ = Integer.parseInt(milestoneId.getText().toString()); - Intent intent = new Intent(context, IssueDetailActivity.class); - intent.putExtra("issueNumber", issueNumber.getText()); + @SuppressLint("InflateParams") View view = LayoutInflater.from(ctx).inflate(R.layout.bottom_sheet_milestones_in_list, null); - TinyDB tinyDb = new TinyDB(context); - tinyDb.putString("issueNumber", issueNumber.getText().toString()); - context.startActivity(intent); + TextView closeMilestone = view.findViewById(R.id.closeMilestone); + TextView openMilestone = view.findViewById(R.id.openMilestone); + + BottomSheetDialog dialog = new BottomSheetDialog(ctx); + dialog.setContentView(view); + dialog.show(); + + if(milestoneStatus.getText().toString().equals("open")) { + + closeMilestone.setVisibility(View.VISIBLE); + openMilestone.setVisibility(View.GONE); + + } + else { + + closeMilestone.setVisibility(View.GONE); + openMilestone.setVisibility(View.VISIBLE); + + } + + closeMilestone.setOnClickListener(v12 -> { + + MilestoneActions.closeMilestone(ctx, milestoneId_); + dialog.dismiss(); + + }); + + openMilestone.setOnClickListener(v12 -> { + + MilestoneActions.openMilestone(ctx, milestoneId_); + dialog.dismiss(); + + }); + + }); - } - });*/ } } @@ -123,46 +155,40 @@ public class MilestonesAdapter extends RecyclerView.Adapter { + plugin.addSchemeHandler(new SchemeHandler() { + @NonNull + @Override + public ImageItem handle(@NonNull String raw, @NonNull Uri uri) { - final int resourceId = mCtx.getResources().getIdentifier( - raw.substring("drawable://".length()), - "drawable", - mCtx.getPackageName()); + final int resourceId = mCtx.getResources().getIdentifier( + raw.substring("drawable://".length()), + "drawable", + mCtx.getPackageName()); - final Drawable drawable = mCtx.getDrawable(resourceId); + final Drawable drawable = mCtx.getDrawable(resourceId); - assert drawable != null; - return ImageItem.withResult(drawable); - } + assert drawable != null; + return ImageItem.withResult(drawable); + } - @NonNull - @Override - public Collection supportedSchemes() { - return Collections.singleton("drawable"); - } - }); - plugin.placeholderProvider(new ImagesPlugin.PlaceholderProvider() { - @Nullable - @Override - public Drawable providePlaceholder(@NonNull AsyncDrawable drawable) { - return null; - } - }); - plugin.addMediaDecoder(GifMediaDecoder.create(false)); - plugin.addMediaDecoder(SvgMediaDecoder.create(mCtx.getResources())); - plugin.addMediaDecoder(SvgMediaDecoder.create()); - plugin.defaultMediaDecoder(DefaultMediaDecoder.create(mCtx.getResources())); - plugin.defaultMediaDecoder(DefaultMediaDecoder.create()); - } + @NonNull + @Override + public Collection supportedSchemes() { + return Collections.singleton("drawable"); + } + }); + plugin.placeholderProvider(drawable -> null); + plugin.addMediaDecoder(GifMediaDecoder.create(false)); + plugin.addMediaDecoder(SvgMediaDecoder.create(mCtx.getResources())); + plugin.addMediaDecoder(SvgMediaDecoder.create()); + plugin.defaultMediaDecoder(DefaultMediaDecoder.create(mCtx.getResources())); + plugin.defaultMediaDecoder(DefaultMediaDecoder.create()); })) .usePlugin(new AbstractMarkwonPlugin() { @Override @@ -182,7 +208,6 @@ public class MilestonesAdapter extends RecyclerView.Adapter filteredList = new ArrayList<>(); @@ -328,6 +355,7 @@ public class MilestonesAdapter extends RecyclerView.Adapter> getRepositoryCommits(@Header("Authorization") String token, @Path("owner") String owner, @Path("repo") String repo, @Query("page") int page, @Query("sha") String branchName); + + @PATCH("repos/{owner}/{repo}/milestones/{index}") // close / reopen milestone + Call closeReopenMilestone(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("index") int index, @Body Milestones jsonStr); } diff --git a/app/src/main/java/org/mian/gitnex/models/Milestones.java b/app/src/main/java/org/mian/gitnex/models/Milestones.java index 91ed30c3..0a16f2c2 100644 --- a/app/src/main/java/org/mian/gitnex/models/Milestones.java +++ b/app/src/main/java/org/mian/gitnex/models/Milestones.java @@ -25,6 +25,10 @@ public class Milestones { this.title = title; } + public Milestones(String state) { + this.state = state; + } + public int getId() { return id; } diff --git a/app/src/main/res/layout/bottom_sheet_milestones_in_list.xml b/app/src/main/res/layout/bottom_sheet_milestones_in_list.xml new file mode 100644 index 00000000..e28f62ea --- /dev/null +++ b/app/src/main/res/layout/bottom_sheet_milestones_in_list.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/list_milestones.xml b/app/src/main/res/layout/list_milestones.xml index c2089961..fd869b15 100644 --- a/app/src/main/res/layout/list_milestones.xml +++ b/app/src/main/res/layout/list_milestones.xml @@ -8,6 +8,18 @@ android:background="?attr/primaryBackgroundColor" android:orientation="vertical"> + + + + @@ -87,18 +100,39 @@ - + android:id="@+id/dueDateFrame" + android:orientation="horizontal" + android:layout_marginTop="3dp"> + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 259a9123..d2929776 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -183,7 +183,7 @@ Commenter:\u0020 #%1$d %2$s Milestone %1$s - Due %1$s + Due on %1$s Opened %1$s Assigned to: %1$s Comment @@ -602,4 +602,8 @@ Issue Subscription failed Issue Unsubscribed Issue Un-Subscription failed + + Close Milestone + Open Milestone + Milestone status updated successfully