Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added coroutines version of roxie #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.11'
ext.kotlin_version = '1.4.31'
repositories {
google()
jcenter()
Expand All @@ -8,7 +8,7 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:4.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
Expand Down Expand Up @@ -40,17 +40,17 @@ task clean(type: Delete) {
}

ext {
targetSdkVersion = 28
targetSdkVersion = 30
minSdkVersion = 14
compileSdkVersion = targetSdkVersion
appCompatVersion = '1.0.2'
lifecycleVersion = '2.0.0'
appCompatVersion = '1.2.0'
lifecycleVersion = '2.3.0'
rxJavaVersion = '2.2.2'
junitVersion = '4.12'
junitVersion = '4.13.2'

// Roxie Library
libVersionCode = 2
libVersionName = '0.4.0'
libVersionName = '0.5.0'

libPomUrl = 'https://github.com/ww-tech/roxie'
libGithubRepo = 'ww-tech/roxie'
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
1 change: 1 addition & 0 deletions roxie-coroutines/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions roxie-coroutines/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: "org.jlleitschuh.gradle.ktlint"

android {
compileSdkVersion rootProject.ext.compileSdkVersion

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.libVersionCode
versionName rootProject.ext.libVersionName
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

// ViewModel and LiveData
compileOnly "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.ext.lifecycleVersion"
compileOnly "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.ext.lifecycleVersion"

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3'

api project(":roxie")

testImplementation "junit:junit:$rootProject.ext.junitVersion"
testImplementation 'org.amshove.kluent:kluent:1.45'
}

// applied specifically at the bottom
apply from: './scripts/bintrayConfig.gradle'
21 changes: 21 additions & 0 deletions roxie-coroutines/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
50 changes: 50 additions & 0 deletions roxie-coroutines/scripts/bintrayConfig.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
>>>>> To publish use below

./gradlew clean build install bintrayUpload -Ppublish=true

>>>>> To publish with javadoc Fail on error flag set to false

./gradlew clean build install bintrayUpload -Ppublish=true -PjavadocFlag=true

*/

// publish property is checked here first
if (project.hasProperty('publish') && project.getProperty('publish')) {
// Bintray Params
ext {
bintrayRepo = 'maven'
bintrayName = rootProject.ext.libBintrayName

publishedGroupId = 'com.ww'
libraryName = rootProject.ext.libModuleName
artifact = rootProject.ext.libModuleName // Has to be same as your library module name

libraryDescription = rootProject.ext.libModuleDesc

// Your github repo link
siteUrl = rootProject.ext.libPomUrl
gitUrl = rootProject.ext.libPomUrl + '.git'
githubRepository = rootProject.ext.libGithubRepo

libraryVersion = rootProject.ext.libVersionName

developerId = POM_DEVELOPER_ID
developerName = POM_DEVELOPER_NAME
developerEmail = POM_DEVELOPER_EMAILID

licenseName = POM_LICENCE_NAME
licenseUrl = POM_LICENCE_URL
allLicenses = [POM_ALL_LICENCES]
}

// Place it at the end of the file
apply from: './scripts/installv1.gradle'
apply from: './scripts/bintrayv1.gradle'

if (project.hasProperty('javadocFlag') && project.getProperty('javadocFlag')) {
javadoc {
failOnError = false
}
}
}
63 changes: 63 additions & 0 deletions roxie-coroutines/scripts/bintrayv1.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
apply plugin: 'com.jfrog.bintray'

version = libraryVersion

if (project.hasProperty("android")) { // Android libraries
task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
} else { // Java libraries
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives javadocJar
archives sourcesJar
}

// Bintray
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")

configurations = ['archives']
pkg {
repo = bintrayRepo
name = bintrayName
userOrg = "ww-tech"
desc = libraryDescription
websiteUrl = siteUrl
issueTrackerUrl = siteUrl + '/issues'
vcsUrl = gitUrl
licenses = allLicenses
githubRepo = githubRepository //Optional Github repository
githubReleaseNotesFile = 'README.md' //Optional Github readme file
publish = false
publicDownloadNumbers = true
version {
desc = libraryDescription
gpg {
sign = false //Determines whether to GPG sign the files. The default is false
passphrase = properties.getProperty("bintray.gpg.password")
//Optional. The passphrase for GPG signing'
}
}
}
}
42 changes: 42 additions & 0 deletions roxie-coroutines/scripts/installv1.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apply plugin: 'com.github.dcendents.android-maven'

group = publishedGroupId // Maven Group ID for the artifact

install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
groupId publishedGroupId
artifactId artifact

// Add your description here
name libraryName
description libraryDescription
url siteUrl

// Set your license
licenses {
license {
name licenseName
url licenseUrl
}
}
developers {
developer {
id developerId
name developerName
email developerEmail
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl

}
}
}
}
}
1 change: 1 addition & 0 deletions roxie-coroutines/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.ww.roxie.coroutines" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ww.roxie.coroutines

import androidx.lifecycle.viewModelScope
import com.ww.roxie.BaseAction
import com.ww.roxie.BaseState
import com.ww.roxie.BaseViewModel
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

/**
* roxie
*
* @author dragos
* @since 23.03.2021
*/
abstract class CoroutineViewModel<A : BaseAction, S : BaseState> : BaseViewModel<A, S>() {

protected val actions = ConflatedBroadcastChannel<A>()

override fun offerAction(action: A) {
actions.offer(action)
}

fun <T> Flow<T>.launchHere() = launchIn(viewModelScope)

fun Flow<S>.observeState() = onEach {
state.value = it
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ww.roxie.coroutines

import kotlinx.coroutines.flow.*

/**
* roxie
*
* @author dragos
* @since 23.03.2021
*/
inline fun <reified R> Flow<*>.ofType(): Flow<R> {
return filter {
it is R
}.map {
it as R
}
}

fun <T> Flow<T>.defaultOnEmpty(default: T): Flow<T> {
return onEmpty {
emit(default)
}
}
53 changes: 53 additions & 0 deletions roxie-coroutines/src/test/java/com/ww/roxie/BaseActionTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2019. WW International, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ww.roxie

import org.amshove.kluent.shouldNotBeEqualTo
import org.amshove.kluent.shouldNotContain
import org.junit.Test

class BaseActionTest {

@Test
fun `Given obfuscated action with data, when to string, then to string returns obfuscated string without data`() {
// GIVEN
val data = "test"

// WHEN
val result = ObfuscatedAction(data).toString()

// THEN
result shouldNotContain data
}

@Test
fun `Given two obfuscated actions with different data, when to string, then actions have different obfuscated strings`() {
// GIVEN
val actionOne = ObfuscatedAction("test")
val actionTwo = ObfuscatedAction("test2")

// WHEN
val resultOne = actionOne.toString()
val resultTwo = actionTwo.toString()

// THEN
resultOne shouldNotBeEqualTo resultTwo
}
}

private data class ObfuscatedAction(val data: String) : BaseAction {
override fun toString(): String = obfuscatedString()
}
Loading