Skip to content

Instantly share code, notes, and snippets.

@DevSrSouza
Created February 5, 2022 19:05
Show Gist options
  • Save DevSrSouza/7dee00bfb2c88e4143f95afcaae997b5 to your computer and use it in GitHub Desktop.
Save DevSrSouza/7dee00bfb2c88e4143f95afcaae997b5 to your computer and use it in GitHub Desktop.
InsertSectionHeader Paging 3
public inline fun <reified T : R, R : Any, C : Any> PagingData<T>.insertSectionHeader(
crossinline sectionBy: suspend (T) -> C,
crossinline insert: suspend (T) -> R,
): PagingData<R> {
return insertSeparators { first: T?, second: T? ->
if(first == null && second != null)
return@insertSeparators insert(second)
if(second == null) return@insertSeparators null
if(first !is T) return@insertSeparators null
val firstSectionByValue = sectionBy(first)
val secondSectionByValue = sectionBy(second)
if(firstSectionByValue != secondSectionByValue) {
insert(first)
} else {
null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment