@yuborama/avatar-view 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.eslintrc.js +5 -0
  2. package/README.md +35 -0
  3. package/android/build.gradle +43 -0
  4. package/android/src/main/AndroidManifest.xml +2 -0
  5. package/android/src/main/java/expo/modules/yuboramaavatarview/AvatarUtils.kt +19 -0
  6. package/android/src/main/java/expo/modules/yuboramaavatarview/AvatarView.kt +77 -0
  7. package/android/src/main/java/expo/modules/yuboramaavatarview/AvatarViewModule.kt +52 -0
  8. package/build/AvatarView.d.ts +4 -0
  9. package/build/AvatarView.d.ts.map +1 -0
  10. package/build/AvatarView.js +8 -0
  11. package/build/AvatarView.js.map +1 -0
  12. package/build/AvatarView.types.d.ts +13 -0
  13. package/build/AvatarView.types.d.ts.map +1 -0
  14. package/build/AvatarView.types.js +2 -0
  15. package/build/AvatarView.types.js.map +1 -0
  16. package/build/AvatarView.web.d.ts +4 -0
  17. package/build/AvatarView.web.d.ts.map +1 -0
  18. package/build/AvatarView.web.js +7 -0
  19. package/build/AvatarView.web.js.map +1 -0
  20. package/build/AvatarViewModule.d.ts +10 -0
  21. package/build/AvatarViewModule.d.ts.map +1 -0
  22. package/build/AvatarViewModule.js +4 -0
  23. package/build/AvatarViewModule.js.map +1 -0
  24. package/build/AvatarViewModule.web.d.ts +10 -0
  25. package/build/AvatarViewModule.web.d.ts.map +1 -0
  26. package/build/AvatarViewModule.web.js +12 -0
  27. package/build/AvatarViewModule.web.js.map +1 -0
  28. package/build/index.d.ts +4 -0
  29. package/build/index.d.ts.map +1 -0
  30. package/build/index.js +6 -0
  31. package/build/index.js.map +1 -0
  32. package/expo-module.config.json +9 -0
  33. package/ios/AvatarView.podspec +29 -0
  34. package/ios/AvatarView.swift +79 -0
  35. package/ios/AvatarViewModule.swift +49 -0
  36. package/package.json +43 -0
  37. package/src/AvatarView.tsx +17 -0
  38. package/src/AvatarView.types.ts +15 -0
  39. package/src/AvatarView.web.tsx +11 -0
  40. package/src/AvatarViewModule.ts +12 -0
  41. package/src/AvatarViewModule.web.ts +15 -0
  42. package/src/index.ts +5 -0
  43. package/tsconfig.json +9 -0
