1 package org.coolreader.crengine;
2 
3 import android.view.LayoutInflater;
4 import android.view.View;
5 import android.widget.EditText;
6 import android.widget.RadioButton;
7 import android.widget.TextView;
8 
9 import org.coolreader.CoolReader;
10 import org.coolreader.R;
11 
12 public class BookmarkEditDialog extends BaseDialog {
13 
14 	private final CoolReader mCoolReader;
15 	private final LayoutInflater mInflater;
16 	private final ReaderView mReaderView;
17 	private final Bookmark mOriginalBookmark;
18 	private final Bookmark mBookmark;
19 	private final boolean mIsNew;
20 	final EditText commentEdit;
21 
BookmarkEditDialog( CoolReader activity, ReaderView readerView, Bookmark bookmark, boolean isNew)22 	public BookmarkEditDialog( CoolReader activity, ReaderView readerView, Bookmark bookmark, boolean isNew)
23 	{
24 		super(activity, "", true, false);
25 		mCoolReader = activity;
26 		mReaderView = readerView;
27 		mIsNew = isNew;
28 		mOriginalBookmark = bookmark;
29 		//if ( !isNew )
30 			mBookmark = new Bookmark(bookmark);
31 		//else
32 		//	mBookmark = bookmark;
33 		if (!isNew) {
34 			setThirdButtonImage(Utils.resolveResourceIdByAttr(activity, R.attr.cr3_button_remove_drawable, R.drawable.cr3_button_remove),
35 					R.string.mi_bookmark_delete);
36 		}
37 		boolean isComment = bookmark.getType()==Bookmark.TYPE_COMMENT;
38 		setTitle(mCoolReader.getString( mIsNew ? R.string.dlg_bookmark_create : R.string.dlg_bookmark_edit));
39 		mInflater = LayoutInflater.from(getContext());
40 		View view = mInflater.inflate(R.layout.bookmark_edit_dialog, null);
41 		final RadioButton btnComment = view.findViewById(R.id.rb_comment);
42 		final RadioButton btnCorrection = view.findViewById(R.id.rb_correction);
43 		final TextView posLabel = view.findViewById(R.id.lbl_position);
44 		final TextView commentLabel = view.findViewById(R.id.lbl_comment_text);
45 		final EditText posEdit = view.findViewById(R.id.position_text);
46 		commentEdit = view.findViewById(R.id.comment_edit);
47 		String postext = mBookmark.getPercent()/100 + "%";
48 		if ( mBookmark.getTitleText()!=null )
49 			postext = postext + "  " + mBookmark.getTitleText();
50 		posLabel.setText(postext);
51 		commentLabel.setText(isComment ? R.string.dlg_bookmark_edit_comment : R.string.dlg_bookmark_edit_correction);
52 		posEdit.setText(mBookmark.getPosText());
53 		commentEdit.setText(bookmark.getCommentText());
54 		if ( isNew ) {
55 			btnComment.setChecked(isComment);
56 			btnCorrection.setChecked(!isComment);
57 			btnComment.setOnCheckedChangeListener((buttonView, isChecked) -> {
58 				if ( isChecked ) {
59 					mBookmark.setType(Bookmark.TYPE_COMMENT);
60 					commentLabel.setText(R.string.dlg_bookmark_edit_comment); // : R.string.dlg_bookmark_edit_correction
61 				}
62 			});
63 			btnCorrection.setOnCheckedChangeListener((buttonView, isChecked) -> {
64 				if ( isChecked ) {
65 					mBookmark.setType(Bookmark.TYPE_CORRECTION);
66 					commentLabel.setText(R.string.dlg_bookmark_edit_correction);
67 					String oldText = commentEdit.getText().toString();
68 					if ( oldText==null || oldText.length()==0 )
69 						commentEdit.setText(mBookmark.getPosText());
70 				}
71 			});
72 		} else {
73 			btnComment.setClickable(false);
74 			btnCorrection.setClickable(false);
75 		}
76 		setView( view );
77 	}
78 
79 	@Override
onPositiveButtonClick()80 	protected void onPositiveButtonClick() {
81 		if ( mIsNew ) {
82 			mBookmark.setCommentText( commentEdit.getText().toString() );
83 			mReaderView.addBookmark(mBookmark);
84 		} else {
85 			if ( mOriginalBookmark.setCommentText(commentEdit.getText().toString()) ) {
86 				mOriginalBookmark.setTimeStamp(System.currentTimeMillis());
87 				mReaderView.updateBookmark(mOriginalBookmark);
88 			}
89 		}
90 		super.onPositiveButtonClick();
91 	}
92 
93 	@Override
onNegativeButtonClick()94 	protected void onNegativeButtonClick() {
95 		super.onNegativeButtonClick();
96 	}
97 
98 	@Override
onThirdButtonClick()99 	protected void onThirdButtonClick() {
100 		mCoolReader.askConfirmation(R.string.win_title_confirm_bookmark_delete, () -> {
101 			mReaderView.removeBookmark(mBookmark);
102 			onNegativeButtonClick();
103 		});
104 	}
105 }
106