Skip to content

Commit

Permalink
Merge pull request #17 from MateusRodCosta/dev
Browse files Browse the repository at this point in the history
Version 1.1.0
  • Loading branch information
MateusRodCosta authored Feb 13, 2024
2 parents ea37a62 + dbc7de6 commit 836c945
Show file tree
Hide file tree
Showing 18 changed files with 1,005 additions and 672 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/gradle-wrapper-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: "Validate Gradle Wrapper"
on: [push, pull_request]

jobs:
validation:
name: "Validation"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v2
7 changes: 3 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ android {
defaultConfig {
applicationId = "com.mateusrodcosta.apps.share2storage"
minSdk = 26
//noinspection OldTargetApi
targetSdk = 33
versionCode = 15
versionName = "1.0.1"
targetSdk = 34
versionCode = 16
versionName = "1.1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

</activity>

<activity
android:name=".SettingsActivity"
android:exported="false" />

<activity
android:name=".DetailsActivity"
android:exported="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,106 +21,79 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.format.Formatter
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.AudioFile
import androidx.compose.material.icons.outlined.Description
import androidx.compose.material.icons.outlined.Image
import androidx.compose.material.icons.outlined.VideoFile
import androidx.compose.material.icons.rounded.Download
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.WindowHeightSizeClass
import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.preference.PreferenceManager
import com.mateusrodcosta.apps.share2storage.model.SampleUriDataProvider
import com.mateusrodcosta.apps.share2storage.model.UriData
import com.mateusrodcosta.apps.share2storage.ui.theme.AppTheme
import com.mateusrodcosta.apps.share2storage.utils.AppBasicDivider
import com.mateusrodcosta.apps.share2storage.screens.DetailsScreen
import com.mateusrodcosta.apps.share2storage.utils.CreateDocumentWithInitialUri
import com.mateusrodcosta.apps.share2storage.utils.SharedPreferenceKeys
import com.mateusrodcosta.apps.share2storage.utils.appTopAppBarColors
import com.mateusrodcosta.apps.share2storage.utils.getUriData
import com.mateusrodcosta.apps.share2storage.utils.saveFile

class DetailsActivity : ComponentActivity() {
private lateinit var createFile: ActivityResultLauncher<String>
private var uriData: UriData? = null

private var createFile: ActivityResultLauncher<String>? = null
private var windowSizeClass: WindowSizeClass? = null
private var skipFileDetails: Boolean? = null
private var defaultSaveLocation: Uri? = null

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)

getPreferences()
handleIntent()
val launchFilePicker = {
createFile.launch(uriData?.displayName ?: "")
}

setContent {
val windowSizeClass = calculateWindowSizeClass(this)
DetailsScreen(
uriData = uriData,
windowSizeClass = windowSizeClass,
launchFilePicker = launchFilePicker,
)
}

if (skipFileDetails == true) launchFilePicker()
}

private fun getPreferences() {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val skipFileDetails =
skipFileDetails =
sharedPreferences.getBoolean(SharedPreferenceKeys.skipFileDetailsKey, false)
val defaultSaveLocationRaw =
sharedPreferences.getString(SharedPreferenceKeys.defaultSaveLocationKey, null)
Log.d("details] defaultSaveLocationRaw", defaultSaveLocationRaw.toString())
val defaultSaveLocation =
if (defaultSaveLocationRaw != null) Uri.parse(defaultSaveLocationRaw)
else null
defaultSaveLocation = if (defaultSaveLocationRaw != null) Uri.parse(defaultSaveLocationRaw)
else null

Log.d("details] defaultSaveLocation", defaultSaveLocation.toString())
}

var uriData: UriData? = null

if (intent?.action == Intent.ACTION_SEND) {

val fileUri: Uri? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)
} else {
@Suppress("DEPRECATION") intent.getParcelableExtra(Intent.EXTRA_STREAM)
}

private fun handleIntent() {
if (intent.action == Intent.ACTION_SEND) {
val fileUri: Uri? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) intent.getParcelableExtra(
Intent.EXTRA_STREAM,
Uri::class.java
)
else @Suppress("DEPRECATION") intent.getParcelableExtra(Intent.EXTRA_STREAM)
Log.d("fileUri", fileUri.toString())

uriData = getUriData(contentResolver, fileUri)

if (uriData != null) {
createFile = registerForActivityResult(
CreateDocumentWithInitialUri(uriData.type ?: "*/*", defaultSaveLocation)
CreateDocumentWithInitialUri(uriData?.type ?: "*/*", defaultSaveLocation)
) { uri ->
if (uri == null || fileUri == null) return@registerForActivityResult
val isSuccess = saveFile(baseContext, uri, fileUri)
Expand All @@ -131,184 +104,9 @@ class DetailsActivity : ComponentActivity() {
R.string.toast_saved_file_failure
}, Toast.LENGTH_LONG
).show()
if (skipFileDetails) finish()
}
}
}

