Skip to content

Commit

Permalink
Merge branch 'release/4.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
CDRussell committed Jan 25, 2018
2 parents 4d0d3ce + 33a4b59 commit f0d5566
Show file tree
Hide file tree
Showing 27 changed files with 659 additions and 208 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'kotlin-kapt'
apply from: '../versioning.gradle'

ext {
VERSION_NAME = "4.0.11"
VERSION_NAME = "4.1.0"
}

android {
Expand Down Expand Up @@ -102,8 +102,8 @@ dependencies {
implementation "com.jakewharton.rxrelay2:rxrelay:2.0.0"

// Anko
compile "org.jetbrains.anko:anko-commons:$ankoVersion"
compile "org.jetbrains.anko:anko-design:$ankoVersion"
implementation "org.jetbrains.anko:anko-commons:$ankoVersion"
implementation "org.jetbrains.anko:anko-design:$ankoVersion"

// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$architectureComponents"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import com.duckduckgo.app.global.db.AppDatabase
import com.duckduckgo.app.migration.legacy.LegacyDb
import com.duckduckgo.app.migration.legacy.LegacyDbContracts
import org.junit.After
import org.junit.Assert.*
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test

class LegacyMigrationTest {
Expand All @@ -39,7 +40,7 @@ class LegacyMigrationTest {
val urlConverter = QueryUrlConverter(DuckDuckGoRequestRewriter(DuckDuckGoUrlDetector()))

var appDatabase: AppDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()
var bookmarksDao = MockBookmarksDao()
var bookmarksDao = StubBookmarksDao()

@After
fun after() {
Expand Down Expand Up @@ -123,10 +124,13 @@ class LegacyMigrationTest {
return values
}

class MockBookmarksDao(): BookmarksDao {

class StubBookmarksDao : BookmarksDao {
var bookmarks = mutableListOf<BookmarkEntity>()

override fun update(bookmarkEntity: BookmarkEntity) {
throw UnsupportedOperationException()
}

override fun insert(bookmark: BookmarkEntity) {
bookmarks.add(bookmark)
}
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@

<data android:mimeType="text/plain" />
</intent-filter>

<!-- Allow app to be default assistant -->
<intent-filter>
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

</activity>
<activity
android:name=".BrowserActivity"
Expand Down Expand Up @@ -101,4 +108,4 @@
</receiver>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey

@Entity(tableName = "bookmarks")
data class BookmarkEntity(@PrimaryKey(autoGenerate = true) var id: Int = 0, var title: String?, var url: String)
data class BookmarkEntity(@PrimaryKey(autoGenerate = true) var id: Int = 0,
var title: String?,
var url: String)
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
package com.duckduckgo.app.bookmarks.db

import android.arch.lifecycle.LiveData
import android.arch.persistence.room.Dao
import android.arch.persistence.room.Delete
import android.arch.persistence.room.Insert
import android.arch.persistence.room.Query
import android.arch.persistence.room.*

@Dao
interface BookmarksDao {
Expand All @@ -34,4 +31,7 @@ interface BookmarksDao {
@Delete
fun delete(bookmark: BookmarkEntity)

@Update
fun update(bookmarkEntity: BookmarkEntity)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2018 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.bookmarks.ui

import android.app.Dialog
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AlertDialog
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import com.duckduckgo.app.bookmarks.db.BookmarkEntity
import com.duckduckgo.app.browser.R
import com.duckduckgo.app.global.view.showKeyboard
import org.jetbrains.anko.find


class BookmarkAddEditDialogFragment : DialogFragment() {

interface BookmarkDialogCreationListener {
fun userWantsToCreateBookmark(title: String, url: String)
}

interface BookmarkDialogEditListener {
fun userWantsToEditBookmark(id: Int, title: String, url: String)
}


override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

val rootView = View.inflate(activity, R.layout.add_or_edit_bookmark, null)
val titleInput = rootView.find<EditText>(R.id.titleInput)
val urlInput = rootView.find<EditText>(R.id.urlInput)

val alertBuilder = AlertDialog.Builder(activity!!)
.setView(rootView)
.setPositiveButton(R.string.bookmarkSave) { _, _ ->
userAcceptedDialog(titleInput, urlInput)
}

validateBundleArguments()

setAlertTitle(alertBuilder)
populateFields(titleInput, urlInput)

val alert = alertBuilder.create()
showKeyboard(titleInput, alert)
return alert
}

private fun userAcceptedDialog(titleInput: EditText, urlInput: EditText) {
if (isInEditMode()) {
val listener = activity as BookmarkDialogEditListener
listener.userWantsToEditBookmark(
getExistingId(),
titleInput.text.toString(),
urlInput.text.toString()
)
} else {
val listener = activity as BookmarkDialogCreationListener
listener.userWantsToCreateBookmark(
titleInput.text.toString(),
urlInput.text.toString()
)
}
}

private fun showKeyboard(titleInput: EditText, alert: AlertDialog) {
titleInput.setSelection(titleInput.text.length)
titleInput.showKeyboard()
alert.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
}

private fun populateFields(titleInput: EditText, urlInput: EditText) {
titleInput.setText(getExistingTitle())
urlInput.setText(getExistingUrl())
}

private fun setAlertTitle(alertBuilder: AlertDialog.Builder) {
val titleStringRes = if (isInEditMode()) {
R.string.bookmarkTitleEdit
} else {
R.string.bookmarkTitleSave
}
alertBuilder.setTitle(titleStringRes)
}

private fun getExistingId(): Int = arguments!!.getInt(KEY_BOOKMARK_ID)
private fun getExistingTitle(): String = arguments!!.getString(KEY_PREEXISTING_TITLE)
private fun getExistingUrl(): String = arguments!!.getString(KEY_PREEXISTING_URL)

private fun validateBundleArguments() {
if (arguments == null) throw IllegalArgumentException("Missing arguments bundle")
val args = arguments!!
if (!args.containsKey(KEY_IS_EDIT_MODE) ||
!args.containsKey(KEY_PREEXISTING_TITLE) ||
!args.containsKey(KEY_PREEXISTING_URL)) {
throw IllegalArgumentException("Bundle arguments required [KEY_IS_EDIT_MODE, KEY_PREEXISTING_TITLE, KEY_PREEXISTING_URL]")
}
}

private fun isInEditMode(): Boolean = arguments!!.getBoolean(KEY_IS_EDIT_MODE)

companion object {
private const val KEY_IS_EDIT_MODE = "KEY_IS_EDIT_MODE"
private const val KEY_BOOKMARK_ID = "KEY_BOOKMARK_ID"
private const val KEY_PREEXISTING_TITLE = "KEY_PREEXISTING_TITLE"
private const val KEY_PREEXISTING_URL = "KEY_PREEXISTING_URL"

fun createDialogEditingMode(bookmark: BookmarkEntity): BookmarkAddEditDialogFragment {

val dialog = BookmarkAddEditDialogFragment()
val bundle = Bundle()

bundle.putBoolean(KEY_IS_EDIT_MODE, true)
bundle.putInt(KEY_BOOKMARK_ID, bookmark.id)
bundle.putString(KEY_PREEXISTING_TITLE, bookmark.title)
bundle.putString(KEY_PREEXISTING_URL, bookmark.url)

dialog.arguments = bundle
return dialog
}

fun createDialogCreationMode(
existingTitle: String?,
existingUrl: String?
): BookmarkAddEditDialogFragment {

val dialog = BookmarkAddEditDialogFragment()
val bundle = Bundle()

bundle.putBoolean(KEY_IS_EDIT_MODE, false)
bundle.putString(KEY_PREEXISTING_TITLE, existingTitle)
bundle.putString(KEY_PREEXISTING_URL, existingUrl)

dialog.arguments = bundle
return dialog
}
}

}
Loading

0 comments on commit f0d5566

Please sign in to comment.