Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Romaniak committed Feb 11, 2018
1 parent 7239989 commit 42adab1
Show file tree
Hide file tree
Showing 17 changed files with 606 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pbxproj -text
46 changes: 46 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

# OSX
#
.DS_Store

# node.js
#
node_modules/
npm-debug.log
yarn-error.log


# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace


# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml

# BUCK
buck-out/
\.buckd/
*.keystore

12 changes: 12 additions & 0 deletions BackgroundManager.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NativeModules } from 'react-native';


function setRootViewBackgroundColor(hex) {
if (hex.length === 4) {
hex = `#${hex[1]}${hex[1]}${hex[2]}${hex[2]}${hex[3]}${hex[3]}`
}
NativeModules.RNRootViewBackground.setBackground(hex);
}


export { setRootViewBackgroundColor };
29 changes: 29 additions & 0 deletions BackgroundManager.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { NativeModules } from 'react-native';

function hexToRgb(hex) {
let c;
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
c = hex.substring(1).split('');
if (c.length === 3) {
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c = `0x${c.join('')}`;
const red = parseFloat((c >> 16) & 255);
const green = parseFloat((c >> 8) & 255);
const blue = parseFloat(c & 255);
return [red, green, blue];
}
throw new Error('Bad Hex');
}


function setRootViewBackgroundColor(hex) {
const [r, g, b] = hexToRgb(hex);
setRootViewBackgroundColorWithRGB(r, g, b, 1);
}

function setRootViewBackgroundColorWithRGB(r, g, b, a) {
NativeModules.RNRootViewBackground.setBackground(r, g, b, a);
}

export { setRootViewBackgroundColor };
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

# react-native-root-view-background

## Getting started

`$ npm install react-native-root-view-background --save`

### Mostly automatic installation

`$ react-native link react-native-root-view-background`

### Manual installation


#### iOS

1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
2. Go to `node_modules``react-native-root-view-background` and add `RNRootViewBackground.xcodeproj`
3. In XCode, in the project navigator, select your project. Add `libRNRootViewBackground.a` to your project's `Build Phases``Link Binary With Libraries`
4. Run your project (`Cmd+R`)<

#### Android

1. Open up `android/app/src/main/java/[...]/MainActivity.java`
- Add `import com.reactlibrary.RNRootViewBackgroundPackage;` to the imports at the top of the file
- Add `new RNRootViewBackgroundPackage()` to the list returned by the `getPackages()` method
2. Append the following lines to `android/settings.gradle`:
```
include ':react-native-root-view-background'
project(':react-native-root-view-background').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-root-view-background/android')
```
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
```
compile project(':react-native-root-view-background')
```

#### Windows
[Read it! :D](https://github.com/ReactWindows/react-native)

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.
2. Open up your `MainPage.cs` app
- Add `using Root.View.Background.RNRootViewBackground;` to the usings at the top of the file
- Add `new RNRootViewBackgroundPackage()` to the `List<IReactPackage>` returned by the `Packages` method


## Usage
```javascript
import RNRootViewBackground from 'react-native-root-view-background';

// TODO: What to do with the module?
RNRootViewBackground;
```

36 changes: 36 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

buildscript {
repositories {
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
lintOptions {
abortOnError false
}
}

repositories {
mavenCentral()
}

dependencies {
compile 'com.facebook.react:react-native:+'
}

6 changes: 6 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reactlibrary">

</manifest>

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

package com.reactlibrary;

import android.app.Activity;
import android.view.View;
import android.graphics.Color;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;

public class RNRootViewBackgroundModule extends ReactContextBaseJavaModule {

private final ReactApplicationContext reactContext;

public RNRootViewBackgroundModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}

@Override
public String getName() {
return "RNRootViewBackground";
}

@ReactMethod
public void setBackground(final String color) {
final Activity activity = getCurrentActivity();

if (activity == null) {
return;
}

activity.runOnUiThread(new Runnable() {
@Override
public void run() {
View rootView = activity.getWindow().getDecorView();
int parsedColor = Color.parseColor(color);
rootView.getRootView().setBackgroundColor(parsedColor);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

package com.reactlibrary;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;
public class RNRootViewBackgroundPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNRootViewBackgroundModule(reactContext));
}

// Deprecated from RN 0.47
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { setRootViewBackgroundColor } from './BackgroundManager';

export { setRootViewBackgroundColor };
12 changes: 12 additions & 0 deletions ios/RNRootViewBackground.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#if __has_include("RCTBridgeModule.h")
#import "RCTBridgeModule.h"
#else
#import <React/RCTBridgeModule.h>
#endif
#import <UIKit/UIKit.h>

@interface RNRootViewBackground : NSObject <RCTBridgeModule>

@end

22 changes: 22 additions & 0 deletions ios/RNRootViewBackground.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#import "RNRootViewBackground.h"

@implementation RNRootViewBackground

- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}

RCT_EXPORT_METHOD(setBackground:(float)red green:(float)green blue:(float)blue alpha:(float)alpha)
{
dispatch_async( dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
rootViewController.view.backgroundColor = [[UIColor alloc] initWithRed:red/255 green:green/255 blue:blue/255 alpha:alpha];
});
}

RCT_EXPORT_MODULE()

@end

24 changes: 24 additions & 0 deletions ios/RNRootViewBackground.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

Pod::Spec.new do |s|
s.name = "RNRootViewBackground"
s.version = "1.0.0"
s.summary = "RNRootViewBackground"
s.description = <<-DESC
RNRootViewBackground
DESC
s.homepage = ""
s.license = "MIT"
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
s.author = { "author" => "author@domain.cn" }
s.platform = :ios, "7.0"
s.source = { :git => "https://github.com/author/RNRootViewBackground.git", :tag => "master" }
s.source_files = "RNRootViewBackground/**/*.{h,m}"
s.requires_arc = true


s.dependency "React"
#s.dependency "others"

end


Loading

0 comments on commit 42adab1

Please sign in to comment.