-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for null as color (#5393)
This commit adds initial support for passing null as a valid value for color properties. When a color property is applied, if it wasn't specified then the default color is applied. Now, when null is passed, no color will be applied leaving the last color applied as is. This is useful when showing an Overlay and you'd like to keep the current StatusBar color unaffected. Another usecase would be when you'd like to avoid coloring an icon in BottomTabs.
- Loading branch information
Showing
40 changed files
with
305 additions
and
126 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
lib/android/app/src/main/java/com/reactnativenavigation/parse/params/DontApplyColour.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.reactnativenavigation.parse.params; | ||
|
||
import android.graphics.Color; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
public class DontApplyColour extends Colour { | ||
public DontApplyColour() { | ||
super(Color.TRANSPARENT); | ||
} | ||
|
||
@Override | ||
public boolean hasValue() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean canApplyValue() { | ||
return false; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String toString() { | ||
return "NoColor"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
lib/android/app/src/main/java/com/reactnativenavigation/parse/parsers/ColorParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
package com.reactnativenavigation.parse.parsers; | ||
|
||
import com.reactnativenavigation.parse.params.Colour; | ||
import com.reactnativenavigation.parse.params.DontApplyColour; | ||
import com.reactnativenavigation.parse.params.NullColor; | ||
|
||
import org.json.JSONObject; | ||
|
||
public class ColorParser { | ||
public static Colour parse(JSONObject json, String color) { | ||
return json.has(color) ? new Colour(json.optInt(color)) : new NullColor(); | ||
if (json.has(color)) { | ||
return json.opt(color) instanceof Integer ? new Colour(json.optInt(color)) : new DontApplyColour(); | ||
} | ||
return new NullColor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
lib/android/app/src/test/java/com/reactnativenavigation/parse/parsers/ColorParseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.reactnativenavigation.parse.parsers; | ||
|
||
import com.reactnativenavigation.BaseTest; | ||
import com.reactnativenavigation.parse.params.DontApplyColour; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Java6Assertions.assertThat; | ||
|
||
public class ColorParseTest extends BaseTest { | ||
|
||
@Test | ||
public void nullIsParsedAsNoColor() throws JSONException { | ||
JSONObject json = new JSONObject(); | ||
json.put("color", "NoColor"); | ||
assertThat(ColorParser.parse(json, "color")).isInstanceOf(DontApplyColour.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
#import "ColorParser.h" | ||
#import "NullColor.h" | ||
#import "NoColor.h" | ||
#import <React/RCTConvert.h> | ||
|
||
@implementation ColorParser | ||
|
||
+ (Color *)parse:(NSDictionary *)json key:(NSString *)key { | ||
return json[key] ? [[Color alloc] initWithValue:[RCTConvert UIColor:json[key]]] : [NullColor new]; | ||
if (json[key]) { | ||
return [json[key] isKindOfClass:[NSNumber class]] ? [[Color alloc] initWithValue:[RCTConvert UIColor:json[key]]] : [NoColor new]; | ||
} | ||
return [NullColor new]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#import "Color.h" | ||
|
||
@interface NoColor : Color | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#import "NoColor.h" | ||
#import "Color.h" | ||
|
||
|
||
@implementation NoColor | ||
|
||
- (BOOL)hasValue { | ||
return YES; | ||
} | ||
|
||
- (UIColor *)get { | ||
// return nil; | ||
return UIColor.cyanColor; | ||
} | ||
|
||
- (UIColor *)getWithDefaultValue:(id)defaultValue { | ||
return UIColor.cyanColor; | ||
// return nil; | ||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.