setContent {
windowSizeClass = calculateWindowSizeClass(this)
DetailsScreen(uriData)
}

if (skipFileDetails) createFile!!.launch(uriData?.displayName ?: "")
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
@Preview
fun DetailsScreen(@PreviewParameter(SampleUriDataProvider::class) uriData: UriData?) {
val widthSizeClass = windowSizeClass?.widthSizeClass ?: WindowWidthSizeClass.Compact
val heightSizeClass = windowSizeClass?.heightSizeClass ?: WindowHeightSizeClass.Medium
AppTheme {
Scaffold(topBar = {
TopAppBar(
title = { Text(stringResource(R.string.file_details)) },
colors = appTopAppBarColors(),
)
}, floatingActionButton =

{
if (uriData != null) {
FloatingActionButton(
onClick = {
createFile!!.launch(uriData.displayName ?: "")
},
content = {
Icon(
Icons.Rounded.Download,
contentDescription = stringResource(R.string.save_button)
)
},
containerColor = MaterialTheme.colorScheme.secondary,
contentColor = MaterialTheme.colorScheme.onSecondary,
)
}
}) { paddingValues ->
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
) {
val showLandscapePhone = heightSizeClass == WindowHeightSizeClass.Compact
val showLandscapeTablet = widthSizeClass == WindowWidthSizeClass.Expanded
if (uriData != null) {
if (showLandscapePhone || showLandscapeTablet) {
FileDetailsLandscape(uriData)
} else {
FileDetailsPortrait(uriData)
}
} else Text(
stringResource(R.string.no_file_found),
style = MaterialTheme.typography.titleMedium
)
if (skipFileDetails == true) finish()
}
}
}
}

@Composable
fun FileDetailsPortrait(uriData: UriData) {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceEvenly
) {
Box(modifier = Modifier.weight(1.0f)) {
FilePreview(uriData)
}
Box(modifier = Modifier.weight(1.0f)) {
FileInfo(uriData)
}
}
}

@Composable
fun FileDetailsLandscape(uriData: UriData) {
Row(
modifier = Modifier.fillMaxSize(),
) {
Box(modifier = Modifier.weight(1.0f)) {
FilePreview(uriData)
}
Box(modifier = Modifier.weight(1.0f)) {
FileInfo(uriData)
}
}
}

@Composable
fun FileInfo(uriData: UriData) {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
) {
AppBasicDivider()
FileInfoLine(
label = stringResource(R.string.file_name),
content = uriData.displayName ?: stringResource(R.string.unknown)
)
AppBasicDivider()
FileInfoLine(
label = stringResource(R.string.file_type),
content = uriData.type ?: stringResource(R.string.unknown)
)
AppBasicDivider()
FileInfoLine(
label = stringResource(R.string.file_size),
// TODO: Find code to calculate file size for previews
content = if (uriData.size != null && baseContext != null) Formatter.formatFileSize(
baseContext, uriData.size
) else stringResource(R.string.unknown)
)
AppBasicDivider()
}
}

@Composable
fun FileInfoLine(label: String, content: String) {
Box(modifier = Modifier
.fillMaxWidth()
.clickable { }) {
Column(
modifier = Modifier
.padding(PaddingValues(horizontal = 16.dp, vertical = 8.dp))
.heightIn(min = 48.dp), horizontalAlignment = Alignment.Start
) {
Text(label, style = MaterialTheme.typography.titleLarge)
Spacer(modifier = Modifier.height(8.dp))
Text(content, softWrap = true, style = MaterialTheme.typography.bodyLarge)
}
}
}

@Composable
fun FilePreview(uriData: UriData) {
val mimeType = uriData.type ?: "*/*"
val fallbackFileIcon = if (mimeType.startsWith("image/")) {
Icons.Outlined.Image
} else if (mimeType.startsWith("audio/")) {
Icons.Outlined.AudioFile
} else if (mimeType.startsWith("video/")) {
Icons.Outlined.VideoFile
} else {
Icons.Outlined.Description
}

Box(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
if (uriData.previewImage != null) {
Image(
modifier = Modifier.align(Alignment.Center),
bitmap = uriData.previewImage.asImageBitmap(),
contentDescription = stringResource(R.string.app_name),
contentScale = ContentScale.Fit,
)
} else {
Icon(
modifier = Modifier
.size(128.dp)
.align(Alignment.Center),
imageVector = fallbackFileIcon,
contentDescription = stringResource(R.string.app_name),
tint = MaterialTheme.colorScheme.tertiary
)
}
}
}
}
Loading

0 comments on commit 836c945

Please sign in to comment.