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 <mmarif@swatian.com>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/576
Reviewed-by: opyale <opyale@noreply.codeberg.org>
This commit is contained in:
M M Arif 2020-07-09 13:55:09 +02:00
parent 3d72d68e14
commit 9c48dec54d
11 changed files with 244 additions and 5 deletions

View File

@ -80,6 +80,7 @@
<activity android:name=".activities.SettingsTranslationActivity" />
<activity android:name=".activities.SettingsReportsActivity" />
<activity android:name=".activities.AddNewTeamMemberActivity" />
<activity android:name=".activities.SettingsDraftsActivity" />
</application>
</manifest>

View File

@ -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") + "/");

View File

@ -55,6 +55,7 @@ public class ReplyToIssueActivity extends BaseActivity {
private ArrayAdapter<Mention> 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();
}

View File

@ -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(); }
}

View File

@ -56,7 +56,8 @@ public class DraftsAdapter extends RecyclerView.Adapter<DraftsAdapter.DraftsView
int getDraftId = Integer.parseInt(draftId.getText().toString());
deleteDraft(getAdapterPosition());
DraftsApi.deleteSingleDraft(getDraftId);
DraftsApi draftsApi = new DraftsApi(mCtx);
draftsApi.deleteSingleDraft(getDraftId);
});

View File

@ -55,6 +55,22 @@ public class DraftsApi {
return draftId;
}
public long getDraftIdAsync(int issueId, int draftRepositoryId) {
try {
Thread thread = new Thread(() -> 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> draft = draftsDao.fetchDraftById(draftId);

View File

@ -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);

View File

@ -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)));

View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutReportingView"
android:orientation="vertical"
android:background="?attr/primaryBackgroundColor">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Widget.AppCompat.SearchView">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/primaryBackgroundColor"
android:theme="@style/AppTheme.AppBarOverlay"
tools:ignore="UnusedAttribute">
<ImageView
android:id="@+id/close"
android:layout_width="@dimen/close_button_size"
android:layout_height="@dimen/close_button_size"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:contentDescription="@string/close"
android:src="@drawable/ic_close" />
<TextView
android:id="@+id/toolbarTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/draftsHeader"
android:textColor="?attr/primaryTextColor"
android:maxLines="1"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<RelativeLayout
android:id="@+id/enableDraftsCommentsDeletion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="horizontal">
<TextView
android:id="@+id/enableCommentsDeletionHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginStart="44dp"
android:layout_marginEnd="24dp"
android:text="@string/settingsEnableCommentsDeletionText"
android:textColor="?attr/primaryTextColor" />
<Switch
android:id="@+id/commentsDeletionSwitch"
android:layout_toEndOf="@+id/enableCommentsDeletionHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="56dp"
android:layout_alignParentEnd="true"
android:paddingStart="0dp"
android:paddingEnd="25dp"
android:gravity="end"
android:layout_gravity="end"
android:layout_marginBottom="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/enableCommentsDeletionHeader"
android:layout_marginTop="5dp"
android:layout_marginStart="44dp"
android:layout_marginEnd="44dp"
android:text="@string/settingsEnableCommentsDeletionHintText"
android:textSize="12sp"
android:textColor="?attr/hintColor" />
</RelativeLayout>
</LinearLayout>

View File

@ -80,6 +80,38 @@
</LinearLayout>
<LinearLayout
android:id="@+id/draftsFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp">
<TextView
android:id="@+id/draftsHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_drafts_24dp"
android:drawablePadding="24dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:text="@string/draftsHeader"
android:textColor="?attr/primaryTextColor"
android:textSize="16sp" />
<TextView
android:id="@+id/draftsHintText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingStart="60dp"
android:paddingEnd="12dp"
android:text="@string/draftsHintText"
android:textColor="?attr/primaryTextColor"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/securityFrame"
android:layout_width="match_parent"

View File

@ -304,6 +304,10 @@
<string name="clearCacheSelectionSelectedText" translatable="false">0 B</string>
<string name="clearCacheDialogHeader">Clear Cache?</string>
<string name="clearCacheDialogMessage">This will delete all the cache data including files and images.\n\nProceed with deletion?</string>
<string name="draftsHeader">Drafts</string>
<string name="draftsHintText">Comments draft</string>
<string name="settingsEnableCommentsDeletionText">Enable Drafts Deletion</string>
<string name="settingsEnableCommentsDeletionHintText">Delete comment draft when comment is posted</string>
<!-- settings -->
<string name="noMoreData">No more data available</string>