Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Remove StorageHelper in favour of FileRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Jul 29, 2023
1 parent 3dd54a9 commit 6da5779
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationManagerCompat
import com.bnyro.recorder.App
import com.bnyro.recorder.services.RecorderService
import com.bnyro.recorder.util.IntentHelper
import com.bnyro.recorder.util.NotificationHelper
import com.bnyro.recorder.util.StorageHelper

class FinishedNotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val fileName = intent.getStringExtra(RecorderService.FILE_NAME_EXTRA_KEY) ?: return
val file = StorageHelper.getOutputDir(context).findFile(fileName)
val file = (context.applicationContext as App).container.fileRepository
.getOutputDir().findFile(fileName)

when (intent.getStringExtra(RecorderService.ACTION_EXTRA_KEY)) {
RecorderService.SHARE_ACTION -> file?.let { IntentHelper.shareFile(context, it) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.bnyro.recorder.services

import com.bnyro.recorder.App
import com.bnyro.recorder.R
import com.bnyro.recorder.enums.AudioChannels
import com.bnyro.recorder.enums.AudioDeviceSource
import com.bnyro.recorder.obj.AudioFormat
import com.bnyro.recorder.util.PlayerHelper
import com.bnyro.recorder.util.Preferences
import com.bnyro.recorder.util.StorageHelper

class AudioRecorderService : RecorderService() {
override val notificationTitle: String
Expand Down Expand Up @@ -38,8 +38,7 @@ class AudioRecorderService : RecorderService() {
setOutputFormat(audioFormat.format)
setAudioEncoder(audioFormat.codec)

outputFile = StorageHelper.getOutputFile(
this@AudioRecorderService,
outputFile = (application as App).container.fileRepository.getOutputFile(
audioFormat.extension
)
fileDescriptor = contentResolver.openFileDescriptor(outputFile!!.uri, "w")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import android.media.MediaRecorder
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.documentfile.provider.DocumentFile
import com.bnyro.recorder.App
import com.bnyro.recorder.R
import com.bnyro.recorder.enums.RecorderState
import com.bnyro.recorder.util.PcmConverter
import com.bnyro.recorder.util.StorageHelper
import java.io.File
import kotlin.concurrent.thread
import kotlin.experimental.and
Expand Down Expand Up @@ -110,9 +110,11 @@ class LosslessRecorderService : RecorderService() {

private fun convertToWav() {
val inputStream = contentResolver.openInputStream(outputFile?.uri ?: return) ?: return
val outputStream = StorageHelper.getOutputFile(this, FILE_NAME_EXTENSION_WAV).let {
contentResolver.openOutputStream(it.uri) ?: return
}
val outputStream = (application as App).container.fileRepository
.getOutputFile(FILE_NAME_EXTENSION_WAV)
.let {
contentResolver.openOutputStream(it.uri) ?: return
}
pcmConverter?.convertToWave(inputStream, outputStream, BUFFER_SIZE_IN_BYTES)
outputFile?.delete()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.util.DisplayMetrics
import android.util.Log
import android.view.Display
import androidx.activity.result.ActivityResult
import com.bnyro.recorder.App
import com.bnyro.recorder.R
import com.bnyro.recorder.enums.AudioChannels
import com.bnyro.recorder.enums.AudioDeviceSource
Expand All @@ -21,7 +22,6 @@ import com.bnyro.recorder.enums.VideoFormat
import com.bnyro.recorder.obj.VideoResolution
import com.bnyro.recorder.util.PlayerHelper
import com.bnyro.recorder.util.Preferences
import com.bnyro.recorder.util.StorageHelper

class ScreenRecorderService : RecorderService() {
override val notificationTitle: String
Expand Down Expand Up @@ -115,10 +115,8 @@ class ScreenRecorderService : RecorderService() {
null
)

outputFile = StorageHelper.getOutputFile(
this@ScreenRecorderService,
videoFormat.extension
)
outputFile = (application as App).container.fileRepository
.getOutputFile(videoFormat.extension)
fileDescriptor = contentResolver.openFileDescriptor(outputFile!!.uri, "w")
setOutputFile(fileDescriptor?.fileDescriptor)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bnyro.recorder.R
import com.bnyro.recorder.ui.common.DialogButton
import com.bnyro.recorder.util.FileRepositoryImpl
import com.bnyro.recorder.util.Preferences
import com.bnyro.recorder.util.StorageHelper

@Composable
fun NamingPatternPref() {
Expand Down Expand Up @@ -48,7 +48,7 @@ fun NamingPatternPref() {
mutableStateOf(
Preferences.getString(
Preferences.namingPatternKey,
StorageHelper.DEFAULT_NAMING_PATTERN
FileRepositoryImpl.DEFAULT_NAMING_PATTERN
)
)
}
Expand Down
50 changes: 47 additions & 3 deletions app/src/main/java/com/bnyro/recorder/util/FileRepository.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
package com.bnyro.recorder.util

import android.annotation.SuppressLint
import android.content.Context
import android.media.MediaMetadataRetriever
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import com.bnyro.recorder.enums.RecorderType
import com.bnyro.recorder.enums.SortOrder
import com.bnyro.recorder.obj.RecordingItemData
import java.text.SimpleDateFormat
import java.util.Calendar

interface FileRepository {
suspend fun getVideoRecordingItems(sortOrder: SortOrder): List<RecordingItemData>
suspend fun getAudioRecordingItems(sortOrder: SortOrder): List<RecordingItemData>
suspend fun deleteSelectedFiles(files: List<DocumentFile>)
suspend fun deleteAllFiles()
fun getOutputFile(extension: String): DocumentFile
fun getOutputDir(): DocumentFile
}

class FileRepositoryImpl(val context: Context) : FileRepository {

private fun getVideoFiles(): List<DocumentFile> =
StorageHelper.getOutputDir(context).listFiles().filter {
getOutputDir().listFiles().filter {
it.isFile && it.name.orEmpty().endsWith("mp4")
}

private fun getAudioFiles(): List<DocumentFile> =
StorageHelper.getOutputDir(context).listFiles().filter {
getOutputDir().listFiles().filter {
it.isFile && !it.name.orEmpty().endsWith("mp4")
}

Expand Down Expand Up @@ -50,10 +56,48 @@ class FileRepositoryImpl(val context: Context) : FileRepository {
}

override suspend fun deleteAllFiles() {
StorageHelper.getOutputDir(context).listFiles().forEach {
getOutputDir().listFiles().forEach {
if (it.isFile) it.delete()
}
}

override fun getOutputFile(extension: String): DocumentFile {
val currentTimeMillis = Calendar.getInstance().time
val currentDateTime = dateTimeFormat.format(currentTimeMillis)
val currentDate = currentDateTime.split("_").first()
val currentTime = currentDateTime.split("_").last()

val fileName = Preferences.getString(
Preferences.namingPatternKey,
DEFAULT_NAMING_PATTERN
)
.replace("%d", currentDate)
.replace("%t", currentTime)
.replace("%m", currentTimeMillis.time.toString())
.replace("%s", currentTimeMillis.time.div(1000).toString())

val recordingFile = getOutputDir().createFile("audio/*", "$fileName.$extension")
return recordingFile!!
}

override fun getOutputDir(): DocumentFile {
val prefDir = Preferences.prefs.getString(Preferences.targetFolderKey, "")
val audioDir = when {
prefDir.isNullOrBlank() -> {
val dir = context.getExternalFilesDir(null) ?: context.filesDir
DocumentFile.fromFile(dir)
}

else -> DocumentFile.fromTreeUri(context, Uri.parse(prefDir))
}
return audioDir!!
}

companion object {
@SuppressLint("SimpleDateFormat")
private val dateTimeFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss")
const val DEFAULT_NAMING_PATTERN = "%d_%t"
}
}

fun List<DocumentFile>.sortedBy(sortOrder: SortOrder): List<DocumentFile> {
Expand Down
42 changes: 0 additions & 42 deletions app/src/main/java/com/bnyro/recorder/util/StorageHelper.kt

This file was deleted.

0 comments on commit 6da5779

Please sign in to comment.