Skip to content

Instantly share code, notes, and snippets.

@dmitriy-chernysh
Last active August 12, 2022 12:29
Show Gist options
  • Save dmitriy-chernysh/8bfb4ebb9f2999c60649736b71d21b8e to your computer and use it in GitHub Desktop.
Save dmitriy-chernysh/8bfb4ebb9f2999c60649736b71d21b8e to your computer and use it in GitHub Desktop.
VideoUploader.uploadAsyncWithProgressCallback(
videoContentBuilder.build(),
pageId,
object : GraphRequest.OnProgressCallback {
override fun onCompleted(response: GraphResponse) {
// {Response: responseCode: 200, graphObject: {"success":true,"video_id":"380208427529152"}
try {
val videoId = parseResponseVideoId(response.jsonObject)
if (videoId.isNullOrEmpty())
onFail("Failed to share video to Facebook: $response")
else
onSuccess(getPostUrl(userOrPageId, videoId))
} catch (e: Exception) {
onFail("Failed to share video to Facebook: ${e.localizedMessage}")
}
}
override fun onProgress(current: Long, max: Long) {
if (currentTime - lastTimeProgressUpdated > 1000) {
val progress: Float =
if (max > 0) (current * 100 / max.toFloat()) else 0F
onProgress(progress)
}
}
}
)
@Throws(JSONException::class)
private fun parseResponseVideoId(json: JSONObject?): String? =
json?.getString(KEY_VIDEO_ID)
private fun getPostUrl(userOrPageId: String, postId: String): String =
"fb.com/$userOrPageId/$postId/"
private const val KEY_VIDEO_ID = "video_id"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment