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 <mmarif@swatian.com>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/628
This commit is contained in:
M M Arif 2020-08-18 16:11:31 +02:00
parent dcacf1f141
commit 76137c56bc
9 changed files with 143 additions and 49 deletions

View File

@ -7,6 +7,7 @@ import com.google.gson.JsonElement;
import org.mian.gitnex.R; import org.mian.gitnex.R;
import org.mian.gitnex.activities.ReplyToIssueActivity; import org.mian.gitnex.activities.ReplyToIssueActivity;
import org.mian.gitnex.clients.RetrofitClient; import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.database.api.DraftsApi;
import org.mian.gitnex.helpers.AlertDialogs; import org.mian.gitnex.helpers.AlertDialogs;
import org.mian.gitnex.helpers.Authorization; import org.mian.gitnex.helpers.Authorization;
import org.mian.gitnex.helpers.TinyDB; import org.mian.gitnex.helpers.TinyDB;
@ -22,7 +23,7 @@ import retrofit2.Callback;
public class IssueActions { 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 TinyDB tinyDb = new TinyDB(ctx);
final String instanceUrl = tinyDb.getString("instanceUrl"); final String instanceUrl = tinyDb.getString("instanceUrl");
@ -47,7 +48,11 @@ public class IssueActions {
if(response.code() == 200) { if(response.code() == 200) {
tinyDb.putBoolean("commentEdited", true); 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(); ((ReplyToIssueActivity) ctx).finish();
} }

View File

@ -33,6 +33,7 @@ import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.models.Collaborators; import org.mian.gitnex.models.Collaborators;
import org.mian.gitnex.models.Issues; import org.mian.gitnex.models.Issues;
import java.util.List; import java.util.List;
import java.util.Objects;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
@ -54,7 +55,7 @@ public class ReplyToIssueActivity extends BaseActivity {
private ArrayAdapter<Mention> defaultMentionAdapter; private ArrayAdapter<Mention> defaultMentionAdapter;
private Button replyButton; private Button replyButton;
private String TAG = StaticGlobalVariables.replyToIssueActivity; private String TAG = StaticGlobalVariables.replyToIssueActivity;
private long draftId; private long draftIdOnCreate;
@Override @Override
protected int getLayoutResourceId(){ protected int getLayoutResourceId(){
@ -97,6 +98,15 @@ public class ReplyToIssueActivity extends BaseActivity {
initCloseListener(); initCloseListener();
closeActivity.setOnClickListener(onClickListener); closeActivity.setOnClickListener(onClickListener);
if(getIntent().getStringExtra("draftId") != null) {
draftIdOnCreate = Long.parseLong(Objects.requireNonNull(getIntent().getStringExtra("draftId")));
}
else {
draftIdOnCreate = returnDraftId();
}
replyButton = findViewById(R.id.replyButton); replyButton = findViewById(R.id.replyButton);
if(getIntent().getStringExtra("commentBody") != null) { 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"); 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) { 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); draftSaved.setVisibility(View.VISIBLE);
} }
@ -145,7 +155,7 @@ public class ReplyToIssueActivity extends BaseActivity {
disableProcessButton(); disableProcessButton();
assert commentId != null; 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) { 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); 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()); TinyDB tinyDb = new TinyDB(getApplicationContext());
@ -195,17 +205,31 @@ public class ReplyToIssueActivity extends BaseActivity {
DraftsApi draftsApi = new DraftsApi(appCtx); DraftsApi draftsApi = new DraftsApi(appCtx);
int countDraft = draftsApi.checkDraft(issueNumber, repositoryId); if(draftIdOnCreate == 0) {
if(countDraft == 0) { draftsApi.insertDraft(repositoryId, currentActiveAccountId, issueNumber, draftText, StaticGlobalVariables.draftTypeComment, commentId);
draftId = draftsApi.insertDraft(repositoryId, currentActiveAccountId, issueNumber, draftText, StaticGlobalVariables.draftTypeComment);
} }
else { 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() { public void loadCollaboratorsList() {
final TinyDB tinyDb = new TinyDB(appCtx); final TinyDB tinyDb = new TinyDB(appCtx);
@ -325,12 +349,8 @@ public class ReplyToIssueActivity extends BaseActivity {
// delete draft comment // delete draft comment
if(tinyDb.getBoolean("draftsCommentsDeletionEnabled")) { if(tinyDb.getBoolean("draftsCommentsDeletionEnabled")) {
int repositoryId = (int) tinyDb.getLong("repositoryId", 0);
int issueNumber = Integer.parseInt(tinyDb.getString("issueNumber"));
DraftsApi draftsApi = new DraftsApi(appCtx); DraftsApi draftsApi = new DraftsApi(appCtx);
draftId = draftsApi.getDraftIdAsync(issueNumber, repositoryId); draftsApi.deleteSingleDraft((int) draftIdOnCreate);
draftsApi.deleteSingleDraft((int) draftId);
} }
finish(); finish();

View File

@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.text.Html; import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -38,6 +39,8 @@ public class DraftsAdapter extends RecyclerView.Adapter<DraftsAdapter.DraftsView
private TextView issueType; private TextView issueType;
private TextView repoOwner; private TextView repoOwner;
private TextView repoName; private TextView repoName;
private TextView commentId;
private ImageView editCommentStatus;
private DraftsViewHolder(View itemView) { private DraftsViewHolder(View itemView) {
@ -51,7 +54,9 @@ public class DraftsAdapter extends RecyclerView.Adapter<DraftsAdapter.DraftsView
issueType = itemView.findViewById(R.id.issueType); issueType = itemView.findViewById(R.id.issueType);
repoOwner = itemView.findViewById(R.id.repoOwner); repoOwner = itemView.findViewById(R.id.repoOwner);
repoName = itemView.findViewById(R.id.repoName); repoName = itemView.findViewById(R.id.repoName);
commentId = itemView.findViewById(R.id.commentId);
ImageView deleteDraft = itemView.findViewById(R.id.deleteDraft); ImageView deleteDraft = itemView.findViewById(R.id.deleteDraft);
editCommentStatus = itemView.findViewById(R.id.editCommentStatus);
deleteDraft.setOnClickListener(itemDelete -> { deleteDraft.setOnClickListener(itemDelete -> {
@ -69,6 +74,12 @@ public class DraftsAdapter extends RecyclerView.Adapter<DraftsAdapter.DraftsView
intent.putExtra("issueNumber", issueNumber.getText().toString()); intent.putExtra("issueNumber", issueNumber.getText().toString());
intent.putExtra("repositoryId", repoId.getText().toString()); intent.putExtra("repositoryId", repoId.getText().toString());
intent.putExtra("draftTitle", repoInfo.getText().toString()); intent.putExtra("draftTitle", repoInfo.getText().toString());
intent.putExtra("commentId", commentId.getText().toString());
intent.putExtra("draftId", draftId.getText().toString());
if(!commentId.getText().toString().equalsIgnoreCase("")) {
intent.putExtra("commentAction", "edit");
}
TinyDB tinyDb = new TinyDB(mCtx); TinyDB tinyDb = new TinyDB(mCtx);
tinyDb.putString("issueNumber", issueNumber.getText().toString()); tinyDb.putString("issueNumber", issueNumber.getText().toString());
@ -117,9 +128,18 @@ public class DraftsAdapter extends RecyclerView.Adapter<DraftsAdapter.DraftsView
holder.repoOwner.setText(currentItem.getRepositoryOwner()); holder.repoOwner.setText(currentItem.getRepositoryOwner());
holder.repoName.setText(currentItem.getRepositoryName()); holder.repoName.setText(currentItem.getRepositoryName());
holder.draftText.setText(currentItem.getDraftText()); holder.draftText.setText(currentItem.getDraftText());
holder.commentId.setText(currentItem.getCommentId());
String issueNumber = "<font color='" + mCtx.getResources().getColor(R.color.lightGray) + "'>" + mCtx.getResources().getString(R.string.hash) + currentItem.getIssueId() + "</font>"; String issueNumber = "<font color='" + mCtx.getResources().getColor(R.color.lightGray) + "'>" + mCtx.getResources().getString(R.string.hash) + currentItem.getIssueId() + "</font>";
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);
}
} }

View File

@ -27,7 +27,7 @@ public class DraftsApi {
draftsDao = db.draftsDao(); 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 draft = new Draft();
draft.setDraftRepositoryId(repositoryId); draft.setDraftRepositoryId(repositoryId);
@ -35,6 +35,7 @@ public class DraftsApi {
draft.setIssueId(issueId); draft.setIssueId(issueId);
draft.setDraftText(draftText); draft.setDraftText(draftText);
draft.setDraftType(draftType); draft.setDraftType(draftType);
draft.setCommentId(draftType);
return insertDraftAsyncTask(draft); return insertDraftAsyncTask(draft);
} }
@ -71,11 +72,11 @@ public class DraftsApi {
return draftId; return draftId;
} }
public Integer checkDraft(int issueId, int draftRepositoryId) { public Integer checkDraft(int issueId, int draftRepositoryId, String commentId) {
try { try {
Thread thread = new Thread(() -> checkDraftFlag = draftsDao.checkDraftDao(issueId, draftRepositoryId)); Thread thread = new Thread(() -> checkDraftFlag = draftsDao.checkDraftDao(issueId, draftRepositoryId, commentId));
thread.start(); thread.start();
thread.join(); thread.join();
} }
@ -112,14 +113,14 @@ public class DraftsApi {
new Thread(() -> draftsDao.deleteAllDrafts(accountId)).start(); 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();
} }
} }

View File

@ -35,14 +35,14 @@ public interface DraftsDao {
@Query("SELECT * FROM Drafts WHERE issueId = :issueId") @Query("SELECT * FROM Drafts WHERE issueId = :issueId")
LiveData<Draft> fetchDraftByIssueId(int issueId); LiveData<Draft> fetchDraftByIssueId(int issueId);
@Query("SELECT count(draftId) FROM Drafts WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId") @Query("SELECT count(draftId) FROM Drafts WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId AND commentId = :commentId")
Integer checkDraftDao(int issueId, int draftRepositoryId); Integer checkDraftDao(int issueId, int draftRepositoryId, String commentId);
@Query("UPDATE Drafts SET draftText= :draftText WHERE draftId = :draftId") @Query("UPDATE Drafts SET draftText = :draftText, commentId = :commentId WHERE draftId = :draftId")
void updateDraft(String draftText, int draftId); void updateDraft(String draftText, int draftId, String commentId);
@Query("UPDATE Drafts SET draftText= :draftText WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId") @Query("UPDATE Drafts SET draftText = :draftText WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId AND commentId = :commentId")
void updateDraftByIssueId(String draftText, int issueId, int draftRepositoryId); void updateDraftByIssueId(String draftText, int issueId, int draftRepositoryId, String commentId);
@Query("SELECT draftId FROM Drafts WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId") @Query("SELECT draftId FROM Drafts WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId")
Integer getDraftId(int issueId, int draftRepositoryId); Integer getDraftId(int issueId, int draftRepositoryId);

View File

@ -19,7 +19,7 @@ import org.mian.gitnex.database.models.UserAccount;
*/ */
@Database(entities = {Draft.class, Repository.class, UserAccount.class}, @Database(entities = {Draft.class, Repository.class, UserAccount.class},
version = 1, exportSchema = false) version = 2, exportSchema = false)
public abstract class GitnexDatabase extends RoomDatabase { public abstract class GitnexDatabase extends RoomDatabase {
private static GitnexDatabase gitnexDatabase; private static GitnexDatabase gitnexDatabase;
@ -30,7 +30,7 @@ public abstract class GitnexDatabase extends RoomDatabase {
String DB_NAME = "gitnex"; String DB_NAME = "gitnex";
gitnexDatabase = Room.databaseBuilder(context, GitnexDatabase.class, DB_NAME) gitnexDatabase = Room.databaseBuilder(context, GitnexDatabase.class, DB_NAME)
//.fallbackToDestructiveMigration() //.fallbackToDestructiveMigration()
//.addMigrations(MIGRATION_1_2) .addMigrations(MIGRATION_1_2)
.build(); .build();
} }
@ -48,7 +48,7 @@ public abstract class GitnexDatabase extends RoomDatabase {
public void migrate(@NonNull SupportSQLiteDatabase database) { public void migrate(@NonNull SupportSQLiteDatabase database) {
//database.execSQL("DROP TABLE Drafts"); //database.execSQL("DROP TABLE Drafts");
//database.execSQL("ALTER TABLE 'Drafts' ADD COLUMN 'draftType' TEXT"); database.execSQL("ALTER TABLE 'Drafts' ADD COLUMN 'commentId' TEXT");
} }
}; };

View File

@ -24,6 +24,8 @@ public class Draft implements Serializable {
private String draftText; private String draftText;
@Nullable @Nullable
private String draftType; private String draftType;
@Nullable
private String commentId;
public int getDraftId() { public int getDraftId() {
@ -86,4 +88,15 @@ public class Draft implements Serializable {
this.draftType = draftType; this.draftType = draftType;
} }
@Nullable
public String getCommentId() {
return commentId;
}
public void setCommentId(@Nullable String commentId) {
this.commentId = commentId;
}
} }

View File

@ -18,6 +18,7 @@ public class DraftWithRepository {
private int issueId; private int issueId;
private String draftText; private String draftText;
private String draftType; private String draftType;
private String commentId;
public int getRepositoryId() { public int getRepositoryId() {
@ -119,4 +120,14 @@ public class DraftWithRepository {
this.draftType = draftType; this.draftType = draftType;
} }
public String getCommentId() {
return commentId;
}
public void setCommentId(String commentId) {
this.commentId = commentId;
}
} }

View File

@ -48,12 +48,19 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:visibility="gone" /> android:visibility="gone" />
<TextView
android:id="@+id/commentId"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone" />
<LinearLayout <LinearLayout
android:id="@+id/frameDraftInfo" android:id="@+id/frameDraftInfo"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal"
android:layout_marginBottom="10dp"> android:layout_marginBottom="10dp"
tools:ignore="UseCompoundDrawables">
<TextView <TextView
android:id="@+id/repoInfo" android:id="@+id/repoInfo"
@ -65,24 +72,41 @@
android:textSize="16sp" /> android:textSize="16sp" />
<ImageView <ImageView
android:id="@+id/deleteDraft" android:id="@+id/editCommentStatus"
android:layout_weight=".05" android:layout_width="wrap_content"
android:layout_width="10dp" android:layout_height="wrap_content"
android:layout_height="20dp" android:src="@drawable/ic_edit"
android:layout_gravity="end" android:layout_gravity="end"
android:contentDescription="@string/menuDeleteText" />
</LinearLayout>
<LinearLayout
android:id="@+id/frameDraftInfo1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp"
tools:ignore="UseCompoundDrawables">
<TextView
android:id="@+id/draftText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".95"
android:layout_marginBottom="0dp"
android:textColor="?attr/primaryTextColor"
android:textSize="12sp"
android:text="@string/noDataFound" />
<ImageView
android:id="@+id/deleteDraft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="@drawable/ic_delete" android:src="@drawable/ic_delete"
android:contentDescription="@string/menuDeleteText" /> android:contentDescription="@string/menuDeleteText" />
</LinearLayout> </LinearLayout>
<TextView
android:id="@+id/draftText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:textColor="?attr/primaryTextColor"
android:textSize="12sp"
android:text="@string/noDataFound" />
</LinearLayout> </LinearLayout>