Skip to content

Commit 0576f99

Browse files
authored
Merge pull request #2 from eadm/model/1.0.3
Model/1.0.3
2 parents e5b5292 + 245a7d1 commit 0576f99

13 files changed

Lines changed: 327 additions & 8 deletions

File tree

Model/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

Model/build.gradle

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
3+
4+
apply plugin: 'com.github.dcendents.android-maven'
5+
apply plugin: 'com.jfrog.bintray'
6+
7+
android {
8+
compileSdkVersion versions.compileSdk
9+
buildToolsVersion versions.buildTools
10+
11+
defaultConfig {
12+
minSdkVersion versions.minSdk
13+
targetSdkVersion versions.targetSdk
14+
versionCode versions.code
15+
versionName versions.coreModel
16+
17+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
18+
19+
}
20+
21+
buildTypes {
22+
release {
23+
minifyEnabled false
24+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25+
}
26+
}
27+
28+
}
29+
30+
dependencies {
31+
implementation libraries.kotlin
32+
33+
testImplementation libraries.junit
34+
testImplementation libraries.robolectric
35+
testImplementation libraries.mockitoCore
36+
testImplementation libraries.mockitoKt
37+
testImplementation libraries.hamcrest
38+
}
39+
40+
ext {
41+
bintrayRepo = 'ru.nobird.android'
42+
bintrayName = 'ru.nobird.android.core.model'
43+
44+
publishedGroupId = 'ru.nobird.android.core'
45+
libraryName = 'model'
46+
artifact = 'model'
47+
48+
libraryDescription = 'Core model'
49+
50+
siteUrl = 'https://github.com/eadm/AndroiKit/Model'
51+
gitUrl = 'https://github.com/eadm/AndroiKit/Model'
52+
53+
libraryVersion = versions.coreModel
54+
55+
developerId = 'eadm'
56+
developerName = 'Ruslan Davletshin'
57+
developerEmail = 'smartmorefeed@gmail.com'
58+
59+
licenseName = 'The Apache Software License, Version 2.0'
60+
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
61+
allLicenses = ["Apache-2.0"]
62+
}
63+
64+
apply from: "../bintray.gradle"

Model/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.nobird.android.core.model;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("ru.nobird.android.core.model.test", appContext.getPackageName());
25+
}
26+
}

