Skip to content
This repository was archived by the owner on Feb 19, 2021. It is now read-only.

Commit 42adab1

Browse files
author
Jan Romaniak
committed
init
1 parent 7239989 commit 42adab1

17 files changed

+606
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# OSX
3+
#
4+
.DS_Store
5+
6+
# node.js
7+
#
8+
node_modules/
9+
npm-debug.log
10+
yarn-error.log
11+
12+
13+
# Xcode
14+
#
15+
build/
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
xcuserdata
25+
*.xccheckout
26+
*.moved-aside
27+
DerivedData
28+
*.hmap
29+
*.ipa
30+
*.xcuserstate
31+
project.xcworkspace
32+
33+
34+
# Android/IntelliJ
35+
#
36+
build/
37+
.idea
38+
.gradle
39+
local.properties
40+
*.iml
41+
42+
# BUCK
43+
buck-out/
44+
\.buckd/
45+
*.keystore
46+

BackgroundManager.android.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NativeModules } from 'react-native';
2+
3+
4+
function setRootViewBackgroundColor(hex) {
5+
if (hex.length === 4) {
6+
hex = `#${hex[1]}${hex[1]}${hex[2]}${hex[2]}${hex[3]}${hex[3]}`
7+
}
8+
NativeModules.RNRootViewBackground.setBackground(hex);
9+
}
10+
11+
12+
export { setRootViewBackgroundColor };

BackgroundManager.ios.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { NativeModules } from 'react-native';
2+
3+
function hexToRgb(hex) {
4+
let c;
5+
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
6+
c = hex.substring(1).split('');
7+
if (c.length === 3) {
8+
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
9+
}
10+
c = `0x${c.join('')}`;
11+
const red = parseFloat((c >> 16) & 255);
12+
const green = parseFloat((c >> 8) & 255);
13+
const blue = parseFloat(c & 255);
14+
return [red, green, blue];
15+
}
16+
throw new Error('Bad Hex');
17+
}
18+
19+
20+
function setRootViewBackgroundColor(hex) {
21+
const [r, g, b] = hexToRgb(hex);
22+
setRootViewBackgroundColorWithRGB(r, g, b, 1);
23+
}
24+
25+
function setRootViewBackgroundColorWithRGB(r, g, b, a) {
26+
NativeModules.RNRootViewBackground.setBackground(r, g, b, a);
27+
}
28+
29+
export { setRootViewBackgroundColor };

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# react-native-root-view-background
3+
4+
## Getting started
5+
6+
`$ npm install react-native-root-view-background --save`
7+
8+
### Mostly automatic installation
9+
10+
`$ react-native link react-native-root-view-background`
11+
12+
### Manual installation
13+
14+
15+
#### iOS
16+
17+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
18+
2. Go to `node_modules``react-native-root-view-background` and add `RNRootViewBackground.xcodeproj`
19+
3. In XCode, in the project navigator, select your project. Add `libRNRootViewBackground.a` to your project's `Build Phases``Link Binary With Libraries`
20+
4. Run your project (`Cmd+R`)<
21+
22+
#### Android
23+
24+
1. Open up `android/app/src/main/java/[...]/MainActivity.java`
25+
- Add `import com.reactlibrary.RNRootViewBackgroundPackage;` to the imports at the top of the file
26+
- Add `new RNRootViewBackgroundPackage()` to the list returned by the `getPackages()` method
27+
2. Append the following lines to `android/settings.gradle`:
28+
```
29+
include ':react-native-root-view-background'
30+
project(':react-native-root-view-background').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-root-view-background/android')
31+
```
32+
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
33+
```
34+
compile project(':react-native-root-view-background')
35+
```
36+
37+
#### Windows
38+
[Read it! :D](https://github.com/ReactWindows/react-native)
39+
40+
1. In Visual Studio add the `RNRootViewBackground.sln` in `node_modules/react-native-root-view-background/windows/RNRootViewBackground.sln` folder to their solution, reference from their app.
41+
2. Open up your `MainPage.cs` app
42+
- Add `using Root.View.Background.RNRootViewBackground;` to the usings at the top of the file
43+
- Add `new RNRootViewBackgroundPackage()` to the `List<IReactPackage>` returned by the `Packages` method
44+
45+
46+
## Usage
47+
```javascript
48+
import RNRootViewBackground from 'react-native-root-view-background';
49+
50+
// TODO: What to do with the module?
51+
RNRootViewBackground;
52+
```
53+

android/build.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
buildscript {
3+
repositories {
4+
jcenter()
5+
}
6+
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.3.1'
9+
}
10+
}
11+
12+
apply plugin: 'com.android.library'
13+
14+
android {
15+
compileSdkVersion 23
16+
buildToolsVersion "23.0.1"
17+
18+
defaultConfig {
19+
minSdkVersion 16
20+
targetSdkVersion 22
21+
versionCode 1
22+
versionName "1.0"
23+
}
24+
lintOptions {
25+
abortOnError false
26+
}
27+
}
28+
29+
repositories {
30+
mavenCentral()
31+
}
32+
33+
dependencies {
34+
compile 'com.facebook.react:react-native:+'
35+
}
36+

