Skip to content

Commit

Permalink
Project converted to a library, parameters list handling added, gener…
Browse files Browse the repository at this point in the history
…ating Tweaks class from list of params added
  • Loading branch information
martawoldanska committed Jan 26, 2018
1 parent 667eb2b commit cf3daf3
Show file tree
Hide file tree
Showing 73 changed files with 479 additions and 88 deletions.
File renamed without changes.
50 changes: 50 additions & 0 deletions androidtweaks/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "org.codehaus.groovy.android"

buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.20"
classpath "gradle.plugin.org.codehaus.groovy:groovy-android-gradle-plugin:2.0.0"
classpath 'com.android.tools.build:gradle:3.0.1'
}

repositories {
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
maven { url 'https://maven.google.com' }
maven { url "https://repo1.maven.org/maven2"}
}
}


android {
compileSdkVersion 26
defaultConfig {
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.yaml:snakeyaml:1.19"

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.2.20"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'

testImplementation 'org.codehaus.groovy:groovy:2.4.7:grooid'
testImplementation 'org.spockframework:spock-core:1.0-groovy-2.4'

implementation 'com.squareup:kotlinpoet:0.6.0'
}
File renamed without changes.
8 changes: 8 additions & 0 deletions androidtweaks/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.polidea.androidtweaks">

<application>
<activity android:name=".TweaksActivity"/>
</application>
</manifest>
5 changes: 5 additions & 0 deletions androidtweaks/src/main/assets/tweaks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
param_tag1: 1.0
param_tag2: 1.5
param_tag3: "testValue"
int_param: 1
boolean_param: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.polidea.androidtweaks

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.polidea.androidtweaks.manager.TweaksGenerator
import com.polidea.androidtweaks.manager.TweaksManager

class TweaksActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tweaks)

val settingsManager = TweaksManager.getInstance(this.applicationContext)
System.out.println(settingsManager.params)
settingsManager.setParamValue("param_tag1", 1.4)
System.out.println(settingsManager.params)
TweaksGenerator().generate(TweaksManager.getInstance(this.applicationContext).params)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.polidea.androidtweaks.manager

import com.polidea.androidtweaks.model.*
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec


class TweaksGenerator {
fun generate(params: List<Param<*>>) {
val propertySpecs: ArrayList<PropertySpec> = ArrayList()
for (p in params) {
when (p) {
is BooleanParam -> propertySpecs.add(PropertySpec.builder(p.name, Boolean::class, KModifier.PUBLIC).build())
is DoubleParam -> propertySpecs.add(PropertySpec.builder(p.name, Double::class, KModifier.PUBLIC).build())
is IntegerParam -> propertySpecs.add(PropertySpec.builder(p.name, Int::class, KModifier.PUBLIC).build())
is StringParam -> propertySpecs.add(PropertySpec.builder(p.name, String::class, KModifier.PUBLIC).build())
}
}

val tweaksFile = FileSpec.builder("com.polidea.androidtweaks.tweaks", "Tweaks")
.addType(TypeSpec.classBuilder("Tweaks")
.addProperties(propertySpecs)
.build())

tweaksFile.build().writeTo(System.out) // todo write to the actual file
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.polidea.androidtweaks.manager

import android.content.Context
import com.polidea.androidtweaks.model.Param


class TweaksManager private constructor(context: Context) {
val params: List<Param<*>> = YamlParser().parseYaml("tweaks.yml", context)

companion object {
@Volatile
private var INSTANCE: TweaksManager? = null

@JvmStatic
fun getInstance(context: Context): TweaksManager {
if (INSTANCE == null) {
INSTANCE = TweaksManager(context)
}

return INSTANCE as TweaksManager
}
}

fun <T> getParamValue(key: String): T? = getParam<T>(key)?.value

fun <T> setParamValue(key: String, value: T) {
val param = getParam<T>(key)
param ?: throw IllegalArgumentException("Param with name $key undefined!")
param.value = value
}

@Suppress("UNCHECKED_CAST")
private fun <T> getParam(key: String): Param<T>? = params.find { it.name == key } as Param<T>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package com.polidea.androidtweaks.manager
import android.content.Context
import com.polidea.androidtweaks.model.*
import org.yaml.snakeyaml.Yaml
import timber.log.Timber


class YamlParser {

fun parseYaml(fileName: String, context: Context): List<GenericParam> {
Timber.i("Started parsing file: $fileName")

fun parseYaml(fileName: String, context: Context): List<Param<*>> {
val yaml = Yaml()
val values: Map<String, Any> = yaml.load(context.assets.open(fileName).bufferedReader().use {
it.readText()
})
val paramList = ArrayList<GenericParam>()
val paramList = ArrayList<Param<*>>()

for (v in values) {
when (v.value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.polidea.androidtweaks.model


data class BooleanParam(override var name: String, override var value: Boolean) : Param<Boolean>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.polidea.androidtweaks.model


data class DoubleParam(override var name: String, override var value: Double) : Param<Double>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.polidea.androidtweaks.model


data class IntegerParam(override var name: String, override var value: Int) : Param<Int>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.polidea.androidtweaks.model


interface Param<T> {
val name: String
var value: T
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.polidea.androidtweaks.model


data class StringParam(override var name: String, override var value: String) : Param<String>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.polidea.androidtweaks.utils


class Logger {
// todo
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.polidea.androidtweaks.MainActivity">
tools:context="com.polidea.androidtweaks.TweaksActivity">

<include layout="@layout/number_param_line"/>

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file removed app/libs/snakeyaml-1.19-android.jar
Binary file not shown.
3 changes: 0 additions & 3 deletions app/src/main/assets/settings.yml

This file was deleted.

12 changes: 0 additions & 12 deletions app/src/main/java/com/polidea/androidtweaks/TweaksActivity.kt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.2.10'
ext.kotlin_version = '1.2.20'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
Expand All @@ -20,11 +21,13 @@ allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

repositories {
mavenCentral()
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
Expand Down
1 change: 1 addition & 0 deletions sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
20 changes: 12 additions & 8 deletions app/build.gradle → sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 26



defaultConfig {
applicationId "com.polidea.androidtweaks"
applicationId "com.polidea.sample"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/snakeyaml-1.19-android.jar')

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
compile "org.jetbrains.kotlin:kotlin-reflect:1.2.10"
implementation 'com.jakewharton.timber:timber:4.6.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

implementation project(":android-tweaks")
}
Loading

0 comments on commit cf3daf3

Please sign in to comment.