From 76137c56bcd5975fd16af78e20de59c0cab36946 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Tue, 18 Aug 2020 16:11:31 +0200 Subject: [PATCH] Improve drafts ui, add edited comments, create new ones on each call (#628) Merge branch 'master' into 627-save-edit-draft # Conflicts: # app/src/main/java/org/mian/gitnex/actions/IssueActions.java Merge branch 'master' into 627-save-edit-draft Improve drafts, add edited comments, create new ones on each call. Co-authored-by: M M Arif Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/628 --- .../org/mian/gitnex/actions/IssueActions.java | 9 +++- .../activities/ReplyToIssueActivity.java | 50 +++++++++++------ .../mian/gitnex/adapters/DraftsAdapter.java | 22 +++++++- .../mian/gitnex/database/api/DraftsApi.java | 15 +++--- .../mian/gitnex/database/dao/DraftsDao.java | 12 ++--- .../gitnex/database/db/GitnexDatabase.java | 6 +-- .../mian/gitnex/database/models/Draft.java | 13 +++++ .../database/models/DraftWithRepository.java | 11 ++++ app/src/main/res/layout/list_drafts.xml | 54 +++++++++++++------ 9 files changed, 143 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/org/mian/gitnex/actions/IssueActions.java b/app/src/main/java/org/mian/gitnex/actions/IssueActions.java index 4e00b1f3..9f51002a 100644 --- a/app/src/main/java/org/mian/gitnex/actions/IssueActions.java +++ b/app/src/main/java/org/mian/gitnex/actions/IssueActions.java @@ -7,6 +7,7 @@ import com.google.gson.JsonElement; import org.mian.gitnex.R; import org.mian.gitnex.activities.ReplyToIssueActivity; import org.mian.gitnex.clients.RetrofitClient; +import org.mian.gitnex.database.api.DraftsApi; import org.mian.gitnex.helpers.AlertDialogs; import org.mian.gitnex.helpers.Authorization; import org.mian.gitnex.helpers.TinyDB; @@ -22,7 +23,7 @@ import retrofit2.Callback; public class IssueActions { - public static void editIssueComment(final Context ctx, final int commentId, final String commentBody) { + public static void editIssueComment(final Context ctx, final int commentId, final String commentBody, long draftIdOnCreate) { final TinyDB tinyDb = new TinyDB(ctx); final String instanceUrl = tinyDb.getString("instanceUrl"); @@ -47,7 +48,11 @@ public class IssueActions { if(response.code() == 200) { tinyDb.putBoolean("commentEdited", true); - Toasty.success(ctx, ctx.getString(R.string.editCommentUpdatedText)); + Toasty.info(ctx, ctx.getString(R.string.editCommentUpdatedText)); + + DraftsApi draftsApi = new DraftsApi(ctx); + draftsApi.deleteSingleDraft((int) draftIdOnCreate); + ((ReplyToIssueActivity) ctx).finish(); } diff --git a/app/src/main/java/org/mian/gitnex/activities/ReplyToIssueActivity.java b/app/src/main/java/org/mian/gitnex/activities/ReplyToIssueActivity.java index 70feeedd..64bd24a4 100644 --- a/app/src/main/java/org/mian/gitnex/activities/ReplyToIssueActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/ReplyToIssueActivity.java @@ -33,6 +33,7 @@ import org.mian.gitnex.helpers.Toasty; import org.mian.gitnex.models.Collaborators; import org.mian.gitnex.models.Issues; import java.util.List; +import java.util.Objects; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -54,7 +55,7 @@ public class ReplyToIssueActivity extends BaseActivity { private ArrayAdapter defaultMentionAdapter; private Button replyButton; private String TAG = StaticGlobalVariables.replyToIssueActivity; - private long draftId; + private long draftIdOnCreate; @Override protected int getLayoutResourceId(){ @@ -97,6 +98,15 @@ public class ReplyToIssueActivity extends BaseActivity { initCloseListener(); closeActivity.setOnClickListener(onClickListener); + if(getIntent().getStringExtra("draftId") != null) { + + draftIdOnCreate = Long.parseLong(Objects.requireNonNull(getIntent().getStringExtra("draftId"))); + } + else { + + draftIdOnCreate = returnDraftId(); + } + replyButton = findViewById(R.id.replyButton); if(getIntent().getStringExtra("commentBody") != null) { @@ -115,7 +125,7 @@ public class ReplyToIssueActivity extends BaseActivity { } - if(getIntent().getStringExtra("commentAction") != null && getIntent().getStringExtra("commentAction").equals("edit")) { + if(getIntent().getStringExtra("commentAction") != null && Objects.equals(getIntent().getStringExtra("commentAction"), "edit") && !Objects.equals(getIntent().getStringExtra("commentId"), "new")) { final String commentId = getIntent().getStringExtra("commentId"); @@ -134,7 +144,7 @@ public class ReplyToIssueActivity extends BaseActivity { public void onTextChanged(CharSequence s, int start, int before, int count) { - saveDraft(addComment.getText().toString()); + saveDraft(addComment.getText().toString(), commentId, draftIdOnCreate); draftSaved.setVisibility(View.VISIBLE); } @@ -145,7 +155,7 @@ public class ReplyToIssueActivity extends BaseActivity { disableProcessButton(); assert commentId != null; - IssueActions.editIssueComment(ctx, Integer.parseInt(commentId), addComment.getText().toString()); + IssueActions.editIssueComment(ctx, Integer.parseInt(commentId), addComment.getText().toString(), draftIdOnCreate); }); @@ -165,7 +175,7 @@ public class ReplyToIssueActivity extends BaseActivity { public void onTextChanged(CharSequence s, int start, int before, int count) { - saveDraft(addComment.getText().toString()); + saveDraft(addComment.getText().toString(), "new", draftIdOnCreate); draftSaved.setVisibility(View.VISIBLE); } @@ -185,7 +195,7 @@ public class ReplyToIssueActivity extends BaseActivity { } - private void saveDraft(String draftText) { + private void saveDraft(String draftText, String commentId, long draftIdOnCreate) { TinyDB tinyDb = new TinyDB(getApplicationContext()); @@ -195,17 +205,31 @@ public class ReplyToIssueActivity extends BaseActivity { DraftsApi draftsApi = new DraftsApi(appCtx); - int countDraft = draftsApi.checkDraft(issueNumber, repositoryId); + if(draftIdOnCreate == 0) { - if(countDraft == 0) { - draftId = draftsApi.insertDraft(repositoryId, currentActiveAccountId, issueNumber, draftText, StaticGlobalVariables.draftTypeComment); + draftsApi.insertDraft(repositoryId, currentActiveAccountId, issueNumber, draftText, StaticGlobalVariables.draftTypeComment, commentId); } else { - DraftsApi.updateDraftByIssueIdAsyncTask(draftText, issueNumber, repositoryId); + + DraftsApi.updateDraft(draftText, (int) draftIdOnCreate, commentId); //updateDraftByIssueIdAsyncTask(draftText, issueNumber, repositoryId, commentId); } } + private long returnDraftId() { + + TinyDB tinyDb = new TinyDB(getApplicationContext()); + + int repositoryId = (int) tinyDb.getLong("repositoryId", 0); + int currentActiveAccountId = tinyDb.getInt("currentActiveAccountId"); + int issueNumber = Integer.parseInt(tinyDb.getString("issueNumber")); + + DraftsApi draftsApi = new DraftsApi(appCtx); + + return draftsApi.insertDraft(repositoryId, currentActiveAccountId, issueNumber, "", StaticGlobalVariables.draftTypeComment, ""); + + } + public void loadCollaboratorsList() { final TinyDB tinyDb = new TinyDB(appCtx); @@ -325,12 +349,8 @@ public class ReplyToIssueActivity extends BaseActivity { // delete draft comment if(tinyDb.getBoolean("draftsCommentsDeletionEnabled")) { - int repositoryId = (int) tinyDb.getLong("repositoryId", 0); - int issueNumber = Integer.parseInt(tinyDb.getString("issueNumber")); - DraftsApi draftsApi = new DraftsApi(appCtx); - draftId = draftsApi.getDraftIdAsync(issueNumber, repositoryId); - draftsApi.deleteSingleDraft((int) draftId); + draftsApi.deleteSingleDraft((int) draftIdOnCreate); } finish(); diff --git a/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java b/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java index 6a111544..4557dc2b 100644 --- a/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java +++ b/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java @@ -4,6 +4,7 @@ import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.text.Html; +import android.text.Spanned; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -38,6 +39,8 @@ public class DraftsAdapter extends RecyclerView.Adapter { @@ -69,6 +74,12 @@ public class DraftsAdapter extends RecyclerView.Adapter" + mCtx.getResources().getString(R.string.hash) + currentItem.getIssueId() + ""; - holder.repoInfo.setText(Html.fromHtml(issueNumber + " " + currentItem.getRepositoryOwner() + " / " + currentItem.getRepositoryName())); + Spanned headTitle = Html.fromHtml(issueNumber + " " + currentItem.getRepositoryOwner() + " / " + currentItem.getRepositoryName()); + holder.repoInfo.setText(headTitle); + + if(!currentItem.getCommentId().equalsIgnoreCase("new")) { + holder.editCommentStatus.setVisibility(View.VISIBLE); + } + else { + holder.editCommentStatus.setVisibility(View.GONE); + } } diff --git a/app/src/main/java/org/mian/gitnex/database/api/DraftsApi.java b/app/src/main/java/org/mian/gitnex/database/api/DraftsApi.java index bee3e7ac..61733dd2 100644 --- a/app/src/main/java/org/mian/gitnex/database/api/DraftsApi.java +++ b/app/src/main/java/org/mian/gitnex/database/api/DraftsApi.java @@ -27,7 +27,7 @@ public class DraftsApi { draftsDao = db.draftsDao(); } - public long insertDraft(int repositoryId, int draftAccountId, int issueId, String draftText, String draftType) { + public long insertDraft(int repositoryId, int draftAccountId, int issueId, String draftText, String draftType, String commentId) { Draft draft = new Draft(); draft.setDraftRepositoryId(repositoryId); @@ -35,6 +35,7 @@ public class DraftsApi { draft.setIssueId(issueId); draft.setDraftText(draftText); draft.setDraftType(draftType); + draft.setCommentId(draftType); return insertDraftAsyncTask(draft); } @@ -71,11 +72,11 @@ public class DraftsApi { return draftId; } - public Integer checkDraft(int issueId, int draftRepositoryId) { + public Integer checkDraft(int issueId, int draftRepositoryId, String commentId) { try { - Thread thread = new Thread(() -> checkDraftFlag = draftsDao.checkDraftDao(issueId, draftRepositoryId)); + Thread thread = new Thread(() -> checkDraftFlag = draftsDao.checkDraftDao(issueId, draftRepositoryId, commentId)); thread.start(); thread.join(); } @@ -112,14 +113,14 @@ public class DraftsApi { new Thread(() -> draftsDao.deleteAllDrafts(accountId)).start(); } - public static void updateDraft(final String draftText, final int draftId) { + public static void updateDraft(final String draftText, final int draftId, final String commentId) { - new Thread(() -> draftsDao.updateDraft(draftText, draftId)).start(); + new Thread(() -> draftsDao.updateDraft(draftText, draftId, commentId)).start(); } - public static void updateDraftByIssueIdAsyncTask(final String draftText, final int issueId, final int draftRepositoryId) { + public static void updateDraftByIssueIdAsyncTask(final String draftText, final int issueId, final int draftRepositoryId, final String commentId) { - new Thread(() -> draftsDao.updateDraftByIssueId(draftText, issueId, draftRepositoryId)).start(); + new Thread(() -> draftsDao.updateDraftByIssueId(draftText, issueId, draftRepositoryId, commentId)).start(); } } diff --git a/app/src/main/java/org/mian/gitnex/database/dao/DraftsDao.java b/app/src/main/java/org/mian/gitnex/database/dao/DraftsDao.java index 79286822..2411c273 100644 --- a/app/src/main/java/org/mian/gitnex/database/dao/DraftsDao.java +++ b/app/src/main/java/org/mian/gitnex/database/dao/DraftsDao.java @@ -35,14 +35,14 @@ public interface DraftsDao { @Query("SELECT * FROM Drafts WHERE issueId = :issueId") LiveData fetchDraftByIssueId(int issueId); - @Query("SELECT count(draftId) FROM Drafts WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId") - Integer checkDraftDao(int issueId, int draftRepositoryId); + @Query("SELECT count(draftId) FROM Drafts WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId AND commentId = :commentId") + Integer checkDraftDao(int issueId, int draftRepositoryId, String commentId); - @Query("UPDATE Drafts SET draftText= :draftText WHERE draftId = :draftId") - void updateDraft(String draftText, int draftId); + @Query("UPDATE Drafts SET draftText = :draftText, commentId = :commentId WHERE draftId = :draftId") + void updateDraft(String draftText, int draftId, String commentId); - @Query("UPDATE Drafts SET draftText= :draftText WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId") - void updateDraftByIssueId(String draftText, int issueId, int draftRepositoryId); + @Query("UPDATE Drafts SET draftText = :draftText WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId AND commentId = :commentId") + void updateDraftByIssueId(String draftText, int issueId, int draftRepositoryId, String commentId); @Query("SELECT draftId FROM Drafts WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId") Integer getDraftId(int issueId, int draftRepositoryId); diff --git a/app/src/main/java/org/mian/gitnex/database/db/GitnexDatabase.java b/app/src/main/java/org/mian/gitnex/database/db/GitnexDatabase.java index b200a34c..4dfb1634 100644 --- a/app/src/main/java/org/mian/gitnex/database/db/GitnexDatabase.java +++ b/app/src/main/java/org/mian/gitnex/database/db/GitnexDatabase.java @@ -19,7 +19,7 @@ import org.mian.gitnex.database.models.UserAccount; */ @Database(entities = {Draft.class, Repository.class, UserAccount.class}, - version = 1, exportSchema = false) + version = 2, exportSchema = false) public abstract class GitnexDatabase extends RoomDatabase { private static GitnexDatabase gitnexDatabase; @@ -30,7 +30,7 @@ public abstract class GitnexDatabase extends RoomDatabase { String DB_NAME = "gitnex"; gitnexDatabase = Room.databaseBuilder(context, GitnexDatabase.class, DB_NAME) //.fallbackToDestructiveMigration() - //.addMigrations(MIGRATION_1_2) + .addMigrations(MIGRATION_1_2) .build(); } @@ -48,7 +48,7 @@ public abstract class GitnexDatabase extends RoomDatabase { public void migrate(@NonNull SupportSQLiteDatabase database) { //database.execSQL("DROP TABLE Drafts"); - //database.execSQL("ALTER TABLE 'Drafts' ADD COLUMN 'draftType' TEXT"); + database.execSQL("ALTER TABLE 'Drafts' ADD COLUMN 'commentId' TEXT"); } }; diff --git a/app/src/main/java/org/mian/gitnex/database/models/Draft.java b/app/src/main/java/org/mian/gitnex/database/models/Draft.java index 1257abb2..aaf36af9 100644 --- a/app/src/main/java/org/mian/gitnex/database/models/Draft.java +++ b/app/src/main/java/org/mian/gitnex/database/models/Draft.java @@ -24,6 +24,8 @@ public class Draft implements Serializable { private String draftText; @Nullable private String draftType; + @Nullable + private String commentId; public int getDraftId() { @@ -86,4 +88,15 @@ public class Draft implements Serializable { this.draftType = draftType; } + @Nullable + public String getCommentId() { + + return commentId; + } + + public void setCommentId(@Nullable String commentId) { + + this.commentId = commentId; + } + } diff --git a/app/src/main/java/org/mian/gitnex/database/models/DraftWithRepository.java b/app/src/main/java/org/mian/gitnex/database/models/DraftWithRepository.java index 7048e423..3509edb7 100644 --- a/app/src/main/java/org/mian/gitnex/database/models/DraftWithRepository.java +++ b/app/src/main/java/org/mian/gitnex/database/models/DraftWithRepository.java @@ -18,6 +18,7 @@ public class DraftWithRepository { private int issueId; private String draftText; private String draftType; + private String commentId; public int getRepositoryId() { @@ -119,4 +120,14 @@ public class DraftWithRepository { this.draftType = draftType; } + public String getCommentId() { + + return commentId; + } + + public void setCommentId(String commentId) { + + this.commentId = commentId; + } + } diff --git a/app/src/main/res/layout/list_drafts.xml b/app/src/main/res/layout/list_drafts.xml index afefa62d..95cad5b6 100644 --- a/app/src/main/res/layout/list_drafts.xml +++ b/app/src/main/res/layout/list_drafts.xml @@ -48,12 +48,19 @@ android:layout_width="wrap_content" android:visibility="gone" /> + + + android:layout_marginBottom="10dp" + tools:ignore="UseCompoundDrawables"> + + + + + + + + - - -