Skip to content

Commit

Permalink
finish video and pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedshasho committed Aug 16, 2023
1 parent 3e1fab1 commit bcb28b5
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 163 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ dependencies {

implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

implementation 'com.google.android.exoplayer:exoplayer-core:2.18.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.1'
implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.18.1'

implementation 'com.github.HamidrezaAmz:MagicalExoPlayer:3.0.8'
}

kapt {
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:networkSecurityConfig="@xml/network_security_config"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:theme="@style/Theme.Scholar"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:theme="@style/Theme.CustomSplashScreenTheme"
android:exported="true">
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:exported="true"
android:theme="@style/Theme.CustomSplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import java.io.BufferedInputStream
import java.io.File
Expand Down Expand Up @@ -41,7 +42,7 @@ class MaterialContentVM @Inject constructor(
init {
viewModelScope.launch {
_loading.value = true
materialUseCase(materialId).collect {
materialUseCase(materialId).distinctUntilChanged().collect {
_material.value = it
_loading.value = false
}
Expand Down
80 changes: 32 additions & 48 deletions app/src/main/java/com/scholar/center/ui/materials/PdfFragment.kt
Original file line number Diff line number Diff line change
@@ -1,87 +1,71 @@
package com.scholar.center.ui.materials

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.scholar.center.R
import com.scholar.center.databinding.FragmentPdfBinding
import com.scholar.center.ui.dialogs.LoadingDialogFragment
import com.scholar.center.unit.Constants.BASE_URL
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.net.URL


@AndroidEntryPoint
class PdfFragment : Fragment(R.layout.fragment_pdf) {
private val viewModel: MaterialContentVM by viewModels()
private lateinit var binding: FragmentPdfBinding

@SuppressLint("SetJavaScriptEnabled")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val binding = FragmentPdfBinding.bind(view)
private lateinit var launcher: ActivityResultLauncher<Intent>

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding = FragmentPdfBinding.bind(view)

launcher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { data ->
findNavController().popBackStack()
}
val loadingDialog = LoadingDialogFragment()
loadingDialog.show(parentFragmentManager, "loading_dialog")
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.material.collect {
viewModel.material.collectLatest {
it?.let { materialWithDetail ->
Log.d("LOadPDF", "${materialWithDetail.material}")
val material = materialWithDetail.material
material.content?.let { link ->
Log.d("LOadPDF", "${BASE_URL}${link}")



binding.webView.settings.javaScriptEnabled = true
binding.webView.settings.builtInZoomControls = true
binding.webView.settings.displayZoomControls = false
val pdfUrl = "${BASE_URL}${link}"

binding.webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
binding.webView.loadUrl(
"javascript:(function() { " +
"document.querySelector('[role=\"toolbar\"]').remove();})()"
)

}
}


binding.webView.loadUrl("$pdfUrl")
val pdfLink = "${BASE_URL}${link}"
openPdfByIntent(pdfLink)
}
if (loadingDialog.isVisible) {
loadingDialog.dismiss()
}
}
}
}
}
}

viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.byteArray.collect {
it?.let { pdfArray ->
Log.d("LOadPDF", "${pdfArray}")

// binding.pdfView.fromFile(pdfArray)
// .defaultPage(0)
// .enableSwipe(true)
// .swipeHorizontal(false)
// .load()
}
}
}
}
private fun openPdfByIntent(pdfLink: String) {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(pdfLink))
launcher.launch(browserIntent)
}

private fun openPdfByWebView(pdfLink: String) {
// binding.webView.settings.javaScriptEnabled = true
// binding.webView.settings.builtInZoomControls = true
// binding.webView.settings.displayZoomControls = false
// val pdfUrl = "${BASE_URL}${link}"
// binding.webView.loadUrl("https://docs.google.com/gview?embedded=true&url=pdfLink")
}
}
133 changes: 37 additions & 96 deletions app/src/main/java/com/scholar/center/ui/materials/VideoFragment.kt
Original file line number Diff line number Diff line change
@@ -1,120 +1,61 @@
package com.scholar.center.ui.materials

import android.net.Uri

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.MediaController
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.upstream.DefaultDataSource
import com.google.android.exoplayer2.util.MimeTypes
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.scholar.center.R
import com.scholar.center.databinding.FragmentVideoBinding
import com.scholar.center.unit.Constants.BASE_URL
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch


