Skip to content

Commit

Permalink
support new arch + android changes
Browse files Browse the repository at this point in the history
  • Loading branch information
skb1129 committed Aug 19, 2023
1 parent 55e6d86 commit f9e98e8
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 26 deletions.
11 changes: 11 additions & 0 deletions NativeChangeIcon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
readonly getConstants: () => {};
changeIcon: (iconName?: string) => Promise<string>;
resetIcon: () => Promise<string>;
getIcon: () => Promise<string>;
}

export default TurboModuleRegistry.get<Spec>('ChangeIcon');
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@
**Android 🤖**
1. Add an alias for each of your new icons within the `AndroidManifest.xml` (within `<application>`).
- Make sure these have the properties as shown below.
- For the name prefix it `MainActivity...` followed by the name you will use to reference your icon. e.g. for our light icon we will use `com.myapp.MainActivityLight` (replace `com.myapp` with your package name)
- Create an alias for `.MainActivityDefault` as well but for this, set `android:enabled="true"`.
- For the name prefix it `.MainActivity...` followed by the name you will use to reference your icon. e.g. for our light icon we will use `.MainActivityLight`
2. You'll have to remove the `LAUNCHER` intent filter from the main `<activity>` as we have added the launcher in `.MainActivityDefault`.


<div align="center">
Expand All @@ -134,7 +136,7 @@

```xml
<activity-alias
android:name="com.myapp.MainActivityLight"
android:name=".MainActivityLight"
android:enabled="false"
android:exported="true"
android:icon="@mipmap/ic_launcher_light"
Expand All @@ -151,8 +153,12 @@
2. Within this dictionary add another key for `CFBundleAlternateIcons`
3. Finally then within this dictionary you can add in the keys for you new icons
- The `key` is the name you will reference from within code.
- Set the first array item to the name of the `.appiconset` we created earlier.
- Set the first array item to the name of the `.appiconset` we created earlier.
4. In XCode, in your app's `General` settings, under `App Icons and Launch Screen`, set "App Icon" to `Default` and check the "Include all app icon assets" checkbox below.

<div align="center">
<img src="docs/ios-example-app-icon.png" alt="Example App Icons and Launch Screen">
</div>
<div align="center">
<a href="./docs/examples/Step-4/Info.plist">Example Info.plist</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
import com.facebook.react.module.annotations.ReactModule;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@ReactModule(name = "ChangeIcon")
public class ChangeIconModule extends ReactContextBaseJavaModule implements Application.ActivityLifecycleCallbacks {
public static final String NAME = "ChangeIcon";
private final String packageName;
private List<String> classesToKill = new ArrayList<>();
private final List<String> classesToKill = new ArrayList<>();
private Boolean iconChanged = false;
private String componentClass = "";

Expand Down Expand Up @@ -69,10 +68,10 @@ public void changeIcon(String iconName, Promise promise) {
return;
}
if (this.componentClass.isEmpty()) {
this.componentClass = activity.getComponentName().getClassName();
this.componentClass = activityName.endsWith("MainActivity") ? activityName + "Default" : activityName;
}

final String newIconName = (iconName == null || iconName.isEmpty() || iconName.equals("Default")) ? "" : iconName;
final String newIconName = (iconName == null || iconName.isEmpty()) ? "Default" : iconName;
final String activeClass = this.packageName + ".MainActivity" + newIconName;
if (this.componentClass.equals(activeClass)) {
promise.reject("ANDROID:ICON_ALREADY_USED:" + this.componentClass);
Expand All @@ -83,7 +82,7 @@ public void changeIcon(String iconName, Promise promise) {
new ComponentName(this.packageName, activeClass),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
promise.resolve(newIconName.isEmpty() ? "Default" : newIconName);
promise.resolve(newIconName);
} catch (Exception e) {
promise.reject("ANDROID:ICON_INVALID");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.List;

public class ChangeIconPackage implements ReactPackage {
private String packageName;
private final String packageName;

public ChangeIconPackage(String packageName) {
this.packageName = packageName;
Expand Down
36 changes: 22 additions & 14 deletions docs/examples/Step-4/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
android:exported="true"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize"
android:exported="true">
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity-alias
android:name="com.myCompany.myApp.MainActivityLight"
<activity-alias
android:name=".MainActivityDefault"
android:enabled="true"
android:exported="true"
android:icon="@mipmap/ic_launcher"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias
android:name=".MainActivityLight"
android:enabled="false"
android:exported="true"
android:icon="@mipmap/ic_launcher_light"
Expand All @@ -32,9 +41,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>

<activity-alias
android:name="com.myCompany.myApp.MainActivityDark"
<activity-alias
android:name=".MainActivityDark"
android:enabled="false"
android:exported="true"
android:icon="@mipmap/ic_launcher_dark"
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"android",
"ios",
"dist/**/*",
"NativeChangeIcon.ts",
"react-native-change-icon.podspec",
"react-native.config.js",
"README.md"
Expand Down Expand Up @@ -41,5 +42,13 @@
"peerDependencies": {
"react-native": "*"
},
"packageManager": "^[email protected]"
"packageManager": "^[email protected]",
"codegenConfig": {
"name": "RNChangeIconSpec",
"type": "all",
"jsSrcsDir": ".",
"android": {
"javaPackageName": "com.facebook.fbreact.specs"
}
}
}
2 changes: 1 addition & 1 deletion react-native-change-icon.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pod::Spec.new do |s|
s.license = package["license"]
s.authors = package["author"]

s.platforms = { :ios => "11.0" }
s.platforms = { :ios => "12.0" }
s.source = { :git => "https://github.com/skb1129/react-native-change-icon.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,m,mm}"
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"strict": true,
"target": "esnext",
"verbatimModuleSyntax": true
}
},
"files": ["./src/index.ts"]
}

0 comments on commit f9e98e8

Please sign in to comment.