Skip to content

Commit 4ca7ce3

Browse files
authored
DROID-3132 Media | Fix | Fix download errors (#2743)
1 parent 08c6261 commit 4ca7ce3

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

app/src/main/java/com/anytypeio/anytype/ui/chats/ChatFragment.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ class ChatFragment : Fragment() {
197197
MediaActivity.start(
198198
context = requireContext(),
199199
mediaType = MediaActivity.TYPE_VIDEO,
200-
obj = attachment.obj
200+
obj = attachment.obj,
201+
space = space
201202
)
202203
}
203204
)
@@ -395,7 +396,8 @@ class ChatFragment : Fragment() {
395396
context = requireContext(),
396397
mediaType = MediaActivity.TYPE_IMAGE,
397398
objects = command.objects,
398-
index = command.index
399+
index = command.index,
400+
space = space
399401
)
400402
}.onFailure {
401403
Timber.e(it, "Error while launching media image viewer")
@@ -465,7 +467,8 @@ class ChatFragment : Fragment() {
465467
context = requireContext(),
466468
mediaType = MediaActivity.TYPE_AUDIO,
467469
obj = command.obj,
468-
name = command.name
470+
name = command.name,
471+
space = space
469472
)
470473
}.onFailure {
471474
Timber.e(it, "Error while launching audio player")

app/src/main/java/com/anytypeio/anytype/ui/editor/EditorFragment.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,8 @@ open class EditorFragment : NavigationFragment<FragmentEditorBinding>(R.layout.f
11491149
MediaActivity.start(
11501150
context = requireContext(),
11511151
mediaType = MediaActivity.TYPE_IMAGE,
1152-
obj = command.obj
1152+
obj = command.obj,
1153+
space = space
11531154
)
11541155
}.onFailure {
11551156
Timber.e(it, "Error while launching media image viewer")
@@ -1325,7 +1326,8 @@ open class EditorFragment : NavigationFragment<FragmentEditorBinding>(R.layout.f
13251326
context = requireContext(),
13261327
mediaType = MediaActivity.TYPE_VIDEO,
13271328
obj = command.obj,
1328-
name = ""
1329+
name = "",
1330+
space = space
13291331
)
13301332
}.onFailure {
13311333
Timber.e(it, "Error while launching video player")
@@ -1337,7 +1339,8 @@ open class EditorFragment : NavigationFragment<FragmentEditorBinding>(R.layout.f
13371339
context = requireContext(),
13381340
mediaType = MediaActivity.TYPE_AUDIO,
13391341
obj = command.obj,
1340-
name = command.name
1342+
name = command.name,
1343+
space = space
13411344
)
13421345
}.onFailure {
13431346
Timber.e(it, "Error while launching audio player")

app/src/main/java/com/anytypeio/anytype/ui/media/MediaActivity.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.lifecycle.lifecycleScope
1717
import androidx.lifecycle.repeatOnLifecycle
1818
import com.anytypeio.anytype.BuildConfig
1919
import com.anytypeio.anytype.core_models.Id
20+
import com.anytypeio.anytype.core_models.primitives.SpaceId
2021
import com.anytypeio.anytype.core_utils.ext.toast
2122
import com.anytypeio.anytype.di.common.componentManager
2223
import com.anytypeio.anytype.presentation.media.MediaViewModel
@@ -36,6 +37,8 @@ class MediaActivity : ComponentActivity() {
3637

3738
private val vm by viewModels<MediaViewModel> { factory }
3839

40+
private val space get() = intent.getStringExtra(EXTRA_SPACE_ID)
41+
3942
override fun onCreate(savedInstanceState: Bundle?) {
4043
inject()
4144
super.onCreate(savedInstanceState)
@@ -72,7 +75,17 @@ class MediaActivity : ComponentActivity() {
7275
images = state.images,
7376
index = state.currentIndex,
7477
onBackClick = { finish() },
75-
onDownloadClick = vm::onDownloadObject,
78+
onDownloadClick = { obj ->
79+
val givenSpace = space
80+
if (givenSpace != null) {
81+
vm.onDownloadObject(
82+
id = obj,
83+
space = SpaceId(space.orEmpty())
84+
)
85+
} else {
86+
toast("Space not found")
87+
}
88+
},
7689
onDeleteClick = vm::onDeleteObject
7790
)
7891
}
@@ -142,20 +155,23 @@ class MediaActivity : ComponentActivity() {
142155
private const val TYPE_UNKNOWN = 0
143156

144157
private const val EXTRA_OBJECTS = "extra_object_ids"
158+
private const val EXTRA_SPACE_ID = "extra_space_id"
145159
private const val EXTRA_IMAGE_INDEX = "extra_image_index"
146160
private const val EXTRA_MEDIA_TYPE = "extra_media_type"
147161
private const val EXTRA_MEDIA_NAME = "extra_media_name"
148162

149163
fun start(
150164
context: Context,
151165
obj: Id,
166+
space: Id,
152167
mediaType: Int,
153168
name: String? = null
154169
) {
155170
val intent = Intent(context, MediaActivity::class.java).apply {
156171
putStringArrayListExtra(EXTRA_OBJECTS, arrayListOf(obj))
157172
putExtra(EXTRA_MEDIA_TYPE, mediaType)
158173
putExtra(EXTRA_MEDIA_NAME, name)
174+
putExtra(EXTRA_SPACE_ID, space)
159175
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
160176
}
161177
context.startActivity(intent)
@@ -167,12 +183,14 @@ class MediaActivity : ComponentActivity() {
167183
fun start(
168184
context: Context,
169185
objects: List<Id>,
186+
space: Id,
170187
mediaType: Int,
171188
name: String? = null,
172189
index: Int = 0
173190
) {
174191
val intent = Intent(context, MediaActivity::class.java).apply {
175192
putStringArrayListExtra(EXTRA_OBJECTS, ArrayList(objects))
193+
putExtra(EXTRA_SPACE_ID, space)
176194
putExtra(EXTRA_MEDIA_TYPE, mediaType)
177195
putExtra(EXTRA_MEDIA_NAME, name)
178196
putExtra(EXTRA_IMAGE_INDEX, index)

core-utils/src/main/java/com/anytypeio/anytype/core_utils/ext/FragmentExtensions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.anytypeio.anytype.core_utils.ext
22

3+
import android.app.Activity
34
import android.content.Intent
45
import android.net.Uri
56
import android.os.Parcelable

presentation/src/main/java/com/anytypeio/anytype/presentation/media/MediaViewModel.kt

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.ViewModelProvider
55
import androidx.lifecycle.viewModelScope
66
import com.anytypeio.anytype.core_models.Id
7+
import com.anytypeio.anytype.core_models.ObjectWrapper
8+
import com.anytypeio.anytype.core_models.Relations
9+
import com.anytypeio.anytype.core_models.primitives.SpaceId
710
import com.anytypeio.anytype.domain.base.onFailure
811
import com.anytypeio.anytype.domain.base.onSuccess
912
import com.anytypeio.anytype.domain.download.DownloadFile
1013
import com.anytypeio.anytype.domain.misc.UrlBuilder
14+
import com.anytypeio.anytype.domain.`object`.FetchObject
1115
import com.anytypeio.anytype.domain.objects.SetObjectListIsArchived
16+
import com.anytypeio.anytype.domain.search.SearchObjects
1217
import com.anytypeio.anytype.presentation.common.BaseViewModel
1318
import kotlinx.coroutines.flow.MutableSharedFlow
1419
import kotlinx.coroutines.flow.MutableStateFlow
@@ -21,7 +26,8 @@ import timber.log.Timber
2126
class MediaViewModel(
2227
private val urlBuilder: UrlBuilder,
2328
private val setObjectListIsArchived: SetObjectListIsArchived,
24-
private val downloadFile: DownloadFile
29+
private val downloadFile: DownloadFile,
30+
private val fetchObject: FetchObject
2531
) : BaseViewModel() {
2632

2733
private val _commands = MutableSharedFlow<Command>()
@@ -93,14 +99,34 @@ class MediaViewModel(
9399
}
94100
}
95101

96-
fun onDownloadObject(id: Id) {
97-
Timber.d("onDownload: $id")
102+
fun onDownloadObject(id: Id, space: SpaceId) {
103+
Timber.d("onDownload: $id, space: $space")
98104
viewModelScope.launch {
105+
val obj = fetchObject.async(
106+
params = FetchObject.Params(
107+
space = space,
108+
obj = id,
109+
keys = listOf(
110+
Relations.ID,
111+
Relations.NAME,
112+
Relations.FILE_EXT
113+
)
114+
)
115+
).getOrNull()
116+
117+
val name: String
118+
119+
if (obj != null) {
120+
val wrapper = ObjectWrapper.File(obj.map)
121+
name = wrapper.name + "." + wrapper.fileExt
122+
} else {
123+
name = ""
124+
}
125+
99126
downloadFile.run(
100127
DownloadFile.Params(
101128
url = urlBuilder.original(id),
102-
// TODO add name
103-
name = ""
129+
name = name
104130
)
105131
).proceed(
106132
failure = {
@@ -149,14 +175,16 @@ class MediaViewModel(
149175
class Factory @Inject constructor(
150176
private val urlBuilder: UrlBuilder,
151177
private val setObjectListIsArchived: SetObjectListIsArchived,
152-
private val downloadFile: DownloadFile
178+
private val downloadFile: DownloadFile,
179+
private val fetchObject: FetchObject
153180
) : ViewModelProvider.Factory {
154181
@Suppress("UNCHECKED_CAST")
155182
override fun <T : ViewModel> create(modelClass: Class<T>): T {
156183
return MediaViewModel(
157184
urlBuilder = urlBuilder,
158185
setObjectListIsArchived = setObjectListIsArchived,
159-
downloadFile = downloadFile
186+
downloadFile = downloadFile,
187+
fetchObject = fetchObject
160188
) as T
161189
}
162190
}

0 commit comments

Comments
 (0)