Skip to content

Commit

Permalink
Add option for rounded corners
Browse files Browse the repository at this point in the history
  • Loading branch information
NickM-27 committed Feb 28, 2021
1 parent d227873 commit 5f912c1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ open class LinkPreview : FrameLayout, View.OnClickListener {

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
bindViews(context)
bindAttrs(attrs, 0)
}

constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
Expand All @@ -69,6 +70,7 @@ open class LinkPreview : FrameLayout, View.OnClickListener {
defStyle
) {
bindViews(context)
bindAttrs(attrs, defStyle)
}

/**
Expand Down Expand Up @@ -110,6 +112,19 @@ open class LinkPreview : FrameLayout, View.OnClickListener {
GlobalScope.launch { linkMap = context.loadLinkMap() }
}

private fun bindAttrs(attrs: AttributeSet, defStyle: Int) {
context.theme.obtainStyledAttributes(attrs, R.styleable.LinkPreview, defStyle, 0).let { linkAttrs ->
try {
linkAttrs.getBoolean(R.styleable.LinkPreview_roundedCorners, false).let { useRoundedCorners ->
binding.previewImage.shapeAppearanceModel = binding.previewImage.shapeAppearanceModel.toBuilder().setAllCornerSizes(if (useRoundedCorners) 24f else 0f).build()
binding.background.shapeAppearanceModel = binding.background.shapeAppearanceModel.toBuilder().setAllCornerSizes(if (useRoundedCorners) 24f else 0f).build()
}
} finally {
linkAttrs.recycle()
}
}
}

/**
* Sets the actual text of the view handling multiple types of images including the link cache
*/
Expand Down
26 changes: 21 additions & 5 deletions linkpreview/src/main/res/layout/preview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

<data>

<import type="android.view.View" />

<variable
name="data"
type="com.nick.mowen.linkpreview.PreviewData" />
Expand All @@ -19,7 +21,7 @@
android:visibility="visible"
tools:visibility="visible">

<ImageView
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/preview_image"
android:layout_width="0dp"
android:layout_height="0dp"
Expand All @@ -33,28 +35,42 @@
app:layout_constraintTop_toTopOf="parent"
tools:src="@tools:sample/backgrounds/scenic" />

<TextView
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#59000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/preview_title" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/preview_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#59000000"
android:ellipsize="end"
android:maxLines="1"
android:paddingStart="8dp"
android:paddingTop="8dp"
android:paddingEnd="8dp"
android:text="@{data.title}"
android:textColor="@android:color/white"
android:textSize="12sp"
android:textStyle="bold"
android:visibility='@{data.title != "" ? View.VISIBLE : View.GONE }'
app:layout_constraintBottom_toTopOf="@+id/preview_text"
app:layout_constraintEnd_toEndOf="@+id/preview_image"
app:layout_constraintStart_toStartOf="@+id/preview_image"
tools:text="@tools:sample/lorem" />

<TextView
<com.google.android.material.textview.MaterialTextView
android:id="@+id/preview_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#59000000"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:text="@{data.baseUrl}"
android:textColor="@android:color/white"
android:textSize="12sp"
Expand Down
15 changes: 8 additions & 7 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
android:layout_height="wrap_content"
android:hint="@string/hint_link_search"
android:inputType="textUri"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/search"
app:layout_constraintEnd_toStartOf="@+id/search"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
Expand All @@ -24,18 +24,19 @@
android:onClick="tryText"
android:src="@drawable/ic_search"
app:layout_constraintBottom_toBottomOf="@+id/url"
app:layout_constraintLeft_toRightOf="@+id/url"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/url"
app:layout_constraintTop_toTopOf="parent" />

<com.nick.mowen.linkpreview.view.LinkPreview
android:id="@+id/preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/card"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/url" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/url"
app:roundedCorners="true" />

<com.nick.mowen.linkpreview.view.LinkCardView
android:id="@+id/card"
Expand Down

0 comments on commit 5f912c1

Please sign in to comment.