android/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.reactlibrary">
4+
5+
</manifest>
6+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
package com.reactlibrary;
3+
4+
import android.app.Activity;
5+
import android.view.View;
6+
import android.graphics.Color;
7+
8+
import com.facebook.react.bridge.ReactApplicationContext;
9+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
10+
import com.facebook.react.bridge.ReactMethod;
11+
import com.facebook.react.bridge.Callback;
12+
13+
public class RNRootViewBackgroundModule extends ReactContextBaseJavaModule {
14+
15+
private final ReactApplicationContext reactContext;
16+
17+
public RNRootViewBackgroundModule(ReactApplicationContext reactContext) {
18+
super(reactContext);
19+
this.reactContext = reactContext;
20+
}
21+
22+
@Override
23+
public String getName() {
24+
return "RNRootViewBackground";
25+
}
26+
27+
@ReactMethod
28+
public void setBackground(final String color) {
29+
final Activity activity = getCurrentActivity();
30+
31+
if (activity == null) {
32+
return;
33+
}
34+
35+
activity.runOnUiThread(new Runnable() {
36+
@Override
37+
public void run() {
38+
View rootView = activity.getWindow().getDecorView();
39+
int parsedColor = Color.parseColor(color);
40+
rootView.getRootView().setBackgroundColor(parsedColor);
41+
}
42+
});
43+
}
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
package com.reactlibrary;
3+
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import com.facebook.react.ReactPackage;
9+
import com.facebook.react.bridge.NativeModule;
10+
import com.facebook.react.bridge.ReactApplicationContext;
11+
import com.facebook.react.uimanager.ViewManager;
12+
import com.facebook.react.bridge.JavaScriptModule;
13+
public class RNRootViewBackgroundPackage implements ReactPackage {
14+
@Override
15+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
16+
return Arrays.<NativeModule>asList(new RNRootViewBackgroundModule(reactContext));
17+
}
18+
19+
// Deprecated from RN 0.47
20+
public List<Class<? extends JavaScriptModule>> createJSModules() {
21+
return Collections.emptyList();
22+
}
23+
24+
@Override
25+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
26+
return Collections.emptyList();
27+
}
28+
}

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { setRootViewBackgroundColor } from './BackgroundManager';
2+
3+
export { setRootViewBackgroundColor };

ios/RNRootViewBackground.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
#if __has_include("RCTBridgeModule.h")
3+
#import "RCTBridgeModule.h"
4+
#else
5+
#import <React/RCTBridgeModule.h>
6+
#endif
7+
#import <UIKit/UIKit.h>
8+
9+
@interface RNRootViewBackground : NSObject <RCTBridgeModule>
10+
11+
@end
12+

ios/RNRootViewBackground.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
#import "RNRootViewBackground.h"
3+
4+
@implementation RNRootViewBackground
5+
6+
- (dispatch_queue_t)methodQueue
7+
{
8+
return dispatch_get_main_queue();
9+
}
10+
11+
RCT_EXPORT_METHOD(setBackground:(float)red green:(float)green blue:(float)blue alpha:(float)alpha)
12+
{
13+
dispatch_async( dispatch_get_main_queue(), ^{
14+
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
15+
rootViewController.view.backgroundColor = [[UIColor alloc] initWithRed:red/255 green:green/255 blue:blue/255 alpha:alpha];
16+
});
17+
}
18+
19+
RCT_EXPORT_MODULE()
20+
21+
@end
22+

ios/RNRootViewBackground.podspec

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
Pod::Spec.new do |s|
3+
s.name = "RNRootViewBackground"
4+
s.version = "1.0.0"
5+
s.summary = "RNRootViewBackground"
6+
s.description = <<-DESC
7+
RNRootViewBackground
8+
DESC
9+
s.homepage = ""
10+
s.license = "MIT"
11+
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
12+
s.author = { "author" => "[email protected]" }
13+
s.platform = :ios, "7.0"
14+
s.source = { :git => "https://github.com/author/RNRootViewBackground.git", :tag => "master" }
15+
s.source_files = "RNRootViewBackground/**/*.{h,m}"
16+
s.requires_arc = true
17+
18+
19+
s.dependency "React"
20+
#s.dependency "others"
21+
22+
end
23+
24+

0 commit comments

Comments
 (0)