Skip to content

Commit

Permalink
Merge pull request #14 from InsanusMokrassar/0.3.1
Browse files Browse the repository at this point in the history
0.3.1
  • Loading branch information
InsanusMokrassar authored Nov 10, 2020
2 parents aeee416 + 5b3d9e8 commit 776a3af
Show file tree
Hide file tree
Showing 56 changed files with 1,196 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ build/
out/

secret.gradle
local.properties

publishing.sh
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
# Changelog

## 0.3.1

**ANDROID PACKAGES**

* `Android`:
* `RecyclerView`:
* Library has been created
* `Common`
* Now available package `dev.inmo:micro_utils.common-android`
* `Coroutines`
* Now available package `dev.inmo:micro_utils.coroutines-android`
* `Ktor`
* `Common`
* Now available package `dev.inmo:micro_utils.ktor.common-android`
* `Client`
* Now available package `dev.inmo:micro_utils.ktor.client-android`
* `MimeTypes`
* Now available package `dev.inmo:micro_utils.mime_types-android`
* `Pagination`
* `Common`
* Now available package `dev.inmo:micro_utils.pagination.common-android`
* `Ktor`
* `Common`
* Now available package `dev.inmo:micro_utils.pagination.ktor.common-android`
* `Repos`
* `Common`
* Now available package `dev.inmo:micro_utils.repos.common-android`
* Now it is possible to use default realizations of repos abstractions natively on android
* `Inmemory`
* Now available package `dev.inmo:micro_utils.repos.inmemory-android`
* `Ktor`
* `Common`
* Now available package `dev.inmo:micro_utils.repos.ktor.common-android`
* `Common`
* Now available package `dev.inmo:micro_utils.repos.ktor.client-android`

## 0.3.0

All deprecations has been removed
Expand Down
23 changes: 23 additions & 0 deletions android/recyclerview/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
id "kotlin-android-extensions"
}

apply from: "$mppAndroidProjectPresetPath"

kotlin {
sourceSets {
commonMain {
dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
}
}
androidMain {
dependencies {
api "androidx.recyclerview:recyclerview:$androidx_recycler_version"
}
}
}
}
1 change: 1 addition & 0 deletions android/recyclerview/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="dev.inmo.micro_utils.android.recyclerview"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.inmo.micro_utils.android.recyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

abstract class AbstractStandardViewHolder<T>(
inflater: LayoutInflater,
container: ViewGroup?,
viewId: Int,
onViewInflated: ((View) -> Unit)? = null
) : AbstractViewHolder<T>(
inflater.inflate(viewId, container, false).also {
onViewInflated ?.invoke(it)
}
) {
constructor(
container: ViewGroup,
viewId: Int,
onViewInflated: ((View) -> Unit)? = null
) : this(LayoutInflater.from(container.context), container, viewId, onViewInflated)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.inmo.micro_utils.android.recyclerview

import android.view.View
import androidx.recyclerview.widget.RecyclerView

abstract class AbstractViewHolder<in T>(
view: View
) : RecyclerView.ViewHolder(view) {
abstract fun onBind(item: T)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.inmo.micro_utils.android.recyclerview

import android.content.Context
import android.widget.LinearLayout
import androidx.recyclerview.widget.DividerItemDecoration

val Context.recyclerViewItemsDecoration
get() = DividerItemDecoration(this, LinearLayout.VERTICAL)

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dev.inmo.micro_utils.android.recyclerview

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*

fun RecyclerView.lastVisibleItemFlow(
completingScope: CoroutineScope
): Flow<Int> {
val lastVisibleElementFun: () -> Int = (layoutManager as? LinearLayoutManager) ?.let { it::findLastVisibleItemPosition } ?: error("Currently supported only linear layout manager")
val lastVisibleFlow = MutableStateFlow(lastVisibleElementFun())
addOnScrollListener(
object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
lastVisibleFlow.value = lastVisibleElementFun()
}
}.also { scrollListener ->
lastVisibleFlow.onCompletion {
removeOnScrollListener(scrollListener)
}.launchIn(completingScope)
}
)
return lastVisibleFlow.asStateFlow()
}

inline fun Flow<Int>.mapLeftItems(
crossinline countGetter: () -> Int
): Flow<Int> = map { countGetter() - it }

