-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/android controller functions #3
Merged
theashraf
merged 22 commits into
LottieFiles:main
from
shamsudeeen-yusuf:feat/android-controller-functions
Oct 8, 2024
Merged
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
72ff7e6
chore: initial commit
elasoshi 0added6
chore: basic dotlottie configuration for android
elasoshi 1f0c7bb
chore: relating branches
elasoshi 816407f
chore: fixed readme
elasoshi 903a1ba
Merge branch 'main' of github.com-toptal:shamsudeeen-yusuf/dotlottie-…
elasoshi e3232aa
Merge branch 'chore/project-config'
elasoshi 4f7d111
feat: android functions
elasoshi 0d6e7b2
feat: android dotlottie controller functions
elasoshi 91a4c4a
Merge branch 'LottieFiles:main' into main
shamsudeeen-yusuf 425eec2
Merge branch 'main' of github.com-toptal:shamsudeeen-yusuf/dotlottie-…
elasoshi ed6bea4
feat: listeners update
elasoshi c92b682
chore: completed android functions and props
elasoshi ef84cfb
chore: pr fixes
elasoshi f9489df
chore: ci fixes
elasoshi fd520e3
chore: ci fixes
elasoshi 41593cf
chore: ci fixes
elasoshi cf9d42f
feat: ios function manually
elasoshi bdeaae4
feat: ios function manually
elasoshi 3c01cfb
feat: ios functions
elasoshi 255dcff
feat: more ios functions
elasoshi 83bfddd
feat: state machine events
elasoshi f8b03d1
fix: ci fix
elasoshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
android/src/main/java/com/dotlottiereactnative/DotlottieReactNativeView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.dotlottiereactnative | ||
|
||
import android.widget.FrameLayout | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.ComposeView | ||
import com.facebook.react.bridge.Arguments | ||
import com.facebook.react.bridge.ReactContext | ||
import com.facebook.react.uimanager.ThemedReactContext | ||
import com.facebook.react.uimanager.events.RCTEventEmitter | ||
import com.lottiefiles.dotlottie.core.compose.runtime.DotLottieController | ||
import com.lottiefiles.dotlottie.core.compose.ui.DotLottieAnimation | ||
import com.lottiefiles.dotlottie.core.util.DotLottieEventListener | ||
import com.lottiefiles.dotlottie.core.util.DotLottieSource | ||
|
||
class DotlottieReactNativeView(context: ThemedReactContext) : FrameLayout(context) { | ||
|
||
private var reactContext: ReactContext = context.reactApplicationContext | ||
private var animationUrl: String? = null | ||
private var loop: Boolean = false | ||
private var autoplay: Boolean = true | ||
private var speed: Float = 1f | ||
var dotLottieController: DotLottieController = DotLottieController() | ||
|
||
private val composeView: ComposeView = | ||
ComposeView(context).apply { | ||
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) | ||
} | ||
|
||
init { | ||
addView(composeView) | ||
// Set initial content | ||
composeView.setContent { DotLottieContent() } | ||
} | ||
|
||
fun onReceiveNativeEvent(eventName: String, value: String?) { | ||
val event = Arguments.createMap().apply { putString("message", value) } | ||
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, eventName, event) | ||
} | ||
|
||
@Composable | ||
fun DotLottieContent() { | ||
dotLottieController = remember { DotLottieController() } | ||
|
||
animationUrl?.let { url -> | ||
DotLottieAnimation( | ||
source = DotLottieSource.Url(url), | ||
autoplay = autoplay, | ||
loop = loop, | ||
speed = speed, | ||
controller = dotLottieController, | ||
eventListeners = | ||
listOf( | ||
object : DotLottieEventListener { | ||
override fun onLoad() { | ||
onReceiveNativeEvent("onLoad", null) | ||
} | ||
override fun onComplete() { | ||
|
||
onReceiveNativeEvent("onComplete", null) | ||
} | ||
override fun onLoadError() { | ||
|
||
onReceiveNativeEvent("onLoadError", null) | ||
} | ||
override fun onPlay() { | ||
onReceiveNativeEvent("onPlay", null) | ||
} | ||
override fun onStop() { | ||
onReceiveNativeEvent("onRender", null) | ||
} | ||
override fun onPause() { | ||
onReceiveNativeEvent("onPause", null) | ||
} | ||
override fun onFreeze() { | ||
onReceiveNativeEvent("onFreeze", null) | ||
} | ||
override fun onUnFreeze() { | ||
onReceiveNativeEvent("onUnFreeze", null) | ||
} | ||
override fun onDestroy() { | ||
onReceiveNativeEvent("onDestroy", null) | ||
} | ||
} | ||
) | ||
) | ||
} | ||
} | ||
|
||
fun setSource(url: String?) { | ||
animationUrl = url | ||
composeView.setContent { DotLottieContent() } | ||
} | ||
|
||
fun setLoop(value: Boolean) { | ||
loop = value | ||
composeView.setContent { DotLottieContent() } | ||
} | ||
|
||
fun setAutoPlay(value: Boolean) { | ||
autoplay = value | ||
composeView.setContent { DotLottieContent() } | ||
} | ||
|
||
fun setSpeed(value: Double) { | ||
speed = value.toFloat() | ||
composeView.setContent { DotLottieContent() } | ||
} | ||
} |
141 changes: 114 additions & 27 deletions
141
android/src/main/java/com/dotlottiereactnative/DotlottieReactNativeViewManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,137 @@ | ||
package com.dotlottiereactnative | ||
|
||
import android.view.View | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.platform.ComposeView | ||
import com.dotlottie.dlplayer.Mode | ||
import com.facebook.react.bridge.ReadableArray | ||
import com.facebook.react.uimanager.SimpleViewManager | ||
import com.facebook.react.uimanager.ThemedReactContext | ||
import com.facebook.react.uimanager.annotations.ReactProp | ||
import com.lottiefiles.dotlottie.core.compose.ui.DotLottieAnimation | ||
import com.lottiefiles.dotlottie.core.util.DotLottieSource | ||
|
||
class DotlottieReactNativeViewManager : SimpleViewManager<View>() { | ||
|
||
private var animationUrl: String? = null | ||
private var loop: Boolean = false | ||
private var autoplay: Boolean = true | ||
class DotlottieReactNativeViewManager : SimpleViewManager<DotlottieReactNativeView>() { | ||
|
||
override fun getName() = "DotlottieReactNativeView" | ||
|
||
override fun createViewInstance(reactContext: ThemedReactContext): ComposeView { | ||
return ComposeView(reactContext).apply { setContent { DotLottieContent() } } | ||
override fun createViewInstance(reactContext: ThemedReactContext): DotlottieReactNativeView { | ||
return DotlottieReactNativeView(reactContext) | ||
} | ||
|
||
override fun getCommandsMap(): MutableMap<String, Int> { | ||
return mutableMapOf( | ||
COMMAND_PLAY to COMMAND_PLAY_ID, | ||
COMMAND_PAUSE to COMMAND_PAUSE_ID, | ||
COMMAND_STOP to COMMAND_STOP_ID, | ||
COMMAND_SET_SPEED to COMMAND_SET_SPEED_ID, | ||
COMMAND_FREEZE to COMMAND_FREEZE_ID, | ||
COMMAND_UNFREEZE to COMMAND_UNFREEZE_ID, | ||
COMMAND_SET_LOOP to COMMAND_SET_LOOP_ID, | ||
COMMAND_SET_FRAME to COMMAND_SET_FRAME_ID, | ||
COMMAND_SET_PLAY_MODE to COMMAND_SET_PLAY_MODE_ID | ||
) | ||
} | ||
|
||
@Composable | ||
fun DotLottieContent() { | ||
// Use the URL to load the animation | ||
animationUrl?.let { url -> | ||
DotLottieAnimation(source = DotLottieSource.Url(url), autoplay = autoplay, loop = loop) | ||
override fun getExportedCustomBubblingEventTypeConstants(): Map<String, Any> { | ||
return getEventTypeConstants( | ||
"onLoad", | ||
"onComplete", | ||
"onLoadError", | ||
"onPlay", | ||
"onLoop", | ||
"onDestroy", | ||
"onUnFreeze", | ||
"onFreeze", | ||
"onPause", | ||
"onFrame", | ||
"onStop", | ||
"onRender", | ||
"onLoop" | ||
) | ||
} | ||
|
||
override fun receiveCommand( | ||
view: DotlottieReactNativeView, | ||
commandId: Int, | ||
args: ReadableArray? | ||
) { | ||
|
||
if (commandId == COMMAND_PLAY_ID) view.dotLottieController.play() | ||
if (commandId == COMMAND_PAUSE_ID) view.dotLottieController.pause() | ||
if (commandId == COMMAND_STOP_ID) view.dotLottieController.stop() | ||
if (commandId == COMMAND_SET_SPEED_ID) { | ||
val speed = args?.getDouble(0)?.toFloat() ?: 1f | ||
view.dotLottieController.setSpeed(speed) | ||
} | ||
if (commandId == COMMAND_FREEZE_ID) { | ||
view.dotLottieController.freeze() | ||
} | ||
if (commandId == COMMAND_UNFREEZE_ID) { | ||
view.dotLottieController.unFreeze() | ||
} | ||
if (commandId == COMMAND_SET_LOOP_ID) { | ||
val loop = args?.getBoolean(0) ?: false | ||
view.dotLottieController.setLoop(loop) | ||
} | ||
|
||
if (commandId == COMMAND_SET_FRAME_ID) { | ||
val frame = args?.getDouble(0)?.toFloat() ?: 0f | ||
view.dotLottieController.setFrame(frame) | ||
} | ||
if (commandId == COMMAND_SET_PLAY_MODE_ID) { | ||
val mode = args?.getInt(0) | ||
val modeValue = Mode.values()[mode ?: 0] | ||
view.dotLottieController.setPlayMode(modeValue) | ||
} | ||
} | ||
|
||
@ReactProp(name = "source") | ||
fun setSource(view: ComposeView, url: String?) { | ||
// Update the URL and re-compose the view | ||
animationUrl = url | ||
view.setContent { DotLottieContent() } | ||
fun setSource(view: DotlottieReactNativeView, url: String?) { | ||
view.setSource(url) | ||
} | ||
|
||
@ReactProp(name = "loop") | ||
fun setLoop(view: ComposeView, value: Boolean) { | ||
loop = value | ||
view.setContent { DotLottieContent() } | ||
fun setLoop(view: DotlottieReactNativeView, value: Boolean) { | ||
view.setLoop(value) | ||
} | ||
|
||
@ReactProp(name = "autoplay") | ||
fun setAutoPlay(view: ComposeView, value: Boolean) { | ||
autoplay = value | ||
view.setContent { DotLottieContent() } | ||
fun setAutoPlay(view: DotlottieReactNativeView, value: Boolean) { | ||
view.setAutoPlay(value) | ||
} | ||
|
||
@ReactProp(name = "speed") | ||
fun setSpeed(view: DotlottieReactNativeView, value: Double) { | ||
view.setSpeed(value) | ||
} | ||
|
||
companion object { | ||
const val TAG = "DotlottieReactNativeViewManager" | ||
|
||
private const val COMMAND_PLAY = "play" | ||
private const val COMMAND_PLAY_ID = 1 | ||
|
||
private const val COMMAND_PAUSE = "pause" | ||
private const val COMMAND_PAUSE_ID = 2 | ||
|
||
private const val COMMAND_STOP = "stop" | ||
private const val COMMAND_STOP_ID = 3 | ||
|
||
private const val COMMAND_SET_SPEED = "setSpeed" | ||
private const val COMMAND_SET_SPEED_ID = 4 | ||
|
||
private const val COMMAND_FREEZE = "freeze" | ||
private const val COMMAND_FREEZE_ID = 5 | ||
|
||
private const val COMMAND_UNFREEZE = "unFreeze" | ||
private const val COMMAND_UNFREEZE_ID = 6 | ||
|
||
private const val COMMAND_SET_LOOP = "setLoop" | ||
private const val COMMAND_SET_LOOP_ID = 7 | ||
|
||
private const val COMMAND_SET_PROGRESS = "setProgress" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this command is used for ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will remove this. was trying to use it for segment |
||
private const val COMMAND_SET_PROGRESS_ID = 8 | ||
|
||
private const val COMMAND_SET_FRAME = "setFrame" | ||
private const val COMMAND_SET_FRAME_ID = 9 | ||
|
||
private const val COMMAND_SET_PLAY_MODE = "setPlayMode" | ||
private const val COMMAND_SET_PLAY_MODE_ID = 10 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.dotlottiereactnative | ||
|
||
fun getEventTypeConstants(vararg list: String): Map<String, Any> { | ||
return list.associateWith { mapOf("phasedRegistrationNames" to mapOf("bubbled" to it)) } | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets create eventListeners same as
dotLottieController
withremember
. I think current approach will create new DotLottieEventListener on every composition.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no it won't because it's on the class so it is created only once
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool we can resolve this