Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rvenky125 committed Jun 15, 2023
0 parents commit 6974d6c
Show file tree
Hide file tree
Showing 19 changed files with 460 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .bundle/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BUNDLE_PATH: "vendor/bundle"
BUNDLE_FORCE_RUBY_PLATFORM: 1
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: '@react-native-community',
};
33 changes: 33 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
63 changes: 63 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
ios/.xcode.env.local

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml
*.hprof
.cxx/
*.keystore
!debug.keystore

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

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

**/fastlane/report.xml
**/fastlane/Preview.html
**/fastlane/screenshots
**/fastlane/test_output

# Bundle artifact
*.jsbundle

# Ruby / CocoaPods
/ios/Pods/
/vendor/bundle/

# Temporary files created by Metro to check the health of the file watcher
.metro-health-check*
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
12 changes: 12 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Logs
*.log
npm-debug.log

# Dependency directory
node_modules

# Runtime data
tmp

# Examples (If applicable to your project)
examples
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
arrowParens: 'avoid',
bracketSameLine: true,
bracketSpacing: false,
singleQuote: true,
trailingComma: 'all',
};
21 changes: 21 additions & 0 deletions AlertProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, {createContext, useContext} from 'react';
import useAlert from './AsyncAlert';

const AlertContext = createContext();

export const useShowAlert = () => useContext(AlertContext);

const AlertProvider = ({children, Alert}) => {
const {showAlert, AlertComp} = useAlert({
AlertComponent: Alert
});

return (
<AlertContext.Provider value={showAlert}>
{children}
<AlertComp />
</AlertContext.Provider>
);
};

export default AlertProvider;
47 changes: 47 additions & 0 deletions App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import {Button, StyleSheet} from 'react-native';
import AlertProvider, {useShowAlert} from './AlertProvider';

function App() {
const showAlert = useShowAlert();
return (
<Button
title="Show Alet"
onPress={async () => {
await showAlert({
alertData: 'Hellow world',
onEvent: e => {
console.log(e);
},
});
await showAlert({
alertData: 'This is venky',
onEvent: e => {
console.log(e);
},
});
}}
/>
);
}

const styles = StyleSheet.create({
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
},
highlight: {
fontWeight: '700',
},
});

export default App;
78 changes: 78 additions & 0 deletions AsyncAlert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, {useRef} from 'react';
import {useState} from 'react';
import {LogBox, Text} from 'react-native';
import {DefAlert} from './DefAlert';

const useAlert = ({AlertComponent} = {AlertComponent: undefined}) => {
const [visible, setVisible] = useState(false);
const [resolveFn, setResolveFn] = useState(null);
const [title, setTitle] = useState('');
const [text, setText] = useState('');
const [alertData, setAlertData] = useState(null);

const onUserEvent = useRef();

const showAlert = ({title, text, alertData, onEvent}) => {
if (onEvent) {
onUserEvent.current = onEvent;
}

if (!alertData && AlertComponent) {
console.error('You need provide alert data while using a custom alert');
return;
}

if (!AlertComponent && alertData) {
console.warn(
'There is no need to provided alert data while not providing the custom alert in provider',
);
}

if (alertData && AlertComponent) {
setAlertData(alertData);
} else {
setTitle(title);
setText(text);
}

setVisible(true);
return new Promise(resolve => {
setResolveFn(() => resolve);
});
};

const onClose = () => {
setVisible(false);
resolveFn(false);
};

const handleButtonPress = () => {
setVisible(false);
resolveFn(true);
};

const AlertComp = () =>
AlertComponent ? (
AlertComponent({
alertData,
visible,
onEvent: onUserEvent.current,
onClose,
})
) : (
<DefAlert
title={title}
renderBody={() => <Text>{text}</Text>}
onClose={onClose}
onOk={handleButtonPress}
isAlertVisible={visible}
/>
);

return {
showAlert,
AlertComp,
};
};

export default useAlert;
103 changes: 103 additions & 0 deletions DefAlert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import React from 'react';
import {Modal, View, TouchableOpacity, Text, Dimensions} from 'react-native';
import {Spacing} from './Spacing';

export const SCREEN_WIDTH = Dimensions.get('window').width;
export const SCREEN_HEIGHT = Dimensions.get('window').height;

export const DefAlert = ({
isAlertVisible = false,
onClose,
title,
renderBody,
hideCancel,
onOk,
}) => {
return (
<Modal
animated
animationType={'fade'}
visible={isAlertVisible}
onRequestClose={() => onClose()}
transparent
onDismiss={() => onClose()}>
<View
style={{
width: '100%',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'rgba(0, 0, 0, 0.3)',
}}>
<View
style={{
backgroundColor: 'white',
padding: Spacing.SpaceMedium,
width: SCREEN_WIDTH / 1.2,
elevation: Spacing.SpaceMedium,
borderRadius: Spacing.SpaceLarge,
paddingEnd: Spacing.SpaceMedium,
}}>
<Text style={[{color: 'black', fontSize: 20}]}>{title}</Text>
{renderBody ? renderBody() : null}

{hideCancel ? (
<TouchableOpacity
style={{
paddingHorizontal: Spacing.SpaceLarge * 1.5,
paddingVertical: Spacing.SpaceSemiSmall,
borderRadius: Spacing.SpaceSemiSmall,
marginHorizontal: Spacing.SpaceSmall,
backgroundColor: 'white',
alignSelf: 'center',
}}
onPress={onOk}>
<Text
style={{
color: 'white',
paddingHorizontal: Spacing.SpaceSemiSmall,
}}>
Ok
</Text>
</TouchableOpacity>
) : (
<View
style={{
flexDirection: 'row',
alignItems: 'center',
marginTop: Spacing.SpaceSmall,
alignSelf: 'flex-end',
}}>
<TouchableOpacity onPress={onClose}>
<Text
style={{
color: 'black',
fontSize: Spacing.SpaceMedium,
}}>
Cancel
</Text>
</TouchableOpacity>
<TouchableOpacity
style={{
paddingHorizontal: Spacing.SpaceSmall,
paddingVertical: Spacing.SpaceVerySmall,
borderRadius: Spacing.SpaceSemiSmall,
marginStart: Spacing.SpaceMedium,
}}
onPress={onOk}>
<Text
style={{
color: 'black',
paddingHorizontal: Spacing.SpaceSemiSmall,
fontSize: Spacing.SpaceMedium,
}}>
Ok
</Text>
</TouchableOpacity>
</View>
)}
</View>
</View>
</Modal>
);
};
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'

# You may use http://rbenv.org/ or https://rvm.io/ to install and use this version
ruby File.read(File.join(__dir__, '.ruby-version')).strip

gem 'cocoapods', '~> 1.11', '>= 1.11.3'
Loading

0 comments on commit 6974d6c

Please sign in to comment.