1+ /*
2+ * Copyright (c) 2018 DuckDuckGo
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com.duckduckgo.app.bookmarks.ui
18+
19+ import android.app.Dialog
20+ import android.os.Bundle
21+ import android.support.v4.app.DialogFragment
22+ import android.support.v7.app.AlertDialog
23+ import android.view.View
24+ import android.view.WindowManager
25+ import android.widget.EditText
26+ import com.duckduckgo.app.bookmarks.db.BookmarkEntity
27+ import com.duckduckgo.app.browser.R
28+ import com.duckduckgo.app.global.view.showKeyboard
29+ import org.jetbrains.anko.find
30+
31+
32+ class BookmarkAddEditDialogFragment : DialogFragment () {
33+
34+ interface BookmarkDialogCreationListener {
35+ fun userWantsToCreateBookmark (title : String , url : String )
36+ }
37+
38+ interface BookmarkDialogEditListener {
39+ fun userWantsToEditBookmark (id : Int , title : String , url : String )
40+ }
41+
42+
43+ override fun onCreateDialog (savedInstanceState : Bundle ? ): Dialog {
44+
45+ val rootView = View .inflate(activity, R .layout.add_or_edit_bookmark, null )
46+ val titleInput = rootView.find<EditText >(R .id.titleInput)
47+ val urlInput = rootView.find<EditText >(R .id.urlInput)
48+
49+ val alertBuilder = AlertDialog .Builder (activity!! )
50+ .setView(rootView)
51+ .setPositiveButton(R .string.bookmarkSave) { _, _ ->
52+ userAcceptedDialog(titleInput, urlInput)
53+ }
54+
55+ validateBundleArguments()
56+
57+ setAlertTitle(alertBuilder)
58+ populateFields(titleInput, urlInput)
59+
60+ val alert = alertBuilder.create()
61+ showKeyboard(titleInput, alert)
62+ return alert
63+ }
64+
65+ private fun userAcceptedDialog (titleInput : EditText , urlInput : EditText ) {
66+ if (isInEditMode()) {
67+ val listener = activity as BookmarkDialogEditListener
68+ listener.userWantsToEditBookmark(
69+ getExistingId(),
70+ titleInput.text.toString(),
71+ urlInput.text.toString()
72+ )
73+ } else {
74+ val listener = activity as BookmarkDialogCreationListener
75+ listener.userWantsToCreateBookmark(
76+ titleInput.text.toString(),
77+ urlInput.text.toString()
78+ )
79+ }
80+ }
81+
82+ private fun showKeyboard (titleInput : EditText , alert : AlertDialog ) {
83+ titleInput.setSelection(titleInput.text.length)
84+ titleInput.showKeyboard()
85+ alert.window.setSoftInputMode(WindowManager .LayoutParams .SOFT_INPUT_STATE_VISIBLE )
86+ }
87+
88+ private fun populateFields (titleInput : EditText , urlInput : EditText ) {
89+ titleInput.setText(getExistingTitle())
90+ urlInput.setText(getExistingUrl())
91+ }
92+
93+ private fun setAlertTitle (alertBuilder : AlertDialog .Builder ) {
94+ val titleStringRes = if (isInEditMode()) {
95+ R .string.bookmarkTitleEdit
96+ } else {
97+ R .string.bookmarkTitleSave
98+ }
99+ alertBuilder.setTitle(titleStringRes)
100+ }
101+
102+ private fun getExistingId (): Int = arguments!! .getInt(KEY_BOOKMARK_ID )
103+ private fun getExistingTitle (): String = arguments!! .getString(KEY_PREEXISTING_TITLE )
104+ private fun getExistingUrl (): String = arguments!! .getString(KEY_PREEXISTING_URL )
105+
106+ private fun validateBundleArguments () {
107+ if (arguments == null ) throw IllegalArgumentException (" Missing arguments bundle" )
108+ val args = arguments!!
109+ if (! args.containsKey(KEY_IS_EDIT_MODE ) ||
110+ ! args.containsKey(KEY_PREEXISTING_TITLE ) ||
111+ ! args.containsKey(KEY_PREEXISTING_URL )) {
112+ throw IllegalArgumentException (" Bundle arguments required [KEY_IS_EDIT_MODE, KEY_PREEXISTING_TITLE, KEY_PREEXISTING_URL]" )
113+ }
114+ }
115+
116+ private fun isInEditMode (): Boolean = arguments!! .getBoolean(KEY_IS_EDIT_MODE )
117+
118+ companion object {
119+ private const val KEY_IS_EDIT_MODE = " KEY_IS_EDIT_MODE"
120+ private const val KEY_BOOKMARK_ID = " KEY_BOOKMARK_ID"
121+ private const val KEY_PREEXISTING_TITLE = " KEY_PREEXISTING_TITLE"
122+ private const val KEY_PREEXISTING_URL = " KEY_PREEXISTING_URL"
123+
124+ fun createDialogEditingMode (bookmark : BookmarkEntity ): BookmarkAddEditDialogFragment {
125+
126+ val dialog = BookmarkAddEditDialogFragment ()
127+ val bundle = Bundle ()
128+
129+ bundle.putBoolean(KEY_IS_EDIT_MODE , true )
130+ bundle.putInt(KEY_BOOKMARK_ID , bookmark.id)
131+ bundle.putString(KEY_PREEXISTING_TITLE , bookmark.title)
132+ bundle.putString(KEY_PREEXISTING_URL , bookmark.url)
133+
134+ dialog.arguments = bundle
135+ return dialog
136+ }
137+
138+ fun createDialogCreationMode (
139+ existingTitle : String? ,
140+ existingUrl : String?
141+ ): BookmarkAddEditDialogFragment {
142+
143+ val dialog = BookmarkAddEditDialogFragment ()
144+ val bundle = Bundle ()
145+
146+ bundle.putBoolean(KEY_IS_EDIT_MODE , false )
147+ bundle.putString(KEY_PREEXISTING_TITLE , existingTitle)
148+ bundle.putString(KEY_PREEXISTING_URL , existingUrl)
149+
150+ dialog.arguments = bundle
151+ return dialog
152+ }
153+ }
154+
155+ }
0 commit comments