Skip to content

Commit

Permalink
Make some names clearer
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Franco <franciscofranco.1990@gmail.com>
  • Loading branch information
franciscofranco committed Oct 28, 2016
1 parent 38f6e1d commit 982bb8f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package liam.franco.selene.bus;

public class AmbientLightSensorChange {
public class LightSensor {
public float light;

public AmbientLightSensorChange(float value) {
public LightSensor(float value) {
this.light = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import android.hardware.SensorManager;

import liam.franco.selene.application.App;
import liam.franco.selene.bus.AmbientLightSensorChange;
import liam.franco.selene.bus.LightSensor;
import liam.franco.selene.bus.StartAmbientLightSensor;
import liam.franco.selene.bus.StopAmbientLightSensor;

Expand All @@ -39,7 +39,7 @@ public AmbientLight() {

@Override
public void onSensorChanged(SensorEvent event) {
App.BUS.post(new AmbientLightSensorChange(event.values[0]));
App.BUS.post(new LightSensor(event.values[0]));
}

@Override
Expand Down
30 changes: 19 additions & 11 deletions Selene/app/src/main/java/liam/franco/selene/modules/Gaia.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import de.halfbit.tinybus.Subscribe;
import liam.franco.selene.application.App;
import liam.franco.selene.bus.AmbientLightSensorChange;
import liam.franco.selene.bus.LightSensor;
import liam.franco.selene.utils.ColorAnimationUtils;
import liam.franco.selene.utils.GaiaUtils;

Expand All @@ -39,20 +39,22 @@
// the views (or any other object you want to mutate). Gaia is omnipotent, it's a God, it can exist as many times
// as the developer wants, at any point in time
public class Gaia {
private static final int COLOR_TRANSFORM_DURATION = 250;

// this field is important because it'll identify the object in the Gaia objects Array inside Application class
private String name;
// well the object that'll be mutated... duh!
private WeakReference<Object> viewToMutate;
private WeakReference<Object> objectToMutate;
// original view color
@ColorInt
private int colorToScale;
private int originalColor;
// stores the last time, in ms, the object mutated
private long lastUpdate;

private Gaia(Builder builder) {
this.name = builder.getName();
this.viewToMutate = builder.getMutativeView();
this.colorToScale = builder.getColorToScale();
this.objectToMutate = builder.getMutativeView();
this.originalColor = builder.getColorToScale();
// tango down!
GaiaUtils.seekAndDestroy(getName());
// good morning!!
Expand Down Expand Up @@ -105,14 +107,14 @@ public String getName() {
}

@Subscribe(mode = Subscribe.Mode.Background)
public void onAmbientLightChange(AmbientLightSensorChange sensor) {
public void onUpdatedAmbientLight(LightSensor sensor) {
// calculates the new color based on the sensor values and the original color
// the algorithm is magical
final int newColor = Color.HSVToColor(GaiaUtils.computeHSV(sensor, viewToMutate, colorToScale));
final int newColor = Color.HSVToColor(GaiaUtils.computeHSV(sensor, objectToMutate, originalColor));
int currentColor = 0;

// mutate all the things
Object view = viewToMutate.get();
Object view = objectToMutate.get();
if (view != null) {
if (view instanceof LinearLayout) {
currentColor = ((ColorDrawable) ((LinearLayout) view).getBackground()).getColor();
Expand All @@ -125,17 +127,23 @@ public void onAmbientLightChange(AmbientLightSensorChange sensor) {
}
}

boolean hasNewColor = currentColor != newColor;
boolean isReady = GaiaUtils.isReady(lastUpdate);

// we don't want to update the colors every time the sensor outputs new values. Yes, the sensor is very nervous
if (currentColor != newColor && GaiaUtils.ready(lastUpdate)) {
final int finalCurrentColor = currentColor;
if (hasNewColor && isReady) {
final int fromColor = currentColor;
lastUpdate = System.currentTimeMillis();

// run this on the Main Thread, this @Subscribe method runs in a background thread
App.MAIN_THREAD.post(new Runnable() {
@Override
public void run() {
// much animation. wow material. such beauty. so rad
ColorAnimationUtils.animateColor(viewToMutate, finalCurrentColor, newColor, 250);
// pass it the object (in this case it's a view) that's going to be mutated,
// the starting color value, and the color we want to animate to
// lastly the duration of the animation, in this case the default should be 250ms
ColorAnimationUtils.animateColor(objectToMutate, fromColor, newColor, COLOR_TRANSFORM_DURATION);
}
});
}
Expand Down
18 changes: 9 additions & 9 deletions Selene/app/src/main/java/liam/franco/selene/utils/GaiaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Locale;

import liam.franco.selene.application.App;
import liam.franco.selene.bus.AmbientLightSensorChange;
import liam.franco.selene.bus.LightSensor;
import liam.franco.selene.modules.Gaia;

public class GaiaUtils {
Expand All @@ -40,7 +40,7 @@ public class GaiaUtils {
public final static float LOW_THRESHOLD = 0.05f;
public final static float MIN = 0.0f;

public static boolean ready(long lastUpdate) {
public static boolean isReady(long lastUpdate) {
return System.currentTimeMillis() > (lastUpdate + MIN_UPDATE_INTERVAL_MS);
}

Expand All @@ -61,16 +61,16 @@ public static void seekAndDestroy(String name) {
/**
*
* @param sensor object coming from our bus containing the Ambient Light sensor value
* @param mutativeObject the object that'll be mutated
* @param colorToScale the current color of the mutative object
* @param objectToMutate the object that'll be mutated
* @param originalColor the original color of the object that'll be mutated
*
* @return a new HSV value to be applied into the object
*/
public static float[] computeHSV(AmbientLightSensorChange sensor, Object mutativeObject, int colorToScale) {
public static float[] computeHSV(LightSensor sensor, Object objectToMutate, int originalColor) {
// we divide the color into red green and blue
int red = Color.red(colorToScale);
int green = Color.green(colorToScale);
int blue = Color.blue(colorToScale);
int red = Color.red(originalColor);
int green = Color.green(originalColor);
int blue = Color.blue(originalColor);

final float hsv[] = new float[3];

Expand All @@ -88,7 +88,7 @@ public static float[] computeHSV(AmbientLightSensorChange sensor, Object mutativ

// Text is, by rule, in a contrasted color to the background, so we have to apply the formula backwards to the
// rest of the views
if (mutativeObject instanceof TextView) {
if (objectToMutate instanceof TextView) {
hsv[2] += div;
} else {
hsv[2] -= div;
Expand Down

0 comments on commit 982bb8f

Please sign in to comment.