Skip to content

Commit

Permalink
Improve AboutViewModel markdown processing
Browse files Browse the repository at this point in the history
  • Loading branch information
keyboardsurfer authored and florina-muntenescu committed Jul 20, 2018
1 parent 345e681 commit 1576435
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 115 deletions.
1 change: 0 additions & 1 deletion about/src/main/java/io/plaidapp/ui/about/AboutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.plaidapp.ui.about

import android.arch.lifecycle.ViewModelProviders
import android.net.Uri
import android.os.Bundle
import android.support.customtabs.CustomTabsIntent
Expand Down
21 changes: 9 additions & 12 deletions about/src/main/java/io/plaidapp/ui/about/AboutStyler.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
/*
* Copyright 2018 Google LLC
* Copyright 2018 Google, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. The ASF licenses this
* file to you 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
* 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
* 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.
* 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 io.plaidapp.ui.about
Expand Down
172 changes: 99 additions & 73 deletions about/src/main/java/io/plaidapp/ui/about/AboutViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ import android.text.Spanned
import android.text.style.AlignmentSpan
import io.plaidapp.about.R
import io.plaidapp.core.util.event.Event
import io.plaidapp.R as appR

/**
* [AndroidViewModel] for the about module.
*/
class AboutViewModel(
private val aboutStyler: AboutStyler,
private val resources: Resources
internal class AboutViewModel(
private val aboutStyler: AboutStyler,
private val resources: Resources
) : ViewModel() {

private val markdown = Bypass(resources.displayMetrics, Bypass.Options())
Expand All @@ -47,88 +46,115 @@ class AboutViewModel(
val navigationTarget: LiveData<Event<String>>
get() = _navigationTarget

val appAboutText: CharSequence
get() {
return with(aboutStyler) {
// fun with spans & markdown
val about0 = getSpannableFromMarkdown(R.string.about_plaid_0,
linksColor,
highlightColor)
val appAboutText: CharSequence by lazy {
with(aboutStyler) {
// fun with spans & markdown
val about0 = getSpannableFromMarkdown(
R.string.about_plaid_0,
linksColor,
highlightColor
)

val about1 = SpannableString(resources.getString(R.string.about_plaid_1)).apply {
setSpan(AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
val about1 = SpannableString(resources.getString(R.string.about_plaid_1)).apply {
setSpan(
AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}

val about2 = SpannableString(
getSpannableFromMarkdown(R.string.about_plaid_2,
linksColor, highlightColor)).apply {
setSpan(AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
val about3 = SpannableString(
getSpannableFromMarkdown(R.string.about_plaid_3,
linksColor, highlightColor)).apply {
setSpan(AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
"$about0\n\n$about1\n$about2\n\n$about3"
val about2 = SpannableString(
getSpannableFromMarkdown(
R.string.about_plaid_2,
linksColor, highlightColor
)
).apply {
setSpan(
AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
val about3 = SpannableString(
getSpannableFromMarkdown(
R.string.about_plaid_3,
linksColor, highlightColor
)
).apply {
setSpan(
AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
"$about0\n\n$about1\n$about2\n\n$about3"
}
}

val iconAboutText: CharSequence
get() {
val icon0 = resources.getString(R.string.about_icon_0)
return with(aboutStyler) {
val iconAboutText: CharSequence by lazy {
val icon0 = resources.getString(R.string.about_icon_0)
with(aboutStyler) {
val icon1 = getSpannableFromMarkdown(R.string.about_icon_1, linksColor, highlightColor)
"$icon0\n$icon1"
}
}
}

val libraries = listOf(
Library(
"Android support libraries",
"The Android support libraries offer a number of features that are " +
"not built into the framework.",
"https://developer.android.com/topic/libraries/support-library",
"https://developer.android.com/images/android_icon_125.png",
false
),
Library(
"Bypass",
"Skip the HTML, Bypass takes markdown and renders it directly.",
"https://github.com/Uncodin/bypass",
"https://avatars.githubusercontent.com/u/1072254",
true
),
Library(
"Glide",
"An image loading and caching library for Android focused onsmooth " +
"scrolling.",
"https://github.com/bumptech/glide",
"https://avatars.githubusercontent.com/u/423539",
false
),
Library(
"JSoup",
"Java HTML Parser, with best of DOM, CSS, and jquery.",
"https://github.com/jhy/jsoup/",
"https://avatars.githubusercontent.com/u/76934",
true
),
Library(
"OkHttp",
"An HTTP & HTTP/2 client for Android and Java applications.",
"http://square.github.io/okhttp/",
"https://avatars.githubusercontent.com/u/82592",
false
),
Library(
"Retrofit",
"A type-safe HTTP client for Android and Java.",
"http://square.github.io/retrofit/",
"https://avatars.githubusercontent.com/u/82592",
false
)
)

internal fun onLibraryClick(library: Library) {
fun onLibraryClick(library: Library) {
_navigationTarget.value = Event(library.link)
}

internal val libraries = listOf(
Library("Android support libraries",
"The Android support libraries offer a number of features that are " +
"not built into the framework.",
"https://developer.android.com/topic/libraries/support-library",
"https://developer.android.com/images/android_icon_125.png",
false),
Library("Bypass",
"Skip the HTML, Bypass takes markdown and renders it directly.",
"https://github.com/Uncodin/bypass",
"https://avatars.githubusercontent.com/u/1072254",
true),
Library("Glide",
"An image loading and caching library for Android focused onsmooth " +
"scrolling.",
"https://github.com/bumptech/glide",
"https://avatars.githubusercontent.com/u/423539",
false),
Library("JSoup",
"Java HTML Parser, with best of DOM, CSS, and jquery.",
"https://github.com/jhy/jsoup/",
"https://avatars.githubusercontent.com/u/76934",
true),
Library("OkHttp",
"An HTTP & HTTP/2 client for Android and Java applications.",
"http://square.github.io/okhttp/",
"https://avatars.githubusercontent.com/u/82592",
false),
Library("Retrofit",
"A type-safe HTTP client for Android and Java.",
"http://square.github.io/retrofit/",
"https://avatars.githubusercontent.com/u/82592",
false))

private fun getSpannableFromMarkdown(
@StringRes stringId: Int,
linksColor: ColorStateList,
@ColorInt highlightColor: Int
@StringRes stringId: Int,
linksColor: ColorStateList,
@ColorInt highlightColor: Int
): CharSequence {
return markdown.markdownToSpannable(resources.getString(stringId), linksColor,
highlightColor, null)
return markdown.markdownToSpannable(
resources.getString(stringId), linksColor,
highlightColor, null
)
}
}
2 changes: 1 addition & 1 deletion about/src/main/java/io/plaidapp/ui/about/LibraryAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import java.security.InvalidParameterException
* Adapter that holds libraries.
*/
internal class LibraryAdapter(
private val uiModel: LibrariesUiModel
private val uiModel: LibrariesUiModel
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
Expand Down
28 changes: 13 additions & 15 deletions about/src/main/java/io/plaidapp/ui/about/uimodel/AboutUiModel.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
/*
* Copyright 2018 Google LLC
* Copyright 2018 Google, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. The ASF licenses this
* file to you 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
* 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
* 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.
* 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 io.plaidapp.ui.about.uimodel
Expand All @@ -23,6 +20,7 @@ package io.plaidapp.ui.about.uimodel
* Hold values displayed in the about Ui.
*/
internal data class AboutUiModel(
val appAboutText: CharSequence,
val iconAboutText: CharSequence,
val librariesUiModel: LibrariesUiModel)
val appAboutText: CharSequence,
val iconAboutText: CharSequence,
val librariesUiModel: LibrariesUiModel
)
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
/*
* Copyright 2018 Google LLC
* Copyright 2018 Google, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. The ASF licenses this
* file to you 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
* 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
* 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.
* 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 io.plaidapp.ui.about.uimodel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ interface Markdown {
content: String,
linksColor: ColorStateList,
@ColorInt highlightColor: Int,
callback: LoadImageCallback
callback: LoadImageCallback?
): CharSequence
}

0 comments on commit 1576435

Please sign in to comment.