@@ -62,9 +62,9 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
6262 extractLines(options.code).apply {
6363 if (! options.shortcut || size <= options.maxLines) // limit is not reached, show full
6464 lines = this
65- else slice(options.maxLines).apply {
66- lines = linesToShow() + options.shortcutNote.toUpperCase()
67- droppedLines = droppedLines()
65+ else slice(options.maxLines).let { (linesToShow, dropped) ->
66+ lines = linesToShow + options.shortcutNote.toUpperCase()
67+ droppedLines = dropped
6868 }
6969 }
7070 }
@@ -178,15 +178,15 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
178178 tvLineContent.typeface = options.font
179179
180180 val isLine = viewType == ViewHolderType .Line .viewType
181+
181182 options.format.apply {
182183 val height = if (isLine) lineHeight else borderHeight
183184 lineView.layoutParams.height = dpToPx(context, height)
184185 }
185- return if (isLine) {
186- val holder = LineViewHolder (lineView)
187- holder.setIsRecyclable(false )
188- holder
189- } else BorderViewHolder (lineView)
186+ return if (isLine)
187+ LineViewHolder (lineView).apply { setIsRecyclable(false ) }
188+ else
189+ BorderViewHolder (lineView)
190190 }
191191
192192 override fun onBindViewHolder (holder : ViewHolder , pos : Int ) {
@@ -207,9 +207,9 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
207207 // - Helpers (for view holder)
208208
209209 private fun bindClickListener (pos : Int , holder : ViewHolder ) {
210- options.lineClickListener?. let {
211- holder.itemView.setOnClickListener {
212- options.lineClickListener?. onCodeLineClicked(pos, lines[pos])
210+ holder.itemView.setOnClickListener {
211+ options.lineClickListener?. apply {
212+ onCodeLineClicked(pos, lines[pos])
213213 }
214214 }
215215 }
@@ -238,23 +238,21 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
238238 private fun displayFooter (pos : Int , holder : ViewHolder ) {
239239 val entityList = footerEntities[pos]
240240
241- holder.llLineFooter.removeAllViews()
241+ holder.llLineFooter.apply {
242+ removeAllViews()
242243
243- entityList?.let {
244- holder.llLineFooter. visibility = if (it. isNotEmpty()) View .VISIBLE else View .GONE
244+ entityList?.apply {
245+ visibility = if (isNotEmpty()) View .VISIBLE else View .GONE
245246
246- it. forEachIndexed { idx, entity ->
247- val footerView = createFooter(context, entity, idx == 0 )
248- holder.llLineFooter.addView(footerView)
247+ forEachIndexed { idx, entity ->
248+ addView( createFooter(context, entity, idx == 0 ) )
249+ }
249250 }
250251 }
251252 }
252253
253254 companion object {
254255 private const val MaxShortcutLines = 6
255-
256- private fun Pair <List <String >, List<String>>.linesToShow () = first
257- private fun Pair <List <String >, List<String>>.droppedLines () = second
258256 }
259257
260258 // - View holder types
@@ -266,7 +264,7 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
266264 const val LineStartIdx = 1
267265 const val BordersCount = 2
268266
269- fun Int.lineEndIdx () = this - BordersCount
267+ private fun Int.lineEndIdx () = this - BordersCount
270268
271269 fun get (pos : Int , n : Int ) = when (pos) {
272270 in LineStartIdx .. n.lineEndIdx() ->
@@ -336,114 +334,53 @@ data class Options(
336334
337335 internal var isHighlighted: Boolean = false
338336
339- fun withCode (code : String ): Options {
340- this .code = code
341- return this
342- }
343-
344- fun withCode (codeResId : Int ): Options {
345- this .code = context.getString(codeResId)
346- return this
347- }
348-
349- fun setCode (codeResId : Int ) {
350- withCode(codeResId)
351- }
352-
353- fun withLanguage (language : String ): Options {
354- this .language = language
355- return this
356- }
357-
358- fun withTheme (theme : ColorThemeData ): Options {
359- this .theme = theme
360- return this
361- }
362-
363- fun withTheme (theme : ColorTheme ): Options {
364- this .theme = theme.theme()
365- return this
366- }
337+ fun withCode (code : String ) = apply { this .code = code }
338+ fun withCode (codeResId : Int ) = apply { code = context.getString(codeResId) }
339+ fun setCode (codeResId : Int ) { withCode(codeResId) }
340+ fun withLanguage (language : String ) = apply { this .language = language }
367341
368- fun setTheme (theme : ColorTheme ) {
369- withTheme( theme)
370- }
342+ fun withTheme (theme : ColorThemeData ) = apply { this .theme = theme }
343+ fun withTheme ( theme : ColorTheme ) = apply { this . theme = theme.theme() }
344+ fun setTheme ( theme : ColorTheme ) { withTheme(theme) }
371345
372- fun withFont (font : Font ): Options {
373- this .font = FontCache .get(context).getTypeface(context, font)
374- return this
375- }
346+ fun withFont (font : Font ) = apply { this .font = font.get() }
347+ fun withFont (font : Typeface ) = font saveAndThen { apply { this .font = font } }
348+ fun withFont (fontPath : String ) = apply { this .font = fontPath.get() }
349+ fun setFont (fontPath : String ) { withFont(fontPath) }
350+ fun setFont (font : Font ) { withFont(font) }
351+ fun withFormat (format : Format ) = apply { this .format = format }
376352
377- fun withFont (font : Typeface ): Options {
378- FontCache .get(context).saveTypeface(font)
379- this .font = font
380- return this
381- }
353+ fun animateOnHighlight () = apply { animateOnHighlight = true }
354+ fun disableHighlightAnimation () = apply { animateOnHighlight = false }
355+ fun withShadows () = apply { shadows = true }
356+ fun withoutShadows () = apply { shadows = false }
382357
383- fun withFont (fontPath : String ): Options {
384- this .font = FontCache .get(context).getTypeface(context, fontPath)
385- return this
386- }
358+ fun addCodeLineClickListener (listener : OnCodeLineClickListener ) = apply { lineClickListener = listener }
359+ fun removeCodeLineClickListener () = apply { lineClickListener = null }
387360
388- fun setFont (fontPath : String ) {
389- withFont(fontPath)
390- }
391-
392- fun setFont (font : Font ) {
393- withFont(font)
394- }
395-
396- fun withFormat (format : Format ): Options {
397- this .format = format
398- return this
399- }
400-
401- fun animateOnHighlight (): Options {
402- this .animateOnHighlight = true
403- return this
404- }
405-
406- fun disableHighlightAnimation (): Options {
407- this .animateOnHighlight = false
408- return this
409- }
410-
411- fun withShadows (): Options {
412- this .shadows = true
413- return this
414- }
415-
416- fun withoutShadows (): Options {
417- this .shadows = false
418- return this
419- }
420-
421- fun shortcut (maxLines : Int , shortcutNote : String ): Options {
361+ fun shortcut (maxLines : Int , shortcutNote : String ) = apply {
422362 this .shortcut = true
423363 this .maxLines = maxLines
424364 this .shortcutNote = shortcutNote
425- return this
426- }
427-
428- fun addCodeLineClickListener (listener : OnCodeLineClickListener ): Options {
429- this .lineClickListener = listener
430- return this
431- }
432-
433- fun removeCodeLineClickListener (): Options {
434- this .lineClickListener = null
435- return this
436365 }
437366
438367 companion object Default {
439368 fun get (context : Context ) = Options (context)
440369 }
370+
371+ // - Font helpers
372+
373+ private val fontCache = FontCache .get(context)
374+ private fun Font.get () = fontCache.getTypeface(context, this )
375+ private fun String.get () = fontCache.getTypeface(context, this )
376+ private infix fun <T > Typeface.saveAndThen (body : () -> T ): T = fontCache.saveTypeface(this ).let { body() }
441377}
442378
443- data class Format (val scaleFactor : Float = 1f ,
444- val lineHeight : Int = (LineHeight * scaleFactor).toInt(),
445- val borderHeight : Int = (BorderHeight * scaleFactor).toInt(),
446- val fontSize : Float = FontSize .toFloat()) {
379+ data class Format (
380+ val scaleFactor : Float = 1f ,
381+ val lineHeight : Int = (LineHeight * scaleFactor).toInt(),
382+ val borderHeight : Int = (BorderHeight * scaleFactor).toInt(),
383+ val fontSize : Float = FontSize .toFloat()) {
447384
448385 companion object Default {
449386 private const val LineHeight = 18
0 commit comments