Fix next/prev at end/beginning and add title

This commit is contained in:
Brandon Presley 2022-12-22 14:28:01 +13:00
parent 4bc8f02c40
commit c6e0cce6be
3 changed files with 34 additions and 16 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,18 +1,17 @@
package com.example.swiper
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.TextView
import android.widget.VideoView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import java.io.File
@ -20,6 +19,7 @@ class MainActivity : AppCompatActivity() {
private var files: List<File>? = null
private var selected = 0
@SuppressLint("SetTextI18n")
private var resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
@ -31,23 +31,17 @@ class MainActivity : AppCompatActivity() {
val video = findViewById<VideoView>(R.id.videoView)
video.setVideoURI(files!![0].toUri())
video.start()
findViewById<TextView>(R.id.textView).text =
"${files!![0].name} (1 / ${files!!.size})"
}
}
@SuppressLint("SetTextI18n")
@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_DENIED
) {
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 0x3)
}
findViewById<Button>(R.id.pick).setOnClickListener {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
resultLauncher.launch(intent)
@ -65,15 +59,23 @@ class MainActivity : AppCompatActivity() {
}
findViewById<Button>(R.id.next).setOnClickListener {
if (files?.size == null) return@setOnClickListener
if (selected >= files!!.size - 1) return@setOnClickListener
selected++
video.setVideoURI(files!![selected].toUri())
video.start()
findViewById<TextView>(R.id.textView).text =
"${files!![selected].name} (${selected + 1} / ${files!!.size})"
}
findViewById<Button>(R.id.prev).setOnClickListener {
if (files?.size == null) return@setOnClickListener
if (selected <= 0) return@setOnClickListener
selected--
video.setVideoURI(files!![selected].toUri())
video.start()
findViewById<TextView>(R.id.textView).text =
"${files!![selected].name} (${selected + 1} / ${files!!.size})"
}
}
}

View File

@ -21,7 +21,7 @@
android:id="@+id/pick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginBottom="8dp"
android:text="PICK"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
@ -32,7 +32,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:layout_marginBottom="8dp"
android:text="NEXT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
@ -40,10 +40,20 @@
<Button
android:id="@+id/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:layout_marginBottom="8dp"
android:text="PREV"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>