Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions Examples/RN0747/.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 Examples/RN0747/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: '@react-native',
};
74 changes: 74 additions & 0 deletions Examples/RN0747/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 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
**/.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
**/Pods/
/vendor/bundle/

# Temporary files created by Metro to check the health of the file watcher
.metro-health-check*

# testing
/coverage

# Yarn
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
7 changes: 7 additions & 0 deletions Examples/RN0747/.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',
};
1 change: 1 addition & 0 deletions Examples/RN0747/.watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
191 changes: 191 additions & 0 deletions Examples/RN0747/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import React, { useCallback, useState } from 'react';
import {
Button,
Platform,
ScrollView,
StatusBar,
Text,
TextInput,
View,
} from 'react-native';
import CodePush, {
ReleaseHistoryInterface,
UpdateCheckRequest,
} from '@bravemobile/react-native-code-push';
import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context';

// Set this to true before run `npx code-push release` to release a new bundle
const IS_RELEASING_BUNDLE = false;

const REACT_NATIVE_VERSION = (() => {
const { major, minor, patch, prerelease } = Platform.constants.reactNativeVersion;
return `${major}.${minor}.${patch}` + (prerelease ? `-${prerelease}` : '');
})();

function App() {
const { top } = useSafeAreaInsets();
const [syncResult, setSyncResult] = useState('');
const [progress, setProgress] = useState(0);
const [runningMetadata, setRunningMetadata] = useState('');
const [pendingMetadata, setPendingMetadata] = useState('');
const [latestMetadata, setLatestMetadata] = useState('');

const handleSync = useCallback(() => {
CodePush.sync(
{},
status => {
setSyncResult(findKeyByValue(CodePush.SyncStatus, status) ?? '');
},
({ receivedBytes, totalBytes }) => {
setProgress(Math.round((receivedBytes / totalBytes) * 100));
},
mismatch => {
console.log('CodePush mismatch', JSON.stringify(mismatch, null, 2));
},
).catch(error => {
console.error(error);
console.log('Sync failed', error.message ?? 'Unknown error');
});
}, []);

const handleMetadata = useCallback(async () => {
const [running, pending, latest] = await Promise.all([
CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING),
CodePush.getUpdateMetadata(CodePush.UpdateState.PENDING),
CodePush.getUpdateMetadata(CodePush.UpdateState.LATEST),
]);
setRunningMetadata(JSON.stringify(running ?? null, null, 2));
setPendingMetadata(JSON.stringify(pending ?? null, null, 2));
setLatestMetadata(JSON.stringify(latest ?? null, null, 2));
}, []);

return (
<View style={{ flex: 1, paddingTop: top, backgroundColor: 'white' }}>
<Text style={{ fontSize: 20, fontWeight: '600' }}>
{`React Native ${REACT_NATIVE_VERSION}`}
</Text>
{IS_RELEASING_BUNDLE && <Text style={{ fontSize: 20, fontWeight: '600' }}>
{'UPDATED!'}
</Text>}

<ScrollView contentContainerStyle={{ padding: 16, gap: 16 }}>
<View style={{ gap: 8 }}>
<Button title="Check for updates" onPress={handleSync} />
<Text>{`Result: ${syncResult}`}</Text>
<Text>{`Progress: ${progress > 0 ? `${progress}%` : ''}`}</Text>
</View>

<View style={{ gap: 8 }}>
<Button
title="Clear updates"
onPress={() => {
CodePush.clearUpdates();
setSyncResult('');
setProgress(0);
}}
/>
<Button title="Restart app" onPress={() => CodePush.restartApp()} />
<Button title="Get update metadata" onPress={handleMetadata} />
<Text>{runningMetadata === '' ? 'METADATA_IDLE' : runningMetadata === 'null' ? 'METADATA_NULL' : `METADATA_V${JSON.parse(runningMetadata).label}`}</Text>
<MetadataBlock label="Running" value={runningMetadata} />
<MetadataBlock label="Pending" value={pendingMetadata} />
<MetadataBlock label="Latest" value={latestMetadata} />
</View>
</ScrollView>
</View>
);
}

function MetadataBlock({
label,
value,
}: {
label: string;
value: string | null | undefined;
}) {
return (
<View style={{ gap: 4 }}>
<Text style={{ fontWeight: '600' }}>{label}</Text>
<TextInput
value={String(value)}
multiline
style={{ borderWidth: 1, borderRadius: 4, padding: 8, minHeight: 60, color: 'black' }}
/>
</View>
);
}

const CODEPUSH_HOST = 'PLACEHOLDER';
const IDENTIFIER = 'RN0747';

async function releaseHistoryFetcher(
updateRequest: UpdateCheckRequest,
): Promise<ReleaseHistoryInterface> {
const jsonFileName = `${updateRequest.app_version}.json`;
const releaseHistoryUrl = `${CODEPUSH_HOST}/histories/${getPlatform()}/${IDENTIFIER}/${jsonFileName}`;

try {
const response = await fetch(releaseHistoryUrl, {
method: 'GET',
headers: {
Accept: 'application/json',
'Cache-Control': 'no-cache',
},
});
if (!response.ok) {
throw new Error(`Failed to fetch release history: ${response.status} ${response.statusText}`);
}
return (await response.json()) as ReleaseHistoryInterface;
} catch (error) {
console.error(error);
throw error;
}
}

function WithSafeAreaProvider() {
return (
<SafeAreaProvider>
<StatusBar barStyle="dark-content" />
<App />
</SafeAreaProvider>
);
}

export default CodePush({
checkFrequency: CodePush.CheckFrequency.MANUAL,
releaseHistoryFetcher,
onUpdateSuccess: label => {
console.log('Update success', label);
},
onUpdateRollback: label => {
console.log('Update rolled back', label);
},
onSyncError: (label, error) => {
console.error(error);
console.log('Sync error', label);
},
onDownloadStart: label => {
console.log('Download start', label);
},
onDownloadSuccess: label => {
console.log('Download success', label);
},
})(WithSafeAreaProvider);

function getPlatform() {
switch (Platform.OS) {
case 'ios':
return 'ios';
case 'android':
return 'android';
default:
throw new Error('Unsupported platform');
}
}

function findKeyByValue(
object: Record<string, unknown>,
value: unknown,
) {
return Object.keys(object).find(key => object[key] === value);
}
11 changes: 11 additions & 0 deletions Examples/RN0747/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'https://rubygems.org'

# You may use http://rbenv.org/ or https://rvm.io/ to install and use this version
ruby ">= 2.6.10"

# Cocoapods 1.15 introduced a bug which break the build. We will remove the upper
# bound in the template on Cocoapods with next React Native release.
gem 'cocoapods', '>= 1.13', '< 1.15'
gem 'activesupport', '>= 6.1.7.5', '< 7.1.0'
gem 'xcodeproj', '< 1.26.0'
gem 'concurrent-ruby', '<= 1.3.4'
Loading