From 9c48dec54d53230b693baa5f770ba27ea6723776 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Thu, 9 Jul 2020 13:55:09 +0200 Subject: [PATCH] Delete drafts [settings] (#576) Merge branch '450-drafts-settings' of codeberg.org:gitnex/GitNex into 450-drafts-settings Fix package move Merge branch 'master' into 450-drafts-settings Merge branch 'master' into 450-drafts-settings fix default value save Delete drafts settings Co-authored-by: M M Arif Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/576 Reviewed-by: opyale --- app/src/main/AndroidManifest.xml | 3 +- .../mian/gitnex/activities/BaseActivity.java | 6 ++ .../activities/ReplyToIssueActivity.java | 17 +++- .../activities/SettingsDraftsActivity.java | 67 ++++++++++++++ .../mian/gitnex/adapters/DraftsAdapter.java | 3 +- .../mian/gitnex/database/api/DraftsApi.java | 18 +++- .../mian/gitnex/database/dao/DraftsDao.java | 3 + .../gitnex/fragments/SettingsFragment.java | 4 + .../res/layout/activity_settings_drafts.xml | 92 +++++++++++++++++++ app/src/main/res/layout/fragment_settings.xml | 32 +++++++ app/src/main/res/values/strings.xml | 4 + 11 files changed, 244 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/mian/gitnex/activities/SettingsDraftsActivity.java create mode 100644 app/src/main/res/layout/activity_settings_drafts.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 07d345b4..3fc4baac 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -80,6 +80,7 @@ + - \ No newline at end of file + diff --git a/app/src/main/java/org/mian/gitnex/activities/BaseActivity.java b/app/src/main/java/org/mian/gitnex/activities/BaseActivity.java index fe6cb0a9..5cc309f3 100644 --- a/app/src/main/java/org/mian/gitnex/activities/BaseActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/BaseActivity.java @@ -103,6 +103,12 @@ public abstract class BaseActivity extends AppCompatActivity { tinyDb.putString("cacheSizeImagesStr", getResources().getString(R.string.cacheSizeImagesSelectionSelectedText)); } + // enable comment drafts by default + if(tinyDb.getString("draftsCommentsDeletionEnabledInit").isEmpty()) { + tinyDb.putBoolean("draftsCommentsDeletionEnabled", true); + tinyDb.putString("draftsCommentsDeletionEnabledInit", "yes"); + } + if(!tinyDb.getString("instanceUrlWithProtocol").endsWith("/")) { tinyDb.putString("instanceUrlWithProtocol", tinyDb.getString("instanceUrlWithProtocol") + "/"); 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 b823ebf7..d4fe6744 100644 --- a/app/src/main/java/org/mian/gitnex/activities/ReplyToIssueActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/ReplyToIssueActivity.java @@ -55,6 +55,7 @@ public class ReplyToIssueActivity extends BaseActivity { private ArrayAdapter defaultMentionAdapter; private Button replyButton; private String TAG = StaticGlobalVariables.replyToIssueActivity; + private long draftId; @Override protected int getLayoutResourceId(){ @@ -193,12 +194,12 @@ public class ReplyToIssueActivity extends BaseActivity { int currentActiveAccountId = tinyDb.getInt("currentActiveAccountId"); int issueNumber = Integer.parseInt(tinyDb.getString("issueNumber")); - DraftsApi draftsApi = new DraftsApi(getApplicationContext()); + DraftsApi draftsApi = new DraftsApi(appCtx); int countDraft = draftsApi.checkDraft(issueNumber, repositoryId); if(countDraft == 0) { - long draftId = draftsApi.insertDraft(repositoryId, currentActiveAccountId, issueNumber, draftText, StaticGlobalVariables.draftTypeComment); + draftId = draftsApi.insertDraft(repositoryId, currentActiveAccountId, issueNumber, draftText, StaticGlobalVariables.draftTypeComment); } else { DraftsApi.updateDraftByIssueIdAsyncTask(draftText, issueNumber, repositoryId); @@ -321,6 +322,18 @@ public class ReplyToIssueActivity extends BaseActivity { tinyDb.putBoolean("commentPosted", true); tinyDb.putBoolean("resumeIssues", true); tinyDb.putBoolean("resumePullRequests", true); + + // 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); + } + finish(); } diff --git a/app/src/main/java/org/mian/gitnex/activities/SettingsDraftsActivity.java b/app/src/main/java/org/mian/gitnex/activities/SettingsDraftsActivity.java new file mode 100644 index 00000000..2e1dcfc0 --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/activities/SettingsDraftsActivity.java @@ -0,0 +1,67 @@ +package org.mian.gitnex.activities; + +import android.content.Context; +import android.os.Bundle; +import android.view.View; +import android.widget.ImageView; +import android.widget.Switch; +import org.mian.gitnex.R; +import org.mian.gitnex.helpers.Toasty; +import org.mian.gitnex.helpers.TinyDB; + +/** + * Author M M Arif + */ + +public class SettingsDraftsActivity extends BaseActivity { + + private Context appCtx; + private View.OnClickListener onClickListener; + + @Override + protected int getLayoutResourceId() { + + return R.layout.activity_settings_drafts; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + + super.onCreate(savedInstanceState); + appCtx = getApplicationContext(); + + TinyDB tinyDb = new TinyDB(appCtx); + + ImageView closeActivity = findViewById(R.id.close); + + initCloseListener(); + closeActivity.setOnClickListener(onClickListener); + + Switch commentsDeletionSwitch = findViewById(R.id.commentsDeletionSwitch); + + if(tinyDb.getBoolean("draftsCommentsDeletionEnabled")) { + commentsDeletionSwitch.setChecked(true); + } + else { + commentsDeletionSwitch.setChecked(false); + } + + // delete comments on submit switcher + commentsDeletionSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { + + if(isChecked) { + tinyDb.putBoolean("draftsCommentsDeletionEnabled", true); + Toasty.info(appCtx, getResources().getString(R.string.settingsSave)); + } + else { + tinyDb.putBoolean("draftsCommentsDeletionEnabled", false); + Toasty.info(appCtx, getResources().getString(R.string.settingsSave)); + } + + }); + + } + + private void initCloseListener() { onClickListener = view -> 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 cd4b973e..a828f839 100644 --- a/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java +++ b/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java @@ -56,7 +56,8 @@ public class DraftsAdapter extends RecyclerView.Adapter draftId = draftsDao.getDraftId(issueId, draftRepositoryId)); + thread.start(); + thread.join(); + } + catch(InterruptedException e) { + + Log.e(StaticGlobalVariables.draftsRepository, e.toString()); + } + + return draftId; + } + public Integer checkDraft(int issueId, int draftRepositoryId) { try { @@ -81,7 +97,7 @@ public class DraftsApi { return draftsDao.fetchDraftByIssueId(issueId); } - public static void deleteSingleDraft(final int draftId) { + public void deleteSingleDraft(final int draftId) { final LiveData draft = draftsDao.fetchDraftById(draftId); 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 f5c65aad..79286822 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 @@ -44,6 +44,9 @@ public interface DraftsDao { @Query("UPDATE Drafts SET draftText= :draftText WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId") void updateDraftByIssueId(String draftText, int issueId, int draftRepositoryId); + @Query("SELECT draftId FROM Drafts WHERE issueId = :issueId AND draftRepositoryId = :draftRepositoryId") + Integer getDraftId(int issueId, int draftRepositoryId); + @Query("DELETE FROM Drafts WHERE draftId = :draftId") void deleteByDraftId(int draftId); diff --git a/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java b/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java index eb2d68a9..2548857d 100644 --- a/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java +++ b/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java @@ -11,6 +11,7 @@ import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import org.mian.gitnex.R; import org.mian.gitnex.activities.SettingsAppearanceActivity; +import org.mian.gitnex.activities.SettingsDraftsActivity; import org.mian.gitnex.activities.SettingsFileViewerActivity; import org.mian.gitnex.activities.SettingsReportsActivity; import org.mian.gitnex.activities.SettingsSecurityActivity; @@ -32,6 +33,7 @@ public class SettingsFragment extends Fragment { LinearLayout appearanceFrame = v.findViewById(R.id.appearanceFrame); LinearLayout fileViewerFrame = v.findViewById(R.id.fileViewerFrame); + LinearLayout draftsFrame = v.findViewById(R.id.draftsFrame); LinearLayout securityFrame = v.findViewById(R.id.securityFrame); LinearLayout languagesFrame = v.findViewById(R.id.languagesFrame); LinearLayout reportsFrame = v.findViewById(R.id.reportsFrame); @@ -40,6 +42,8 @@ public class SettingsFragment extends Fragment { fileViewerFrame.setOnClickListener(v1 -> startActivity(new Intent(getContext(), SettingsFileViewerActivity.class))); + draftsFrame.setOnClickListener(v1 -> startActivity(new Intent(getContext(), SettingsDraftsActivity.class))); + securityFrame.setOnClickListener(v1 -> startActivity(new Intent(getContext(), SettingsSecurityActivity.class))); languagesFrame.setOnClickListener(v1 -> startActivity(new Intent(getContext(), SettingsTranslationActivity.class))); diff --git a/app/src/main/res/layout/activity_settings_drafts.xml b/app/src/main/res/layout/activity_settings_drafts.xml new file mode 100644 index 00000000..80994bcd --- /dev/null +++ b/app/src/main/res/layout/activity_settings_drafts.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index e5867d09..d6e5f46c 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -80,6 +80,38 @@ + + + + + + + + 0 B Clear Cache? This will delete all the cache data including files and images.\n\nProceed with deletion? + Drafts + Comments draft + Enable Drafts Deletion + Delete comment draft when comment is posted No more data available