-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewTransferActivity.kt
112 lines (99 loc) · 4.32 KB
/
NewTransferActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Infomaniak SwissTransfer - Android
* Copyright (C) 2024-2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.swisstransfer.ui
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.core.os.BundleCompat
import androidx.lifecycle.lifecycleScope
import com.infomaniak.core.utils.enumValueOfOrNull
import com.infomaniak.swisstransfer.ui.navigation.*
import com.infomaniak.swisstransfer.ui.screen.newtransfer.NewTransferOpenManager
import com.infomaniak.swisstransfer.ui.screen.newtransfer.NewTransferScreen
import com.infomaniak.swisstransfer.ui.screen.newtransfer.pickfiles.components.TransferTypeUi
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject
import com.infomaniak.swisstransfer.ui.screen.newtransfer.NewTransferOpenManager.Reason as OpenReason
@AndroidEntryPoint
class NewTransferActivity : ComponentActivity() {
@Inject
lateinit var newTransferOpenManager: NewTransferOpenManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge(statusBarStyle = SystemBarStyle.dark(Color.TRANSPARENT))
handleSharedFiles()
setContent {
SwissTransferTheme {
NewTransferScreen(
startDestination = getStartDestination(),
closeActivity = { startMainActivityIfTaskIsEmpty ->
finishNewTransferActivity(startMainActivityIfTaskIsEmpty)
},
)
}
}
}
private fun handleSharedFiles() {
val openReason: OpenReason = when (intent?.action) {
Intent.ACTION_SEND -> OpenReason.ExternalShareIncoming(getSingleUri())
Intent.ACTION_SEND_MULTIPLE -> OpenReason.ExternalShareIncoming(getMultipleUris())
else -> OpenReason.Other
}
lifecycleScope.launch { newTransferOpenManager.setOpenReason(openReason) }
}
private fun getSingleUri(): List<Uri> {
val extras = intent.extras ?: return emptyList()
val uri = BundleCompat.getParcelable(extras, Intent.EXTRA_STREAM, Uri::class.java)
return if (uri == null) emptyList() else listOf(uri)
}
private fun getMultipleUris(): List<Uri> {
val extras = intent.extras ?: return emptyList()
val uris = BundleCompat.getParcelableArrayList(extras, Intent.EXTRA_STREAM, Uri::class.java)
return uris ?: emptyList()
}
private fun finishNewTransferActivity(startMainActivityIfTaskIsEmpty: Boolean) {
when {
isTaskRoot && startMainActivityIfTaskIsEmpty -> {
startActivity(Intent(this, MainActivity::class.java))
finish()
}
isTaskRoot -> finishAndRemoveTask()
else -> finish()
}
}
private fun getStartDestination(): NewTransferNavigation {
val externalNavigation = enumValueOfOrNull<ExternalNavigation>(
value = intent?.extras?.getString(EXTERNAL_NAVIGATION_KEY),
)
val transferType = enumValueOfOrNull<TransferTypeUi>(intent?.extras?.getString(TRANSFER_TYPE_KEY))
val authorEmail = intent?.extras?.getString(TRANSFER_AUTHOR_EMAIL_KEY)
return when (externalNavigation) {
ExternalNavigation.Upload -> {
NewTransferNavigation.UploadDestination
}
null -> NewTransferNavigation.startDestination
}
}
}