inline fun Flow<Int>.mapRequireFilling(
minimalLeftItems: Int,
crossinline countGetter: () -> Int
): Flow<Int> = mapLeftItems(countGetter).mapNotNull {
if (it < minimalLeftItems) {
it
} else {
null
}
}

inline fun RecyclerView.mapRequireFilling(
minimalLeftItems: Int,
completingScope: CoroutineScope,
crossinline countGetter: () -> Int
): Flow<Int> = lastVisibleItemFlow(completingScope).mapRequireFilling(minimalLeftItems, countGetter)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dev.inmo.micro_utils.android.recyclerview

import android.view.View
import androidx.recyclerview.widget.RecyclerView


abstract class RecyclerViewAdapter<T>(
val data: List<T>
): RecyclerView.Adapter<AbstractViewHolder<T>>() {
var emptyView: View? = null
set(value) {
field = value
checkEmpty()
}

init {
registerAdapterDataObserver(
object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
super.onItemRangeChanged(positionStart, itemCount)
checkEmpty()
}

override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
super.onItemRangeChanged(positionStart, itemCount, payload)
checkEmpty()
}

override fun onChanged() {
super.onChanged()
checkEmpty()
}

override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
super.onItemRangeRemoved(positionStart, itemCount)
checkEmpty()
}

override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
super.onItemRangeMoved(fromPosition, toPosition, itemCount)
checkEmpty()
}

override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)
checkEmpty()
}
}
)
checkEmpty()
}

override fun getItemCount(): Int = data.size

override fun onBindViewHolder(holder: AbstractViewHolder<T>, position: Int) {
holder.onBind(data[position])
}

private fun checkEmpty() {
emptyView ?. let {
if (data.isEmpty()) {
it.visibility = View.VISIBLE
} else {
it.visibility = View.GONE
}
}
}
}
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
buildscript {
repositories {
mavenLocal()
jcenter()
google()
mavenCentral()
mavenLocal()
maven { url "https://plugins.gradle.org/m2/" }
}

dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version"
classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:$dexcount_version"
classpath "com.github.breadmoirai:github-release:$github_release_plugin_version"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
}
}

Expand All @@ -19,6 +23,7 @@ allprojects {
mavenLocal()
jcenter()
mavenCentral()
google()
maven { url "https://kotlin.bintray.com/kotlinx" }
}
}
Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
id "kotlin-android-extensions"
}

apply from: "$mppProjectWithSerializationPresetPath"
1 change: 1 addition & 0 deletions common/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="dev.inmo.micro_utils.common"/>
7 changes: 7 additions & 0 deletions common/src/main/kotlin/dev/inmo/micro_utils/common/Mapping.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.inmo.micro_utils.common

@Suppress("UNCHECKED_CAST", "SimplifiableCall")
inline fun <T, R> Iterable<T>.mapNotNullA(transform: (T) -> R?): List<R> = map(transform).filter { it != null } as List<R>

@Suppress("UNCHECKED_CAST", "SimplifiableCall")
inline fun <T, R> Array<T>.mapNotNullA(mapper: (T) -> R?): List<R> = map(mapper).filter { it != null } as List<R>
7 changes: 7 additions & 0 deletions coroutines/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
id "kotlin-android-extensions"
}

apply from: "$mppProjectWithSerializationPresetPath"
Expand All @@ -12,5 +14,10 @@ kotlin {
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
}
}
androidMain {
dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
}
}
}
}
1 change: 1 addition & 0 deletions coroutines/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="dev.inmo.micro_utils.coroutines"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.inmo.micro_utils.coroutines

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

suspend inline fun <T> doInUI(noinline block: suspend CoroutineScope.() -> T) = withContext(
Dispatchers.Main,
block
)
31 changes: 31 additions & 0 deletions defaultAndroidSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
android {
compileSdkVersion "$android_compileSdkVersion".toInteger()
buildToolsVersion "$android_buildToolsVersion"

defaultConfig {
minSdkVersion "$android_minSdkVersion".toInteger()
targetSdkVersion "$android_compileSdkVersion".toInteger()
versionCode "${android_code_version}".toInteger()
versionName "$version"
}
buildTypes {
release {
minifyEnabled false
}
}

packagingOptions {
exclude 'META-INF/kotlinx-serialization-runtime.kotlin_module'
exclude 'META-INF/kotlinx-serialization-cbor.kotlin_module'
exclude 'META-INF/kotlinx-serialization-properties.kotlin_module'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
Loading

0 comments on commit 776a3af

Please sign in to comment.