package/.eslintrc.js ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ['universe/native', 'universe/web'],
4
+ ignorePatterns: ['build'],
5
+ };
package/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # @yuborama/avatar-view
2
+
3
+ My new module
4
+
5
+ # API documentation
6
+
7
+ - [Documentation for the latest stable release](https://docs.expo.dev/versions/latest/sdk/@yuborama/avatar-view/)
8
+ - [Documentation for the main branch](https://docs.expo.dev/versions/unversioned/sdk/@yuborama/avatar-view/)
9
+
10
+ # Installation in managed Expo projects
11
+
12
+ For [managed](https://docs.expo.dev/archive/managed-vs-bare/) Expo projects, please follow the installation instructions in the [API documentation for the latest stable release](#api-documentation). If you follow the link and there is no documentation available then this library is not yet usable within managed projects — it is likely to be included in an upcoming Expo SDK release.
13
+
14
+ # Installation in bare React Native projects
15
+
16
+ For bare React Native projects, you must ensure that you have [installed and configured the `expo` package](https://docs.expo.dev/bare/installing-expo-modules/) before continuing.
17
+
18
+ ### Add the package to your npm dependencies
19
+
20
+ ```
21
+ npm install @yuborama/avatar-view
22
+ ```
23
+
24
+ ### Configure for Android
25
+
26
+
27
+
28
+
29
+ ### Configure for iOS
30
+
31
+ Run `npx pod-install` after installing the npm package.
32
+
33
+ # Contributing
34
+
35
+ Contributions are very welcome! Please refer to guidelines described in the [contributing guide]( https://github.com/expo/expo#contributing).
@@ -0,0 +1,43 @@
1
+ apply plugin: 'com.android.library'
2
+
3
+ group = 'expo.modules.yuboramaavatarview'
4
+ version = '0.1.0'
5
+
6
+ def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
7
+ apply from: expoModulesCorePlugin
8
+ applyKotlinExpoModulesCorePlugin()
9
+ useCoreDependencies()
10
+ useExpoPublishing()
11
+
12
+ // If you want to use the managed Android SDK versions from expo-modules-core, set this to true.
13
+ // The Android SDK versions will be bumped from time to time in SDK releases and may introduce breaking changes in your module code.
14
+ // Most of the time, you may like to manage the Android SDK versions yourself.
15
+ def useManagedAndroidSdkVersions = false
16
+ if (useManagedAndroidSdkVersions) {
17
+ useDefaultAndroidSdkVersions()
18
+ } else {
19
+ buildscript {
20
+ // Simple helper that allows the root project to override versions declared by this library.
21
+ ext.safeExtGet = { prop, fallback ->
22
+ rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
23
+ }
24
+ }
25
+ project.android {
26
+ compileSdkVersion safeExtGet("compileSdkVersion", 36)
27
+ defaultConfig {
28
+ minSdkVersion safeExtGet("minSdkVersion", 24)
29
+ targetSdkVersion safeExtGet("targetSdkVersion", 36)
30
+ }
31
+ }
32
+ }
33
+
34
+ android {
35
+ namespace "expo.modules.yuboramaavatarview"
36
+ defaultConfig {
37
+ versionCode 1
38
+ versionName "0.1.0"
39
+ }
40
+ lintOptions {
41
+ abortOnError false
42
+ }
43
+ }
@@ -0,0 +1,2 @@
1
+ <manifest>
2
+ </manifest>
@@ -0,0 +1,19 @@
1
+ package expo.modules.yuboramaavatarview
2
+
3
+ import android.graphics.Color
4
+ import kotlin.random.Random
5
+
6
+ fun getInitialsFromName(name: String): String {
7
+ if (name.isBlank()) return ""
8
+
9
+ val parts = name.trim().split(" ").filter { it.isNotBlank() }
10
+ val first = parts.getOrNull(0)?.firstOrNull()?.toString() ?: ""
11
+ val second = parts.getOrNull(1)?.firstOrNull()?.toString() ?: ""
12
+
13
+ return (first + second).uppercase()
14
+ }
15
+
16
+ fun generateRandomColor(): Int {
17
+ val hue = Random.nextFloat() * 360f
18
+ return Color.HSVToColor(floatArrayOf(hue, 0.55f, 0.78f))
19
+ }
@@ -0,0 +1,77 @@
1
+ package expo.modules.yuboramaavatarview
2
+
3
+ import android.content.Context
4
+ import android.graphics.Color
5
+ import android.graphics.Typeface
6
+ import android.graphics.drawable.GradientDrawable
7
+ import android.view.Gravity
8
+ import android.util.TypedValue
9
+ import androidx.appcompat.widget.AppCompatTextView
10
+ import expo.modules.kotlin.AppContext
11
+ import expo.modules.kotlin.views.ExpoView
12
+ import kotlin.math.max
13
+
14
+ class AvatarView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {
15
+ private var avatarSizeDp: Float = 120f
16
+ private var avatarColor: Int = generateRandomColor()
17
+
18
+ private val initialsView = AppCompatTextView(context).apply {
19
+ gravity = Gravity.CENTER
20
+ setTextColor(Color.WHITE)
21
+ typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
22
+ textSize = 38f
23
+ }
24
+
25
+ var name: String = ""
26
+ set(value) {
27
+ field = value
28
+ avatarColor = generateRandomColor()
29
+ updateAppearance()
30
+ }
31
+
32
+ var size: Float = 120f
33
+ set(value) {
34
+ field = if (value <= 0f) 120f else value
35
+ avatarSizeDp = field
36
+ updateCircleSize()
37
+ updateAppearance()
38
+ }
39
+
40
+ init {
41
+ addView(initialsView, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
42
+ gravity = Gravity.CENTER
43
+ })
44
+ updateAppearance()
45
+ }
46
+
47
+ override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
48
+ super.onSizeChanged(w, h, oldw, oldh)
49
+ updateCircleSize()
50
+ }
51
+
52
+ private fun updateCircleSize() {
53
+ val side = dpToPx(avatarSizeDp)
54
+ initialsView.layoutParams = (initialsView.layoutParams as LayoutParams).apply {
55
+ this.width = side
56
+ this.height = side
57
+ gravity = Gravity.CENTER
58
+ }
59
+ initialsView.setTextSize(TypedValue.COMPLEX_UNIT_PX, side / 3f)
60
+ }
61
+
62
+ private fun updateAppearance() {
63
+ initialsView.text = getInitialsFromName(name)
64
+ initialsView.background = GradientDrawable().apply {
65
+ shape = GradientDrawable.OVAL
66
+ setColor(avatarColor)
67
+ setStroke(max(dpToPx(4f), (dpToPx(avatarSizeDp) * 0.05f).toInt()), Color.WHITE)
68
+ }
69
+ }
70
+
71
+ private fun dpToPx(dp: Float): Int =
72
+ TypedValue.applyDimension(
73
+ TypedValue.COMPLEX_UNIT_DIP,
74
+ dp,
75
+ context.resources.displayMetrics
76
+ ).toInt()
77
+ }
@@ -0,0 +1,52 @@
1
+ package expo.modules.yuboramaavatarview
2
+
3
+ import expo.modules.kotlin.modules.Module
4
+ import expo.modules.kotlin.modules.ModuleDefinition
5
+
6
+ class AvatarViewModule : Module() {
7
+ // Each module class must implement the definition function. The definition consists of components
8
+ // that describes the module's functionality and behavior.
9
+ // See https://docs.expo.dev/modules/module-api for more details about available components.
10
+ override fun definition() = ModuleDefinition {
11
+ // Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument.
12
+ // Can be inferred from module's class name, but it's recommended to set it explicitly for clarity.
13
+ // The module will be accessible from `requireNativeModule('AvatarView')` in JavaScript.
14
+ Name("AvatarView")
15
+
16
+ // Defines constant property on the module.
17
+ Constant("PI") {
18
+ Math.PI
19
+ }
20
+
21
+ // Defines event names that the module can send to JavaScript.
22
+ Events("onChange")
23
+
24
+ // Defines a JavaScript synchronous function that runs the native code on the JavaScript thread.
25
+ Function("hello") {
26
+ "Hello world! 👋"
27
+ }
28
+
29
+ // Defines a JavaScript function that always returns a Promise and whose native code
30
+ // is by default dispatched on the different thread than the JavaScript runtime runs on.
31
+ AsyncFunction("setValueAsync") { value: String ->
32
+ // Send an event to JavaScript.
33
+ sendEvent("onChange", mapOf(
34
+ "value" to value
35
+ ))
36
+ }
37
+
38
+ // Enables the module to be used as a native view. Definition components that are accepted as part of
39
+ // the view definition: Prop, Events.
40
+ View(AvatarView::class) {
41
+ // Defines a setter for the `name` prop.
42
+ Prop("name") { view: AvatarView, name: String ->
43
+ view.name = name
44
+ }
45
+
46
+ // Defines a setter for the `size` prop.
47
+ Prop("size") { view: AvatarView, size: Float ->
48
+ view.size = size
49
+ }
50
+ }
51
+ }
52
+ }
@@ -0,0 +1,4 @@
1
+ import * as React from "react";
2
+ import { AvatarViewProps } from "./AvatarView.types";
3
+ export default function AvatarView(props: AvatarViewProps): React.JSX.Element;
4
+ //# sourceMappingURL=AvatarView.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarView.d.ts","sourceRoot":"","sources":["../src/AvatarView.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAKrD,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,KAAK,EAAE,eAAe,qBAQxD"}
@@ -0,0 +1,8 @@
1
+ import { requireNativeView } from "expo";
2
+ import * as React from "react";
3
+ const NativeView = requireNativeView("AvatarView");
4
+ export default function AvatarView(props) {
5
+ const size = props.size ?? 120;
6
+ return (<NativeView {...props} style={[{ width: size, height: size }, props.style]}/>);
7
+ }
8
+ //# sourceMappingURL=AvatarView.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarView.js","sourceRoot":"","sources":["../src/AvatarView.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAI/B,MAAM,UAAU,GACd,iBAAiB,CAAC,YAAY,CAAC,CAAC;AAElC,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,KAAsB;IACvD,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,IAAI,GAAG,CAAC;IAC/B,OAAO,CACL,CAAC,UAAU,CACT,IAAI,KAAK,CAAC,CACV,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,EACpD,CACH,CAAC;AACJ,CAAC","sourcesContent":["import { requireNativeView } from \"expo\";\nimport * as React from \"react\";\n\nimport { AvatarViewProps } from \"./AvatarView.types\";\n\nconst NativeView: React.ComponentType<AvatarViewProps> =\n requireNativeView(\"AvatarView\");\n\nexport default function AvatarView(props: AvatarViewProps) {\n const size = props.size ?? 120;\n return (\n <NativeView\n {...props}\n style={[{ width: size, height: size }, props.style]}\n />\n );\n}"]}
@@ -0,0 +1,13 @@
1
+ import type { StyleProp, ViewStyle } from 'react-native';
2
+ export type AvatarViewModuleEvents = {
3
+ onChange: (params: ChangeEventPayload) => void;
4
+ };
5
+ export type ChangeEventPayload = {
6
+ value: string;
7
+ };
8
+ export type AvatarViewProps = {
9
+ name: string;
10
+ size?: number;
11
+ style?: StyleProp<ViewStyle>;
12
+ };
13
+ //# sourceMappingURL=AvatarView.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarView.types.d.ts","sourceRoot":"","sources":["../src/AvatarView.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzD,MAAM,MAAM,sBAAsB,GAAG;IACnC,QAAQ,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,IAAI,CAAC;CAChD,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;CAC9B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=AvatarView.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarView.types.js","sourceRoot":"","sources":["../src/AvatarView.types.ts"],"names":[],"mappings":"","sourcesContent":["import type { StyleProp, ViewStyle } from 'react-native';\n\nexport type AvatarViewModuleEvents = {\n onChange: (params: ChangeEventPayload) => void;\n};\n\nexport type ChangeEventPayload = {\n value: string;\n};\n\nexport type AvatarViewProps = {\n name: string;\n size?: number;\n style?: StyleProp<ViewStyle>;\n};"]}
@@ -0,0 +1,4 @@
1
+ import * as React from 'react';
2
+ import { AvatarViewProps } from './AvatarView.types';
3
+ export default function AvatarView(props: AvatarViewProps): React.JSX.Element;
4
+ //# sourceMappingURL=AvatarView.web.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarView.web.d.ts","sourceRoot":"","sources":["../src/AvatarView.web.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,KAAK,EAAE,eAAe,qBAMxD"}
@@ -0,0 +1,7 @@
1
+ import * as React from 'react';
2
+ export default function AvatarView(props) {
3
+ return (<div>
4
+
5
+ </div>);
6
+ }
7
+ //# sourceMappingURL=AvatarView.web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarView.web.js","sourceRoot":"","sources":["../src/AvatarView.web.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAI/B,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,KAAsB;IACvD,OAAO,CACL,CAAC,GAAG,CAEJ;;IAAA,EAAE,GAAG,CAAC,CACP,CAAC;AACJ,CAAC","sourcesContent":["import * as React from 'react';\n\nimport { AvatarViewProps } from './AvatarView.types';\n\nexport default function AvatarView(props: AvatarViewProps) {\n return (\n <div>\n \n </div>\n );\n}\n"]}
@@ -0,0 +1,10 @@
1
+ import { NativeModule } from 'expo';
2
+ import { AvatarViewModuleEvents } from './AvatarView.types';
3
+ declare class AvatarViewModule extends NativeModule<AvatarViewModuleEvents> {
4
+ PI: number;
5
+ hello(): string;
6
+ setValueAsync(value: string): Promise<void>;
7
+ }
8
+ declare const _default: AvatarViewModule;
9
+ export default _default;
10
+ //# sourceMappingURL=AvatarViewModule.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarViewModule.d.ts","sourceRoot":"","sources":["../src/AvatarViewModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAuB,MAAM,MAAM,CAAC;AAEzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,OAAO,OAAO,gBAAiB,SAAQ,YAAY,CAAC,sBAAsB,CAAC;IACzE,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,IAAI,MAAM;IACf,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAC5C;;AAGD,wBAAmE"}
@@ -0,0 +1,4 @@
1
+ import { requireNativeModule } from 'expo';
2
+ // This call loads the native module object from the JSI.
3
+ export default requireNativeModule('AvatarView');
4
+ //# sourceMappingURL=AvatarViewModule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarViewModule.js","sourceRoot":"","sources":["../src/AvatarViewModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,mBAAmB,EAAE,MAAM,MAAM,CAAC;AAUzD,yDAAyD;AACzD,eAAe,mBAAmB,CAAmB,YAAY,CAAC,CAAC","sourcesContent":["import { NativeModule, requireNativeModule } from 'expo';\n\nimport { AvatarViewModuleEvents } from './AvatarView.types';\n\ndeclare class AvatarViewModule extends NativeModule<AvatarViewModuleEvents> {\n PI: number;\n hello(): string;\n setValueAsync(value: string): Promise<void>;\n}\n\n// This call loads the native module object from the JSI.\nexport default requireNativeModule<AvatarViewModule>('AvatarView');\n"]}
@@ -0,0 +1,10 @@
1
+ import { NativeModule } from 'expo';
2
+ import { AvatarViewModuleEvents } from './AvatarView.types';
3
+ declare class AvatarViewModule extends NativeModule<AvatarViewModuleEvents> {
4
+ PI: number;
5
+ setValueAsync(value: string): Promise<void>;
6
+ hello(): string;
7
+ }
8
+ declare const _default: typeof AvatarViewModule;
9
+ export default _default;
10
+ //# sourceMappingURL=AvatarViewModule.web.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarViewModule.web.d.ts","sourceRoot":"","sources":["../src/AvatarViewModule.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,YAAY,EAAE,MAAM,MAAM,CAAC;AAEvD,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,cAAM,gBAAiB,SAAQ,YAAY,CAAC,sBAAsB,CAAC;IACjE,EAAE,SAAW;IACP,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAGjD,KAAK;CAGN;;AAED,wBAAuE"}
@@ -0,0 +1,12 @@
1
+ import { registerWebModule, NativeModule } from 'expo';
2
+ class AvatarViewModule extends NativeModule {
3
+ PI = Math.PI;
4
+ async setValueAsync(value) {
5
+ this.emit('onChange', { value });
6
+ }
7
+ hello() {
8
+ return 'Hello world! 👋';
9
+ }
10
+ }
11
+ export default registerWebModule(AvatarViewModule, 'AvatarViewModule');
12
+ //# sourceMappingURL=AvatarViewModule.web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AvatarViewModule.web.js","sourceRoot":"","sources":["../src/AvatarViewModule.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAIvD,MAAM,gBAAiB,SAAQ,YAAoC;IACjE,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC;IACb,KAAK,CAAC,aAAa,CAAC,KAAa;QAC/B,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IACnC,CAAC;IACD,KAAK;QACH,OAAO,iBAAiB,CAAC;IAC3B,CAAC;CACF;AAED,eAAe,iBAAiB,CAAC,gBAAgB,EAAE,kBAAkB,CAAC,CAAC","sourcesContent":["import { registerWebModule, NativeModule } from 'expo';\n\nimport { AvatarViewModuleEvents } from './AvatarView.types';\n\nclass AvatarViewModule extends NativeModule<AvatarViewModuleEvents> {\n PI = Math.PI;\n async setValueAsync(value: string): Promise<void> {\n this.emit('onChange', { value });\n }\n hello() {\n return 'Hello world! 👋';\n }\n}\n\nexport default registerWebModule(AvatarViewModule, 'AvatarViewModule');\n"]}
@@ -0,0 +1,4 @@
1
+ export { default } from './AvatarViewModule';
2
+ export { default as AvatarView } from './AvatarView';
3
+ export * from './AvatarView.types';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC;AACrD,cAAe,oBAAoB,CAAC"}
package/build/index.js ADDED
@@ -0,0 +1,6 @@
1
+ // Reexport the native module. On web, it will be resolved to AvatarViewModule.web.ts
2
+ // and on native platforms to AvatarViewModule.ts
3
+ export { default } from './AvatarViewModule';
4
+ export { default as AvatarView } from './AvatarView';
5
+ export * from './AvatarView.types';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,iDAAiD;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC;AACrD,cAAe,oBAAoB,CAAC","sourcesContent":["// Reexport the native module. On web, it will be resolved to AvatarViewModule.web.ts\n// and on native platforms to AvatarViewModule.ts\nexport { default } from './AvatarViewModule';\nexport { default as AvatarView } from './AvatarView';\nexport * from './AvatarView.types';\n"]}
@@ -0,0 +1,9 @@
1
+ {
2
+ "platforms": ["apple", "android", "web"],
3
+ "apple": {
4
+ "modules": ["AvatarViewModule"]
5
+ },
6
+ "android": {
7
+ "modules": ["expo.modules.yuboramaavatarview.AvatarViewModule"]
8
+ }
9
+ }
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = 'AvatarView'
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.description = package['description']
10
+ s.license = package['license']
11
+ s.author = package['author']
12
+ s.homepage = package['homepage']
13
+ s.platforms = {
14
+ :ios => '15.1',
15
+ :tvos => '15.1'
16
+ }
17
+ s.swift_version = '5.9'
18
+ s.source = { git: 'https://github.com/yuborama/yuborama-avatar-view' }
19
+ s.static_framework = true
20
+
21
+ s.dependency 'ExpoModulesCore'
22
+
23
+ # Swift/Objective-C compatibility
24
+ s.pod_target_xcconfig = {
25
+ 'DEFINES_MODULE' => 'YES',
26
+ }
27
+
28
+ s.source_files = "**/*.{h,m,mm,swift,hpp,cpp}"
29
+ end
@@ -0,0 +1,79 @@
1
+ import ExpoModulesCore
2
+ import UIKit
3
+
4
+ class AvatarView: ExpoView {
5
+ private var avatarSize: CGFloat = 120
6
+ private var avatarColor: UIColor = .systemGray
7
+ private let avatarContainer = UIView()
8
+ private let initialsLabel = UILabel()
9
+ var name: String = "" {
10
+ didSet {
11
+ avatarColor = generateRandomColor()
12
+ updateAppearance()
13
+ }
14
+ }
15
+ var size: CGFloat = 120 {
16
+ didSet {
17
+ avatarSize = size > 0 ? size : 120
18
+ setNeedsLayout()
19
+ updateAppearance()
20
+ }
21
+ }
22
+
23
+ required init(appContext: AppContext? = nil) {
24
+ super.init(appContext: appContext)
25
+ clipsToBounds = false
26
+
27
+ avatarContainer.clipsToBounds = true
28
+ avatarContainer.layer.borderWidth = 6
29
+ avatarContainer.layer.borderColor = UIColor.white.cgColor
30
+ initialsLabel.textAlignment = .center
31
+ initialsLabel.textColor = .white
32
+ initialsLabel.font = .systemFont(ofSize: 22, weight: .regular)
33
+
34
+ avatarContainer.addSubview(initialsLabel)
35
+ addSubview(avatarContainer)
36
+ avatarColor = generateRandomColor()
37
+ updateAppearance()
38
+ }
39
+
40
+ override func layoutSubviews() {
41
+ super.layoutSubviews()
42
+ avatarContainer.frame = CGRect(
43
+ x: (bounds.width - avatarSize) / 2,
44
+ y: (bounds.height - avatarSize) / 2,
45
+ width: avatarSize,
46
+ height: avatarSize
47
+ )
48
+ avatarContainer.layer.cornerRadius = avatarSize / 2
49
+ initialsLabel.frame = avatarContainer.bounds
50
+ initialsLabel.font = .systemFont(ofSize: avatarSize / 3, weight: .regular)
51
+ }
52
+
53
+ private func updateAppearance() {
54
+ initialsLabel.text = getInitialsFromName(name)
55
+ avatarContainer.backgroundColor = avatarColor
56
+ avatarContainer.layer.borderWidth = max(4, avatarSize * 0.05)
57
+ }
58
+
59
+ private func getInitialsFromName(_ name: String) -> String {
60
+ if name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
61
+ return ""
62
+ }
63
+
64
+ let parts = name
65
+ .trimmingCharacters(in: .whitespacesAndNewlines)
66
+ .components(separatedBy: " ")
67
+ .filter { !$0.isEmpty }
68
+
69
+ let first = parts.first?.prefix(1) ?? ""
70
+ let second = parts.count > 1 ? parts[1].prefix(1) : ""
71
+
72
+ return "\(first)\(second)".uppercased()
73
+ }
74
+
75
+ private func generateRandomColor() -> UIColor {
76
+ let hue = CGFloat.random(in: 0...1)
77
+ return UIColor(hue: hue, saturation: 0.55, brightness: 0.78, alpha: 1.0)
78
+ }
79
+ }
@@ -0,0 +1,49 @@
1
+ import ExpoModulesCore
2
+
3
+ public class AvatarViewModule: Module {
4
+ // Each module class must implement the definition function. The definition consists of components
5
+ // that describes the module's functionality and behavior.
6
+ // See https://docs.expo.dev/modules/module-api for more details about available components.
7
+ public func definition() -> ModuleDefinition {
8
+ // Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument.
9
+ // Can be inferred from module's class name, but it's recommended to set it explicitly for clarity.
10
+ // The module will be accessible from `requireNativeModule('AvatarView')` in JavaScript.
11
+ Name("AvatarView")
12
+
13
+ // Defines constant property on the module.
14
+ Constant("PI") {
15
+ Double.pi
16
+ }
17
+
18
+ // Defines event names that the module can send to JavaScript.
19
+ Events("onChange")
20
+
21
+ // Defines a JavaScript synchronous function that runs the native code on the JavaScript thread.
22
+ Function("hello") {
23
+ return "Hello world! 👋"
24
+ }
25
+
26
+ // Defines a JavaScript function that always returns a Promise and whose native code
27
+ // is by default dispatched on the different thread than the JavaScript runtime runs on.
28
+ AsyncFunction("setValueAsync") { (value: String) in
29
+ // Send an event to JavaScript.
30
+ self.sendEvent("onChange", [
31
+ "value": value
32
+ ])
33
+ }
34
+
35
+ // Enables the module to be used as a native view. Definition components that are accepted as part of the
36
+ // view definition: Prop, Events.
37
+ View(AvatarView.self) {
38
+ // Defines a setter for the `name` prop.
39
+ Prop("name") { (view: AvatarView, name: String) in
40
+ view.name = name
41
+ }
42
+
43
+ // Defines a setter for the `size` prop.
44
+ Prop("size") { (view: AvatarView, size: Double) in
45
+ view.size = CGFloat(size)
46
+ }
47
+ }
48
+ }
49
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@yuborama/avatar-view",
3
+ "version": "0.2.0",
4
+ "description": "My new module",
5
+ "main": "build/index.js",
6
+ "types": "build/index.d.ts",
7
+ "scripts": {
8
+ "build": "expo-module build",
9
+ "clean": "expo-module clean",
10
+ "lint": "expo-module lint",
11
+ "test": "expo-module test",
12
+ "prepare": "expo-module prepare",
13
+ "prepublishOnly": "expo-module prepublishOnly",
14
+ "expo-module": "expo-module",
15
+ "open:ios": "xed example/ios",
16
+ "open:android": "open -a \"Android Studio\" example/android"
17
+ },
18
+ "keywords": [
19
+ "react-native",
20
+ "expo",
21
+ "@yuborama/avatar-view",
22
+ "AvatarView"
23
+ ],
24
+ "repository": "https://github.com/yuborama/yuborama-avatar-view",
25
+ "bugs": {
26
+ "url": "https://github.com/yuborama/yuborama-avatar-view/issues"
27
+ },
28
+ "author": "Yuborama <davidjohan2@hotmail.com> (https://github.com/yuborama)",
29
+ "license": "MIT",
30
+ "homepage": "https://github.com/yuborama/yuborama-avatar-view#readme",
31
+ "dependencies": {},
32
+ "devDependencies": {
33
+ "@types/react": "~19.1.1",
34
+ "expo-module-scripts": "^55.0.2",
35
+ "expo": "^55.0.1",
36
+ "react-native": "0.82.1"
37
+ },
38
+ "peerDependencies": {
39
+ "expo": "*",
40
+ "react": "*",
41
+ "react-native": "*"
42
+ }
43
+ }
@@ -0,0 +1,17 @@
1
+ import { requireNativeView } from "expo";
2
+ import * as React from "react";
3
+
4
+ import { AvatarViewProps } from "./AvatarView.types";
5
+
6
+ const NativeView: React.ComponentType<AvatarViewProps> =
7
+ requireNativeView("AvatarView");
8
+
9
+ export default function AvatarView(props: AvatarViewProps) {
10
+ const size = props.size ?? 120;
11
+ return (
12
+ <NativeView
13
+ {...props}
14
+ style={[{ width: size, height: size }, props.style]}
15
+ />
16
+ );
17
+ }
@@ -0,0 +1,15 @@
1
+ import type { StyleProp, ViewStyle } from 'react-native';
2
+
3
+ export type AvatarViewModuleEvents = {
4
+ onChange: (params: ChangeEventPayload) => void;
5
+ };
6
+
7
+ export type ChangeEventPayload = {
8
+ value: string;
9
+ };
10
+
11
+ export type AvatarViewProps = {
12
+ name: string;
13
+ size?: number;
14
+ style?: StyleProp<ViewStyle>;
15
+ };
@@ -0,0 +1,11 @@
1
+ import * as React from 'react';
2
+
3
+ import { AvatarViewProps } from './AvatarView.types';
4
+
5
+ export default function AvatarView(props: AvatarViewProps) {
6
+ return (
7
+ <div>
8
+
9
+ </div>
10
+ );
11
+ }
@@ -0,0 +1,12 @@
1
+ import { NativeModule, requireNativeModule } from 'expo';
2
+
3
+ import { AvatarViewModuleEvents } from './AvatarView.types';
4
+
5
+ declare class AvatarViewModule extends NativeModule<AvatarViewModuleEvents> {
6
+ PI: number;
7
+ hello(): string;
8
+ setValueAsync(value: string): Promise<void>;
9
+ }
10
+
11
+ // This call loads the native module object from the JSI.
12
+ export default requireNativeModule<AvatarViewModule>('AvatarView');
@@ -0,0 +1,15 @@
1
+ import { registerWebModule, NativeModule } from 'expo';
2
+
3
+ import { AvatarViewModuleEvents } from './AvatarView.types';
4
+
5
+ class AvatarViewModule extends NativeModule<AvatarViewModuleEvents> {
6
+ PI = Math.PI;
7
+ async setValueAsync(value: string): Promise<void> {
8
+ this.emit('onChange', { value });
9
+ }
10
+ hello() {
11
+ return 'Hello world! 👋';
12
+ }
13
+ }
14
+
15
+ export default registerWebModule(AvatarViewModule, 'AvatarViewModule');
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ // Reexport the native module. On web, it will be resolved to AvatarViewModule.web.ts
2
+ // and on native platforms to AvatarViewModule.ts
3
+ export { default } from './AvatarViewModule';
4
+ export { default as AvatarView } from './AvatarView';
5
+ export * from './AvatarView.types';
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ // @generated by expo-module-scripts
2
+ {
3
+ "extends": "expo-module-scripts/tsconfig.base",
4
+ "compilerOptions": {
5
+ "outDir": "./build"
6
+ },
7
+ "include": ["./src"],
8
+ "exclude": ["**/__mocks__/*", "**/__tests__/*", "**/__rsc_tests__/*"]
9
+ }