Implement drafts, introduce Room persistence library for db #139

Open
mmarif wants to merge 25 commits from 15-comments-draft into master
6 changed files with 174 additions and 46 deletions
Showing only changes of commit 6e24bf57bb - Show all commits

View File

@ -16,6 +16,7 @@ import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
@ -327,7 +328,21 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
DraftsFragment frag = (DraftsFragment) fm.findFragmentById(R.id.fragment_container);
if(frag != null) {
frag.deleteAllDrafts(currentActiveAccountId);
new AlertDialog.Builder(ctx)
.setTitle(R.string.deleteAllDrafts)
.setIcon(R.drawable.ic_delete)
.setCancelable(false)
.setMessage(R.string.deleteAllDraftsDialogMessage)
.setPositiveButton(R.string.menuDeleteText, (dialog, which) -> {
frag.deleteAllDrafts(currentActiveAccountId);
dialog.dismiss();
})
.setNegativeButton(R.string.cancelButton, (dialog, which) -> dialog.dismiss())
.show();
}
else {
Toasty.error(ctx, getResources().getString(R.string.genericError));

View File

@ -7,6 +7,8 @@ import retrofit2.Response;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
@ -46,6 +48,7 @@ public class ReplyToIssueActivity extends BaseActivity {
private SocialAutoCompleteTextView addComment;
private ArrayAdapter<Mention> defaultMentionAdapter;
private Button replyButton;
private String TAG = StaticGlobalVariables.replyToIssueActivity;
@Override
protected int getLayoutResourceId(){
@ -86,26 +89,68 @@ public class ReplyToIssueActivity extends BaseActivity {
replyButton = findViewById(R.id.replyButton);
if(getIntent().getStringExtra("commentAction") != null && getIntent().getStringExtra("commentAction").equals("edit")) {
if(getIntent().getStringExtra("commentBody") != null) {
addComment.setText(getIntent().getStringExtra("commentBody"));
}
if(getIntent().getStringExtra("draftTitle") != null) {
toolbar_title.setText(getIntent().getStringExtra("draftTitle"));
}
if(getIntent().getStringExtra("commentAction") != null && getIntent().getStringExtra("commentAction").equals("edit")) {
final String commentId = getIntent().getStringExtra("commentId");
toolbar_title.setText(getResources().getString(R.string.editCommentTitle));
replyButton.setText(getResources().getString(R.string.editCommentButtonText));
replyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
disableProcessButton();
IssueActions.editIssueComment(ctx, Integer.parseInt(commentId), addComment.getText().toString());
addComment.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
saveDraft(addComment.getText().toString());
}
});
replyButton.setOnClickListener(v -> {
disableProcessButton();
IssueActions.editIssueComment(ctx, Integer.parseInt(commentId), addComment.getText().toString());
});
return;
}
addComment.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
saveDraft(addComment.getText().toString());
}
});
if(!connToInternet) {
disableProcessButton();
@ -118,6 +163,34 @@ public class ReplyToIssueActivity extends BaseActivity {
}
private void saveDraft(String draftText) {
TinyDB tinyDb = new TinyDB(getApplicationContext());
int repositoryId = (int) tinyDb.getLong("repositoryId", 0);
int currentActiveAccountId = tinyDb.getInt("currentActiveAccountId");
int issueNumber = Integer.parseInt(tinyDb.getString("issueNumber"));
DraftsRepository draftsRepository = new DraftsRepository(getApplicationContext());
try {
int countDraft = draftsRepository.checkDraft(issueNumber, repositoryId);
if(countDraft == 0) {
long draftId = draftsRepository.insertDraft(repositoryId, currentActiveAccountId, issueNumber, draftText, StaticGlobalVariables.draftTypeComment);
}
else {
DraftsRepository.updateDraftByIssueIdAsycTask(draftText, issueNumber, repositoryId);
}
}
catch(ExecutionException | InterruptedException e) {
Log.e(TAG, e.toString());
}
}
public void loadCollaboratorsList() {
final TinyDB tinyDb = new TinyDB(getApplicationContext());
@ -154,7 +227,7 @@ public class ReplyToIssueActivity extends BaseActivity {
} else {
Log.i("onResponse", String.valueOf(response.code()));
Log.i(TAG, String.valueOf(response.code()));
}
@ -162,7 +235,7 @@ public class ReplyToIssueActivity extends BaseActivity {
@Override
public void onFailure(@NonNull Call<List<Collaborators>> call, @NonNull Throwable t) {
Log.i("onFailure", t.getMessage());
Log.e(TAG, t.toString());
}
});
@ -204,28 +277,6 @@ public class ReplyToIssueActivity extends BaseActivity {
}
else {
int repositoryId = (int) tinyDb.getLong("repositoryId", 0);
int currentActiveAccountId = tinyDb.getInt("currentActiveAccountId");
int issueNumber = Integer.parseInt(tinyDb.getString("issueNumber"));
DraftsRepository draftsRepository = new DraftsRepository(getApplicationContext());
try {
int count = draftsRepository.checkDraft(issueNumber, repositoryId);
if(count == 0) {
long draftId = draftsRepository.insertDraft(repositoryId, currentActiveAccountId, issueNumber, newReplyDT, StaticGlobalVariables.draftTypeComment);
}
else {
DraftsRepository.updateDraftByIssueIdAsycTask(newReplyDT, issueNumber, repositoryId);
}
}
catch(ExecutionException | InterruptedException e) {
Log.e(StaticGlobalVariables.replyToIssueActivity, e.toString());
}
disableProcessButton();
replyComment(newReplyDT);
@ -287,7 +338,7 @@ public class ReplyToIssueActivity extends BaseActivity {
@Override
public void onFailure(@NonNull Call<Issues> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
Log.e(TAG, t.toString());
enableProcessButton();
}
});

View File

@ -2,6 +2,7 @@ package org.mian.gitnex.adapters;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -10,9 +11,11 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.mian.gitnex.R;
import org.mian.gitnex.activities.ReplyToIssueActivity;
import org.mian.gitnex.database.models.DraftsWithRepositories;
import org.mian.gitnex.database.repository.DraftsRepository;
import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.util.TinyDB;
import java.util.List;
/**
@ -28,7 +31,12 @@ public class DraftsAdapter extends RecyclerView.Adapter<DraftsAdapter.DraftsView
private TextView draftText;
private TextView repoInfo;
private TextView repoId;
private TextView draftId;
private TextView issueNumber;
private TextView issueType;
private TextView repoOwner;
private TextView repoName;
private DraftsViewHolder(View itemView) {
@ -36,7 +44,12 @@ public class DraftsAdapter extends RecyclerView.Adapter<DraftsAdapter.DraftsView
draftText = itemView.findViewById(R.id.draftText);
repoInfo = itemView.findViewById(R.id.repoInfo);
repoId = itemView.findViewById(R.id.repoId);
draftId = itemView.findViewById(R.id.draftId);
issueNumber = itemView.findViewById(R.id.issueNumber);
issueType = itemView.findViewById(R.id.issueType);
repoOwner = itemView.findViewById(R.id.repoOwner);
repoName = itemView.findViewById(R.id.repoName);
ImageView deleteDraft = itemView.findViewById(R.id.deleteDraft);
deleteDraft.setOnClickListener(itemDelete -> {
@ -47,6 +60,23 @@ public class DraftsAdapter extends RecyclerView.Adapter<DraftsAdapter.DraftsView
});
draftText.setOnClickListener(itemEdit -> {
Intent intent = new Intent(mCtx, ReplyToIssueActivity.class);
intent.putExtra("commentBody", draftText.getText());
intent.putExtra("issueNumber", issueNumber.getText());
intent.putExtra("repositoryId", repoId.getText());
intent.putExtra("draftTitle", repoInfo.getText());
TinyDB tinyDb = new TinyDB(mCtx);
tinyDb.putString("issueNumber", issueNumber.getText().toString());
tinyDb.putLong("repositoryId", Long.parseLong(repoId.getText().toString()));
//tinyDb.putString("issueType", issueType.getText().toString());
mCtx.startActivity(intent);
});
}
}
@ -78,7 +108,12 @@ public class DraftsAdapter extends RecyclerView.Adapter<DraftsAdapter.DraftsView
DraftsWithRepositories currentItem = draftsList.get(position);
holder.repoId.setText(String.valueOf(currentItem.getRepositoryId()));
holder.draftId.setText(String.valueOf(currentItem.getDraftId()));
holder.issueNumber.setText(String.valueOf(currentItem.getIssueId()));
holder.issueType.setText(currentItem.getDraftType());
holder.repoOwner.setText(currentItem.getRepositoryOwner());
holder.repoName.setText(currentItem.getRepositoryName());
holder.draftText.setText(currentItem.getDraftText());
holder.repoInfo.setText(String.format("%s/%s %s%d", currentItem.getRepositoryOwner(), currentItem.getRepositoryName(), mCtx.getResources().getString(R.string.hash), currentItem.getIssueId()));

View File

@ -9,7 +9,6 @@ import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import org.mian.gitnex.R;
import org.mian.gitnex.helpers.StaticGlobalVariables;
@ -32,20 +31,8 @@ public class BottomSheetDraftsFragment extends BottomSheetDialogFragment {
deleteAllDrafts.setOnClickListener(v1 -> {
Context ctx = v1.getContext();
dismiss();
new AlertDialog.Builder(ctx)
.setTitle(R.string.deleteAllDrafts)
.setIcon(R.drawable.ic_delete)
.setCancelable(false)
.setMessage(R.string.deleteAllDraftsDialogMessage)
.setPositiveButton(R.string.menuDeleteText, (dialog, which) -> {
bmListener.onButtonClicked("deleteDrafts");
dialog.dismiss();
})
.setNegativeButton(R.string.cancelButton, (dialog, which) -> dialog.dismiss())
.show();
bmListener.onButtonClicked("deleteDrafts");
});

View File

@ -36,6 +36,7 @@ public class DraftsFragment extends Fragment {
private DraftsRepository draftsRepository;
private TextView noData;
private List<DraftsWithRepositories> draftsList_;
private int currentActiveAccountId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -65,12 +66,13 @@ public class DraftsFragment extends Fragment {
swipeRefresh.setOnRefreshListener(() -> new Handler().postDelayed(() -> {
draftsList_.clear();
swipeRefresh.setRefreshing(false);
fetchDataAsync(1);
}, 250));
int currentActiveAccountId = tinyDb.getInt("currentActiveAccountId");
currentActiveAccountId = tinyDb.getInt("currentActiveAccountId");
fetchDataAsync(currentActiveAccountId);
@ -85,6 +87,7 @@ public class DraftsFragment extends Fragment {
assert drafts != null;
if(drafts.size() > 0) {
draftsList_.clear();
noData.setVisibility(View.GONE);
draftsList_.addAll(drafts);
adapter.notifyDataSetChanged();
@ -101,6 +104,13 @@ public class DraftsFragment extends Fragment {
}
@Override
public void onResume() {
super.onResume();
draftsList_.clear();
fetchDataAsync(currentActiveAccountId);
}
public void deleteAllDrafts(int accountId) {
if(draftsList_.size() > 0) {

View File

@ -12,12 +12,42 @@
android:background="?attr/primaryBackgroundColor"
tools:context=".activities.MainActivity">
<TextView
android:id="@+id/repoId"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/draftId"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/issueNumber"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/issueType"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/repoOwner"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/repoName"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone" />
<LinearLayout
android:id="@+id/frameDraftInfo"
android:layout_width="match_parent"