Model/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="ru.nobird.android.core.model" />
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package ru.nobird.android.core.model
2+
3+
fun <T: Comparable<T>> Array<T>.isOrdered(): Boolean =
4+
(0 until this.size - 1).none { this[it] > this[it + 1] }
5+
6+
fun <T: Comparable<T>> Array<T>.isNotOrdered(): Boolean =
7+
!this.isOrdered()
8+
9+
fun <T: Comparable<T>> Array<T>.isOrderedDesc(): Boolean =
10+
(0 until this.size - 1).none { this[it] < this[it + 1] }
11+
12+
/**
13+
* Maps collection straight to long array
14+
*/
15+
inline fun <T> Collection<T>.mapToLongArray(transform: (T) -> Long): LongArray {
16+
val array = LongArray(this.size)
17+
forEachIndexed { index, t ->
18+
array[index] = transform(t)
19+
}
20+
return array
21+
}
22+
23+
/**
24+
* Maps array straight to long array
25+
*/
26+
inline fun <T> Array<T>.mapToLongArray(transform: (T) -> Long): LongArray {
27+
val array = LongArray(this.size)
28+
forEachIndexed { index, t ->
29+
array[index] = transform(t)
30+
}
31+
return array
32+
}
33+
34+
fun LongArray?.isNullOrEmpty(): Boolean =
35+
this == null || this.isEmpty()
36+
37+
/**
38+
* Flattens collection of long arrays
39+
*/
40+
@JvmName("Iterable_LongArray__flatten")
41+
fun Iterable<LongArray>.flatten(): LongArray {
42+
val size = sumBy { it.size }
43+
val array = LongArray(size)
44+
var offset = 0
45+
forEach { subArray ->
46+
subArray.copyInto(array, destinationOffset = offset)
47+
offset += subArray.size
48+
}
49+
50+
return array
51+
}
52+
53+
/**
54+
* Removes duplicates from long array
55+
*/
56+
@JvmName("LongArray_distinct")
57+
fun LongArray.distinct(): LongArray =
58+
toMutableSet().toLongArray()
59+
60+
/**
61+
* Immutable swap
62+
*/
63+
fun <T> List<T>.swap(i: Int, j: Int): List<T> {
64+
if (i !in 0 until size ||
65+
j !in 0 until size) {
66+
return this
67+
}
68+
69+
val a = this[i]
70+
val b = this[j]
71+
return mapIndexed { index, t ->
72+
when (index) {
73+
i -> b
74+
j -> a
75+
else -> t
76+
}
77+
}
78+
}
79+
80+
/**
81+
* Applies mutation to list
82+
*/
83+
inline fun <T> List<T>.mutate(mutation: MutableList<T>.() -> Unit): List<T> =
84+
this.toMutableList().apply(mutation)
85+
86+
/**
87+
* Applies mutation to list
88+
*/
89+
inline fun <T> PagedList<T>.mutate(mutation: MutableList<T>.() -> Unit): PagedList<T> =
90+
PagedList(this.toMutableList().apply(mutation), hasPrev = hasPrev, hasNext = hasNext, page = page)
91+
92+
/**
93+
* Creates new list from current with inserted [item] at [pos]
94+
*/
95+
fun <T> List<T>.insert(item: T, pos: Int = size): List<T> {
96+
val list = mutableListOf<T>()
97+
list.addAll(this.slice(to = pos))
98+
list.add(item)
99+
list.addAll(this.slice(from = pos))
100+
return list
101+
}
102+
103+
/**
104+
* Creates sublist as view from current
105+
*/
106+
fun <T> List<T>.slice(from: Int = 0, to: Int = size): List<T> =
107+
subList(from.coerceAtLeast(0), to.coerceAtMost(size))
108+
109+
/**
110+
* Puts [value] in map if it is not null
111+
*/
112+
fun <K, V> MutableMap<K, V>.putNullable(key: K, value: V?) {
113+
if (value != null) {
114+
put(key, value)
115+
}
116+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ru.nobird.android.core.model
2+
3+
interface Identifiable<T> {
4+
val id: T
5+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ru.nobird.android.core.model
2+
3+
class PagedList<E>(
4+
list: List<E>,
5+
6+
val page: Int = 1,
7+
val hasNext: Boolean = false,
8+
val hasPrev: Boolean = false
9+
) : List<E> by list
10+
11+
fun <E> List<E>.concatWithPagedList(pagedList: PagedList<E>): PagedList<E> =
12+
PagedList(this + pagedList, page = pagedList.page, hasNext = pagedList.hasNext, hasPrev = pagedList.hasPrev)
13+
14+
inline fun <T, R> PagedList<T>.mapPaged(transform: (T) -> R): PagedList<R> =
15+
PagedList(map(transform), page = page, hasNext = hasNext, hasPrev = hasPrev)
16+
17+
/**
18+
* Concatenate two paged lists
19+
*/
20+
operator fun <E> PagedList<E>.plus(pagedList: PagedList<E>): PagedList<E> =
21+
PagedList(this as List<E> + pagedList, page = pagedList.page, hasNext = pagedList.hasNext, hasPrev = hasPrev)
22+
23+
/**
24+
* Adds element to the end of paged list and returns it
25+
*/
26+
operator fun <E> PagedList<E>.plus(element: E): PagedList<E> =
27+
PagedList(this as List<E> + element, page = page, hasNext = hasNext, hasPrev = hasPrev)
28+
29+
/**
30+
* Returns a list containing all elements not matching the given [predicate].
31+
*/
32+
inline fun <E> PagedList<E>.filterNot(predicate: (E) -> Boolean): PagedList<E> =
33+
PagedList((this as List<E>).filterNot(predicate), page = page, hasNext = hasNext, hasPrev = hasPrev)
34+
35+
/**
36+
* Transforms current paged list into another with saving of pagination information
37+
*/
38+
inline fun <E, R> PagedList<E>.transform(operation: PagedList<E>.() -> List<R>): PagedList<R> =
39+
PagedList(operation(this), page = page, hasNext = hasNext, hasPrev = hasPrev)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ru.nobird.android.core.model
2+
3+
enum class PaginationDirection {
4+
UP, DOWN
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Model</string>
3+
</resources>

0 commit comments

Comments
 (0)