@AndroidEntryPoint
class VideoFragment : Fragment(R.layout.fragment_video) {
// private val viewModel : MaterialContentVM by viewModels()

private var player: ExoPlayer? = null
private val isPlaying get() = player?.isPlaying ?: false
private val viewModel : MaterialContentVM by viewModels()
private lateinit var binding: FragmentVideoBinding

private val link = "https://media.geeksforgeeks.org/wp-content/uploads/20201217192146/Screenrecorder-2020-12-17-19-17-36-828.mp4?_=1"
private val link =
"https://media.geeksforgeeks.org/wp-content/uploads/20201217192146/Screenrecorder-2020-12-17-19-17-36-828.mp4?_=1"
private val localLink = "http://192.168.1.101:8000/media/1/promo.mp4"
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding = FragmentVideoBinding.bind(view)

val v = "https://4422-146-70-174-67.ngrok-free.app/media/1/cc.mp4"


// Uri object to refer the
// resource from the videoUrl
// Uri object to refer the
// resource from the videoUrl
val uri = Uri.parse(v)

// sets the resource from the
// videoUrl to the videoView

// sets the resource from the
// videoUrl to the videoView
binding.videoView.setVideoURI(uri)

// creating object of
// media controller class
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.material.collect { it ->
val material = it?.material
material?.content?.let { link ->
val url = "${BASE_URL}$link"
binding.andExoPlayerView.setSource(url)
}
}
}
}
}

// creating object of
// media controller class
/*
private fun playVideoWithVideoView(){
val uri = Uri.parse(localLink)
val mediaController = MediaController(requireContext())

// sets the anchor view
// anchor view for the videoView

// sets the anchor view
// anchor view for the videoView
mediaController.setAnchorView(binding.videoView)

// sets the media player to the videoView

// sets the media player to the videoView
mediaController.setMediaPlayer(binding.videoView)

// sets the media controller to the videoView

// sets the media controller to the videoView
binding.videoView.setMediaController(mediaController)

binding.videoView.layoutParams = RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT)

// starts the video

// starts the video
binding.videoView.start()

// viewLifecycleOwner.lifecycleScope.launch {
// repeatOnLifecycle(Lifecycle.State.STARTED) {
// viewModel.material.collect { it ->
// val material = it?.material
// material?.content?.let { link ->
// mediaController.setAnchorView(binding.videoView)
// mediaController.setMediaPlayer(binding.videoView)
// binding.videoView.setMediaController(mediaController)
// binding.videoView.setVideoURI(uri)
// binding.progressBar.visibility = View.VISIBLE
// binding.videoView.setOnPreparedListener {mp: MediaPlayer->
// mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
// binding.progressBar.visibility = View.GONE
// mp.setScreenOnWhilePlaying(false)
//
// }
// }
// }
// }
// binding.videoView.start()
}
*/

private fun initializePlayer() {
player = ExoPlayer.Builder(requireContext()) // <- context
.build()

// create a media item.
val mediaItem = MediaItem.Builder()
.setUri("https://storage.googleapis.com/exoplayer-test-media-0/BigBuckBunny_320x180.mp4")
.setMimeType(MimeTypes.APPLICATION_MP4)
.build()

// Create a media source and pass the media item
val mediaSource = ProgressiveMediaSource.Factory(
DefaultDataSource.Factory(requireContext()) // <- context
)
.createMediaSource(mediaItem)

// Finally assign this media source to the player
player!!.apply {
setMediaSource(mediaSource)
playWhenReady = true // start playing when the exoplayer has setup
seekTo(0, 0L) // Start from the beginning
prepare() // Change the state from idle.
}.also {
// Do not forget to attach the player to the view

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ class MaterialDetailFragment : Fragment(R.layout.fragment_material_detail) {
)
return@setOnClickListener
}
viewModel.purchase()
// viewModel.purchase()
// if (material.price == null || material.price == 0) {
// navigateTo(materialId = material.id, material.categoryId)
navigateTo(materialId = material.id, material.categoryId)
// } else {
// navigateTo(materialId = material.id, material.categoryId)
// // purchase
Expand All @@ -153,7 +153,7 @@ class MaterialDetailFragment : Fragment(R.layout.fragment_material_detail) {
)
} else {
navController.navigate(
MaterialDetailFragmentDirections.actionMaterialDetailToVideo(
MaterialDetailFragmentDirections.actionMaterialDetailToPdf(
materialId = materialId
)
)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/scholar/center/unit/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.scholar.center.unit

object Constants {

const val BASE_URL = "https://4422-146-70-174-67.ngrok-free.app"
const val BASE_URL = "http://192.168.1.101:8000"

const val TEACHER_ID_KEY = "teacher_id"
const val STAGE_ID_KEY = "stage_id"
Expand Down
26 changes: 19 additions & 7 deletions app/src/main/res/layout/fragment_video.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
android:gravity="center"
>

<VideoView
android:id="@+id/videoView"
<!-- <VideoView-->
<!-- android:id="@+id/videoView"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- />-->

<com.potyvideo.library.AndExoPlayerView
android:id="@+id/andExoPlayerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
android:layout_centerInParent="true"
android:visibility="gone"/>

</LinearLayout>
</RelativeLayout>
Loading

0 comments on commit bcb28b5

Please sign in to comment.