@@ -50,19 +50,33 @@ import androidx.ui.text.style.TextDecoration
50
50
import androidx.ui.text.style.TextIndent
51
51
import com.example.jetnews.R
52
52
53
+ private const val defaultBodyLineHeight = 1.49f
54
+ private val defaultSpacerSize = 16 .dp
55
+
56
+ private val codeFontFamily = FontFamily (font = Font (name = " fira_code_regular.ttf" ))
57
+
58
+ private val italicTextStyle = (+ themeTextStyle { body1 }).copy(fontStyle = FontStyle .Italic )
59
+ private val boldTextStyle = (+ themeTextStyle { body1 }).copy(fontWeight = FontWeight .bold)
60
+ private val codeTextStyle = (+ themeTextStyle { body1 })
61
+ .copy(background = Color .LightGray , fontFamily = codeFontFamily)
62
+ private val linkTextStyle = (+ themeTextStyle { body1 }).copy(decoration = TextDecoration .Underline )
63
+
64
+ private val bulletParagraphStyle = ParagraphStyle (textIndent = TextIndent (firstLine = Px (30f )))
65
+ private val metadataTextStyle = + themeTextStyle { caption.copy(color = Color .Gray ) }
66
+
53
67
@Composable
54
68
fun PostContent (post : Post ) {
55
69
VerticalScroller {
56
70
Padding (left = defaultSpacerSize, right = defaultSpacerSize) {
57
71
Column (crossAxisAlignment = CrossAxisAlignment .Start ) {
58
72
HeightSpacer (height = defaultSpacerSize)
59
- Text (text = post.title, style = titleTextSyle )
73
+ Text (text = post.title, style = + themeTextStyle { h4 } )
60
74
HeightSpacer (height = defaultSpacerSize)
61
- post.subtitle?.let {
75
+ post.subtitle?.let { subtitle ->
62
76
Text (
63
- text = it ,
64
- style = subtitleTextStyle ,
65
- paragraphStyle = subtitleParagraphStyle
77
+ text = subtitle ,
78
+ style = + themeTextStyle { subtitle1 } ,
79
+ paragraphStyle = ParagraphStyle (lineHeight = 1.49f )
66
80
)
67
81
HeightSpacer (height = defaultSpacerSize)
68
82
}
@@ -76,12 +90,12 @@ fun PostContent(post: Post) {
76
90
77
91
@Composable
78
92
fun PostMetadata (metadata : Metadata ) {
79
- val text =
93
+ val postMetadata =
80
94
" ${metadata.author.name} on ${metadata.date} - ${metadata.readTimeMinutes} min read"
81
95
Row {
82
96
DefaultAuthorImage ()
83
97
WidthSpacer (width = 8 .dp)
84
- Text (text = text , style = metadataTextStyle)
98
+ Text (text = postMetadata , style = metadataTextStyle)
85
99
}
86
100
}
87
101
@@ -100,34 +114,15 @@ fun DefaultAuthorImage() {
100
114
@Composable
101
115
fun PostContents (paragraphs : List <Paragraph >) {
102
116
paragraphs.forEach {
103
- HeightSpacer (height = 4 .dp)
104
- Paragraph (paragraph = it)
105
- HeightSpacer (height = 4 .dp)
106
- }
107
- }
108
-
109
- private fun getTextAndParagraphStyle (paragraph : Paragraph ): Pair <TextStyle , ParagraphStyle > {
110
- var textStyle: TextStyle = bodyTextStyle
111
- var paragraphStyle = ParagraphStyle ()
112
-
113
- when (paragraph.type) {
114
- is ParagraphType .Caption -> textStyle = bodyTextStyle
115
- ParagraphType .Title -> textStyle = h3TextStyle
116
- ParagraphType .Subtitle -> textStyle = subtitleTextStyle
117
- ParagraphType .Text -> {
118
- textStyle = bodyTextStyle
119
- paragraphStyle = paragraphStyle.copy(lineHeight = defaultBodyLineHeight)
117
+ Padding (top = 4 .dp, bottom = 4 .dp) {
118
+ Paragraph (paragraph = it)
120
119
}
121
- ParagraphType .CodeBlock -> textStyle = codeTextStyle
122
- ParagraphType .Quote -> textStyle = bodyTextStyle
123
- ParagraphType .Bullet -> paragraphStyle = bulletParagraphStyle
124
120
}
125
- return Pair (textStyle, paragraphStyle)
126
121
}
127
122
128
123
@Composable
129
124
fun Paragraph (paragraph : Paragraph ) {
130
- val (textStyle, paragraphStyle) = getTextAndParagraphStyle(paragraph )
125
+ val (textStyle, paragraphStyle) = paragraph.type. getTextAndParagraphStyle()
131
126
132
127
val annotatedString = paragraphToAnnotatedString(paragraph)
133
128
when (paragraph.type) {
@@ -170,35 +165,35 @@ fun BulletParagraph(text: AnnotatedString, textStyle: TextStyle, paragraphStyle:
170
165
}
171
166
}
172
167
173
- fun paragraphToAnnotatedString (paragraph : Paragraph ): AnnotatedString {
174
- val styles = paragraph.markups.map {
175
- when (it.type) {
176
- is MarkupType .Italic -> AnnotatedString .Item (italicTextStyle, it.start, it.end)
177
- is MarkupType .Link -> AnnotatedString .Item (linkTextStyle, it.start, it.end)
178
- is MarkupType .Bold -> AnnotatedString .Item (boldTextStyle, it.start, it.end)
179
- is MarkupType .Code -> AnnotatedString .Item (codeTextStyle, it.start, it.end)
168
+ private fun ParagraphType.getTextAndParagraphStyle (): Pair <TextStyle , ParagraphStyle > {
169
+ var textStyle: TextStyle = + themeTextStyle { body1 }
170
+ var paragraphStyle = ParagraphStyle ()
171
+
172
+ when (this ) {
173
+ is ParagraphType .Caption -> textStyle = + themeTextStyle { body1 }
174
+ ParagraphType .Title -> textStyle = + themeTextStyle { h4 }
175
+ ParagraphType .Subtitle -> textStyle = + themeTextStyle { subtitle1 }
176
+ ParagraphType .Text -> {
177
+ textStyle = + themeTextStyle { body1 }
178
+ paragraphStyle = paragraphStyle.copy(lineHeight = defaultBodyLineHeight)
180
179
}
180
+ ParagraphType .CodeBlock -> textStyle = codeTextStyle
181
+ ParagraphType .Quote -> textStyle = + themeTextStyle { body1 }
182
+ ParagraphType .Bullet -> paragraphStyle = bulletParagraphStyle
181
183
}
182
- return AnnotatedString (text = paragraph.text, textStyles = styles)
184
+ return textStyle to paragraphStyle
183
185
}
184
186
185
- const val defaultBodyLineHeight = 1.49f
186
- val defaultSpacerSize = 16 .dp
187
-
188
- val codeFontFamily = FontFamily (font = Font (name = " fira_code_regular.ttf" ))
189
-
190
- val titleTextSyle = + themeTextStyle { h4 }
191
-
192
- val subtitleTextStyle = + themeTextStyle { subtitle1 }
193
- val subtitleParagraphStyle = ParagraphStyle (lineHeight = 1.49f )
194
- val bodyTextStyle = + themeTextStyle { body1 }
195
- val h3TextStyle = + themeTextStyle { h4 }
196
-
197
- val italicTextStyle = (+ themeTextStyle { body1 }).copy(fontStyle = FontStyle .Italic )
198
- val boldTextStyle = (+ themeTextStyle { body1 }).copy(fontWeight = FontWeight .bold)
199
- val codeTextStyle = (+ themeTextStyle { body1 })
200
- .copy(background = Color .LightGray , fontFamily = codeFontFamily)
201
- val linkTextStyle = (+ themeTextStyle { body1 }).copy(decoration = TextDecoration .Underline )
187
+ private fun paragraphToAnnotatedString (paragraph : Paragraph ): AnnotatedString {
188
+ val styles = paragraph.markups.map { it.toAnnotatedStringItem() }
189
+ return AnnotatedString (text = paragraph.text, textStyles = styles)
190
+ }
202
191
203
- val bulletParagraphStyle = ParagraphStyle (textIndent = TextIndent (firstLine = Px (30f )))
204
- val metadataTextStyle = + themeTextStyle { caption.copy(color = Color .Gray ) }
192
+ private fun Markup.toAnnotatedStringItem (): AnnotatedString .Item <TextStyle > {
193
+ return when (this .type) {
194
+ is MarkupType .Italic -> AnnotatedString .Item (italicTextStyle, start, end)
195
+ is MarkupType .Link -> AnnotatedString .Item (linkTextStyle, start, end)
196
+ is MarkupType .Bold -> AnnotatedString .Item (boldTextStyle, start, end)
197
+ is MarkupType .Code -> AnnotatedString .Item (codeTextStyle, start, end)
198
+ }
199
+ }
0 commit comments