Skip to content

Commit

Permalink
Feature/bookmark favicons (#216)
Browse files Browse the repository at this point in the history
* Add glide to project

* Load favicon image from bookmarks adapter

* Restore tools:text value

* Code formatting

* Reduce adapter row margin
  • Loading branch information
CDRussell committed Mar 9, 2018
1 parent 10ce192 commit 03f99c4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Expand Up @@ -79,6 +79,7 @@ ext {
dagger = "2.14.1"
retrofit = "2.3.0"
ankoVersion = "0.10.4"
glide = "4.6.1"
}


Expand Down Expand Up @@ -127,6 +128,10 @@ dependencies {
kaptAndroidTest "com.google.dagger:dagger-android-processor:$dagger"
kaptAndroidTest "com.google.dagger:dagger-compiler:$dagger"

// Glide
implementation "com.github.bumptech.glide:glide:$glide"
kapt "com.github.bumptech.glide:compiler:$glide"

testImplementation "org.mockito:mockito-core:2.15.0"
testImplementation "com.nhaarman:mockito-kotlin-kt1.1:1.5.0"
testImplementation "junit:junit:4.12"
Expand Down
Expand Up @@ -37,6 +37,8 @@ import com.duckduckgo.app.browser.R
import com.duckduckgo.app.global.DuckDuckGoActivity
import com.duckduckgo.app.global.ViewModelFactory
import com.duckduckgo.app.global.baseHost
import com.duckduckgo.app.global.faviconLocation
import com.duckduckgo.app.global.image.GlideApp
import com.duckduckgo.app.global.view.gone
import com.duckduckgo.app.global.view.show
import kotlinx.android.synthetic.main.content_bookmarks.*
Expand Down Expand Up @@ -146,15 +148,15 @@ class BookmarksActivity : DuckDuckGoActivity() {
private const val EDIT_BOOKMARK_FRAGMENT_TAG = "EDIT_BOOKMARK"
}

class BookmarksAdapter(val context: Context, val viewModel: BookmarksViewModel) : Adapter<BookmarksViewHolder>() {
class BookmarksAdapter(private val context: Context,
private val viewModel: BookmarksViewModel) : Adapter<BookmarksViewHolder>() {

var bookmarks: List<BookmarkEntity> = emptyList()
set(value) {
field = value
notifyDataSetChanged()
}


override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): BookmarksViewHolder {
val inflater = LayoutInflater.from(context)
val view = inflater.inflate(R.layout.view_bookmark_entry, parent, false)
Expand All @@ -168,10 +170,9 @@ class BookmarksActivity : DuckDuckGoActivity() {
override fun getItemCount(): Int {
return bookmarks.size
}

}

class BookmarksViewHolder(itemView: View?, val viewModel: BookmarksViewModel) :
class BookmarksViewHolder(itemView: View?, private val viewModel: BookmarksViewModel) :
ViewHolder(itemView) {

lateinit var bookmark: BookmarkEntity
Expand All @@ -186,6 +187,7 @@ class BookmarksActivity : DuckDuckGoActivity() {

itemView.title.text = bookmark.title
itemView.url.text = parseDisplayUrl(bookmark.url)
loadFavicon(bookmark.url)

itemView.overflowMenu.setOnClickListener {
showOverFlowMenu(itemView.overflowMenu, bookmark)
Expand All @@ -196,6 +198,16 @@ class BookmarksActivity : DuckDuckGoActivity() {
}
}

private fun loadFavicon(url: String) {
val faviconUrl = Uri.parse(url).faviconLocation()

GlideApp.with(itemView)
.load(faviconUrl)
.placeholder(R.drawable.ic_globe_white_24dp)
.error(R.drawable.ic_globe_white_24dp)
.into(itemView.favicon)
}

private fun parseDisplayUrl(urlString: String): String {
val uri = Uri.parse(urlString)
return uri.baseHost ?: return urlString
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/duckduckgo/app/global/UriExtension.kt
Expand Up @@ -56,3 +56,11 @@ fun Uri.toDesktopUri(): Uri {
this
}
}

private const val faviconBaseUrlFormat = "https://icons.duckduckgo.com/ip3/%s.ico"

fun Uri?.faviconLocation(): Uri? {
val host = this?.host
if (host.isNullOrBlank()) return null
return Uri.parse(String.format(faviconBaseUrlFormat, host))
}
@@ -0,0 +1,24 @@
/*
* 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.global.image

import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule


@GlideModule
class GlobalGlideModule : AppGlideModule()
18 changes: 15 additions & 3 deletions app/src/main/res/layout/view_bookmark_entry.xml
Expand Up @@ -22,10 +22,22 @@
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_marginTop="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
>

<ImageView
android:id="@+id/favicon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="22dp"
android:importantForAccessibility="no"
android:src="@drawable/ic_globe_white_24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>

<TextView
android:id="@+id/title"
android:layout_width="0dp"
Expand All @@ -39,7 +51,7 @@
app:layout_constraintBottom_toTopOf="@+id/url"
app:layout_constraintEnd_toStartOf="@+id/overflowMenu"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toEndOf="@id/favicon"
app:layout_constraintTop_toTopOf="parent"
tools:text="Bookmark" />

Expand All @@ -50,13 +62,13 @@
android:fontFamily="sans-serif"
android:layout_marginStart="22dp"
android:paddingTop="2dp"
app:layout_constraintStart_toEndOf="@id/favicon"
android:paddingBottom="4dp"
android:textColor="@color/warmGray"
android:textSize="12sp"
android:textStyle="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/overflowMenu"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title"
tools:text="Bookmark" />

Expand Down

0 comments on commit 03f99c4

Please sign in to comment.