Browse Source

Implement upload video to server

Wayne 6 năm trước cách đây
mục cha
commit
5737b6ff43

+ 13 - 15
src/main/java/com/bomostory/sceneeditmodule/screen/movie/MovieEditActivity.kt

@@ -381,21 +381,19 @@ class MovieEditActivity : AppCompatActivity(),
             onClickBomo = Runnable {
                 saveMovie(object : OnMovieSavedListener {
                     override fun onSaved(file: File) {
-                    AlertDialog.Builder(this@MovieEditActivity)
-                            .setMessage("Complete")
-                            .setPositiveButton("Play") { _, _ ->
-                                val uri = Uri.parse(file.absolutePath)
-                                val intent = Intent(Intent.ACTION_VIEW, uri).apply {
-                                    setDataAndType(uri, "video/mp4")
-                                }
-                                if (intent.resolveActivity(packageManager) != null) {
-                                    Intent.createChooser(intent, "").apply {
-                                        startActivity(this)
-                                    }
-                                } else {
-                                    Toast.makeText(this@MovieEditActivity, "No app can play video", Toast.LENGTH_SHORT).show()
-                                }
-                            }.show()
+                        val pd = ProgressDialog(this@MovieEditActivity).apply {
+                            isCancelable = false
+                        }
+                        viewModel.uploadVideo(this@MovieEditActivity, file, project)
+                                .subscribeOn(Schedulers.io())
+                                .observeOn(AndroidSchedulers.mainThread())
+                                .doOnSubscribe { pd.show() }
+                                .doFinally { pd.dismiss() }
+                                .subscribe({
+                                    Toast.makeText(this@MovieEditActivity, R.string.share_dialog_upload_suc, Toast.LENGTH_SHORT).show()
+                                }, {
+                                    Toast.makeText(this@MovieEditActivity, R.string.share_dialog_upload_failed, Toast.LENGTH_SHORT).show()
+                                })
                     }
                 })
             }

+ 18 - 0
src/main/java/com/bomostory/sceneeditmodule/screen/movie/MovieEditViewModel.kt

@@ -3,12 +3,19 @@ package com.bomostory.sceneeditmodule.screen.movie
 import android.arch.lifecycle.MutableLiveData
 import android.arch.lifecycle.ViewModel
 import android.content.ContentResolver
+import android.content.Context
 import android.media.MediaMetadataRetriever
 import com.bomostory.sceneeditmodule.basicdata.*
 import com.bomostory.sceneeditmodule.utils.TimeUtils
+import com.example.bomocloud.BoMoCloud
+import com.example.bomocloud.video.UploadVideoData
 import com.example.exportmedia.MediaHelper
 import com.example.exportmedia.audio.AudioEditor
+import com.example.tfat.myapplication.R
 import io.reactivex.Observable
+import okhttp3.MediaType
+import okhttp3.MultipartBody
+import okhttp3.RequestBody
 import java.io.File
 
 class MovieEditViewModel : ViewModel() {
@@ -98,4 +105,15 @@ class MovieEditViewModel : ViewModel() {
         return totalPeriod
     }
 
+    fun uploadVideo(context: Context, file: File, project: Project): Observable<UploadVideoData> {
+        val id = context.getString(R.string.bomo_cloud_client_id)
+        val secret = context.getString(R.string.bomo_cloud_client_secret)
+        val author = project.author ?: ""
+        val name = project.name ?: ""
+        val category = 0
+        val filePart = MultipartBody.Part.createFormData("file", file.name, RequestBody.create(MediaType.parse("video/*"), file))
+        return BoMoCloud(id, secret).videoService
+                .uploadVideo(id, secret, author, name, category, filePart)
+    }
+
 }

+ 3 - 0
src/main/res/values/strings.xml

@@ -22,4 +22,7 @@
     <string name="export_pdf_dialog_radio_foldable_booklet">Foldable Booklet</string>
     <string name="export_pdf_dialog_print">Print</string>
     <string name="export_pdf_dialog_cancel">Cancel</string>
+
+    <string name="share_dialog_upload_suc">Upload success!</string>
+    <string name="share_dialog_upload_failed">Upload failed</string>
 </resources>