Skip to content

Commit

Permalink
Contract for questions created
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkaDev committed Feb 17, 2022
1 parent 0ccd2aa commit c926d3f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,55 +36,112 @@ class AddOwnQuestionPresenter @Inject constructor(
}
var videoAbsolutePath: String = ""

fun initVideoUrl(videoAbsolutePath: String){
fun initVideoUrl(videoAbsolutePath: String) {
this.videoAbsolutePath = videoAbsolutePath
presenterScope.launch {
viewState.showVideoThumbnail(videoAbsolutePath)
}
}

fun initAnswersList(){
fun initAnswersList() {
presenterScope.launch {
viewState.initAnswersList(answers)
}
}

fun initCategoriesSpinner(){
fun initCategoriesSpinner() {
presenterScope.launch {
viewState.initCategoriesSpinner(
firebaseRepository.getCategories()
)
}
}

fun uploadVideoToStorage(videoAbsolutePath: String){
fun uploadVideoToStorage(videoAbsolutePath: String) {
presenterScope.launch {
var videoFullRef: StorageReference? = null
firebaseUser?.let { user ->
val videoFileName = user.uid + ".mp4"
videoFullRef = firebaseStorageRef.child(VIDEO_PATH + videoFileName)

val stream = FileInputStream(File(videoAbsolutePath))

val uploadTask = videoFullRef?.putStream(stream)
uploadTask?.addOnFailureListener {
Log.d("upload", "upload fail, cause: ${it.message}")
}?.addOnSuccessListener { taskSnapshot ->
Log.d(
"upload",
"upload success, bytesTransferred = ${taskSnapshot.bytesTransferred}"
)
}
val videoFileName = user.uid + ".mp4"
videoFullRef = firebaseStorageRef.child(VIDEO_PATH + videoFileName)

val stream = FileInputStream(File(videoAbsolutePath))

val uploadTask = videoFullRef?.putStream(stream)
uploadTask?.addOnFailureListener {
Log.d("upload", "upload fail, cause: ${it.message}")
}?.addOnSuccessListener { taskSnapshot ->
Log.d(
"upload",
"upload success, bytesTransferred = ${taskSnapshot.bytesTransferred}"
)
}
} ?: run {
viewState.showError("Can't get profile's data")
}
}
}

companion object{
fun buildQuestion() {
val unixTimestamp = "_" + System.currentTimeMillis() / 1000
proposedQuestion.apply {
firebaseUser?.let{ firebaseUser ->
// Restriction for document name is 1024KiB (1024 bit)
questionId = firebaseUser.uid + unixTimestamp
// categoryId - should be set from spinner in controller already
answerVariants = answers

questionBody = firebaseUser.uid + unixTimestamp
questionAuthorUid = firebaseUser.uid
questionType = Question.Companion.QUESTION_TYPE.VIDEO
} ?: kotlin.run {
Log.d("buildQuestion", "buildQuestion: firebaseUser is null. " +
"Question must be stopped with contract checking")
}
}
}

fun checkQuestionContract(): Boolean {
var rightAnswersCount = 0

if (proposedQuestion.categoryId.isEmpty()) {
Log.d("checkQuestionContract", "checkQuestionContract: categoryId is empty")
return false
}
proposedQuestion.answerVariants.forEachIndexed() { index, answer ->
if (answer.answer.isEmpty()) {
Log.d("checkQuestionContract", "answer with index=$index is empty")
return false
} else {
if (answer.isCorrect) rightAnswersCount++
}
}
if (rightAnswersCount != 1) {
Log.d("checkQuestionContract", "right answers count = $rightAnswersCount")
return false
}

if (proposedQuestion.questionAuthorUid.isNullOrEmpty()) {
Log.d(
"checkQuestionContract",
"firebaseUser.uid = ${proposedQuestion.questionAuthorUid}"
)
return false
}
return true
}

fun sendQuestion() {

}

companion object {
val VIDEO_PATH = "video/"
}

@StateStrategyType(AddToEndSingleStrategy::class)
interface IAddOwnQuestionView : MvpView, IError {
fun initCategoriesSpinner(categoriesList: List<Category>)
fun initAnswersList(answersList: MutableList<Answer>)
fun showVideoThumbnail(videoAbsolutePath: String)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class AddOwnQuestionController : MvpController(), AddOwnQuestionPresenter.IAddOw
checkPermissionForVideoUploading()
}

binding.proposeQuestionButton.setOnClickListener {
presenter.buildQuestion()
if (presenter.checkQuestionContract())
presenter.sendQuestion()
}
return binding.root
}

Expand Down Expand Up @@ -106,6 +111,13 @@ class AddOwnQuestionController : MvpController(), AddOwnQuestionPresenter.IAddOw
}
}

override fun showVideoThumbnail(videoAbsolutePath: String) {
Log.d("showVideoThumbnail", "showVideoThumbnail called in controller, videoAbsolutePath=$videoAbsolutePath")
binding.questionVideoView.visibility = View.VISIBLE
binding.questionVideoView.setVideoPath(videoAbsolutePath)
binding.questionVideoView.start()
}

private fun startChooseVideoIntent() {
try {
Intent(MediaStore.ACTION_VIDEO_CAPTURE).also { takeVideoIntent ->
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/view_controller_add_own_question.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
app:cardElevation="0dp"
android:elevation="0dp"
app:cardCornerRadius="160dp">
<VideoView
android:id="@+id/questionVideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="gone">
</VideoView>
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/addVideoButton"
android:layout_width="match_parent"
Expand Down

0 comments on commit c926d3f

Please sign in to comment.