-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathShareLinkContainerView.kt
180 lines (154 loc) · 6.48 KB
/
ShareLinkContainerView.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Infomaniak kDrive - Android
* Copyright (C) 2022-2024 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.drive.views
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.infomaniak.core.utils.format
import com.infomaniak.drive.MatomoDrive.trackShareRightsEvent
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.data.models.ShareLink
import com.infomaniak.drive.databinding.ViewShareLinkContainerBinding
import com.infomaniak.drive.utils.shareText
class ShareLinkContainerView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
private var shareLink: ShareLink? = null
private var urlValue: String = ""
private lateinit var currentFile: File
val binding by lazy { ViewShareLinkContainerBinding.inflate(LayoutInflater.from(context), this, true) }
fun setup(
file: File,
shareLink: ShareLink? = null,
onTitleClicked: ((shareLink: ShareLink?) -> Unit)? = null,
onSettingsClicked: ((shareLink: ShareLink) -> Unit)? = null,
) {
currentFile = file
this.shareLink = shareLink
isVisible = true
urlValue = file.shareLink?.url ?: ""
selectUi(file.isDropBox())
if (!file.isDropBox()) {
binding.titleContainer.setOnClickListener { onTitleClicked?.invoke(this.shareLink) }
binding.shareLinkSettings.setOnClickListener {
this.shareLink?.let { shareLink -> onSettingsClicked?.invoke(shareLink) }
}
binding.shareLinkButton.setOnClickListener { this.shareLink?.url?.let(context::shareText) }
}
}
fun update(shareLink: ShareLink? = null) {
this.shareLink = shareLink
if (shareLink == null) urlValue = ""
selectUi()
}
fun setupMyKSuitePlusChip(canCreateShareLink: Boolean, showMyKSuitePlusAd: () -> Unit) = with(binding) {
if ([email protected] != null || currentFile.isDropBox()) return@with
shareLinkMyKSuiteChip.isGone = canCreateShareLink
titleContainer.isEnabled = canCreateShareLink
titleContainer.isClickable = canCreateShareLink
shareLinkSettings.isEnabled = canCreateShareLink
shareLinkButton.isEnabled = canCreateShareLink
root.apply {
if (!canCreateShareLink) setOnClickListener { showMyKSuitePlusAd() } else setOnClickListener(null)
isClickable = !canCreateShareLink
isEnabled = !canCreateShareLink
}
}
private fun selectUi(isDropbox: Boolean = false) {
when {
isDropbox -> {
setDropboxUi()
binding.shareLinkSwitch.isGone = true
}
shareLink == null && urlValue.isBlank() -> {
context?.trackShareRightsEvent("restrictedShareLink")
setRestrictedUi()
}
else -> {
context?.trackShareRightsEvent("publicShareLink")
setPublicUi()
}
}
}
private fun setDropboxUi() {
setUi(
iconId = R.drawable.ic_folder_dropbox,
title = context.getString(R.string.dropboxSharedLinkTitle),
containerVisibility = false,
status = context.getString(R.string.dropboxSharedLinkDescription),
)
}
private fun setRestrictedUi() {
setUi(
iconId = R.drawable.ic_lock,
title = context.getString(R.string.restrictedSharedLinkTitle),
containerVisibility = false,
status = context.getString(R.string.shareLinkRestrictedRightDescription, currentFile.getTypeName(context)),
)
}
private fun setPublicUi() {
setUi(
iconId = R.drawable.ic_unlock,
title = context.getString(R.string.publicSharedLinkTitle),
containerVisibility = true,
status = getShareLinkPublicRightDescription(),
)
}
private fun setUi(iconId: Int, title: String, containerVisibility: Boolean, status: String) = with(binding) {
shareLinkIcon.setImageResource(iconId)
shareLinkTitle.text = title
shareLinkBottomContainer.isVisible = containerVisibility
shareLinkStatus.text = status
}
private fun getShareLinkPublicRightDescription(): String {
val resId = R.string.shareLinkPublicRightDescription
val permission = context.getString(
if (shareLink?.capabilities?.canEdit == true) R.string.shareLinkOfficePermissionWriteTitle
else R.string.shareLinkOfficePermissionReadTitle
).lowercase()
val fileName = currentFile.getTypeName(context)
val password = if (shareLink?.right == ShareLink.ShareLinkFilePermission.PASSWORD) {
context.getString(R.string.shareLinkPublicRightDescriptionPassword)
} else {
""
}
val validityDate = if (shareLink?.validUntil != null) {
context.getString(R.string.shareLinkPublicRightDescriptionDate, shareLink?.validUntil?.format(formatFullDate))
} else {
""
}
return context.getString(resId, permission, fileName, password, validityDate)
}
private fun File.getTypeName(context: Context): String = context.getString(getTypeName(isFolder(), hasOnlyOffice))
companion object {
const val formatFullDate = "dd MMM yyyy - HH:mm"
fun getTypeName(isFolder: Boolean, isOnlyOffice: Boolean): Int {
return when {
isFolder -> R.string.shareLinkTypeFolder
isOnlyOffice -> R.string.shareLinkTypeDocument
else -> R.string.shareLinkTypeFile
}
}
}
}