Skip to main content

Getting Timeline Trash Bin

Trash bin is a section of cloud storage of media files.

A media file uploaded to the cloud and then deleted from Timeline is moved to the trash bin. If a media file has not been uploaded to the cloud, then it cannot be moved to the trash bin when deleted.

You can restore the media file back to Timeline, or delete it permanently.

Getting Photos from the Backend#

Getting a list of media files from cloud storage is completely similar to Timeline.

To get a list of media elements from the cloud, use the following code example.

important

Since the reading procedure takes a significant amount of time (and, on the other hand, does not require permissions from the user), it should be started as soon as the user is logged into the application.

var disposable: Disposable? = null
// If true, then reading of the full list of photos and videos from the backend is // performed. This can take a long time. If false, then reading is performed incrementally, // only the last changes are read.var fullRefresh: Boolean = true
// ...
// Returns Completable, which completes when the operation ends.// App has to subscribe to the returned Completable to receive updates.disposable = PhotoManager.timelineTrash.reloadBackend(fullRefresh)        .observeOn(AndroidSchedulers.mainThread())        .subscribeBy(                onComplete = {                    // Handle it                },                onError = { // it: Throwable                    // Handle it                }        )
// ...
// App must call dispose() on disposable that returned by the subscribe() method,// when it is no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()

Getting a List of Photos#

The Cloudike Photos SDK provides photo information to the client application as a paged list of PhotoItem objects.

To access the list of photos, use the following code example.

important

The createPagingObservable() function creates new Observable that emits a PagingData of PhotoItems every time there is a change to the local database. To display the paginated list in the RecyclerView, you must use PagingDataAdapter from the Android Paging Library.

The Cloudike Photos SDK uses the following Android Paging Library.

// Pagingimplementation 'androidx.paging:paging-runtime-ktx:3.1.1'implementation 'androidx.paging:paging-rxjava2-ktx:3.1.1'
// Declare view modelprivate class TrashViewModel(    private val trash: TimelineTrash) : ViewModel() {
    // NOTE: Your app can use either Observable or Flow. Both of them are created here for demo purposes only.
    val contentObservable: Observable<PagingData<PhotoItem>> by lazy {        trash.createPagingObservable()            .cachedIn(viewModelScope)    }
    val contentFlow: Flow<PagingData<PhotoItem>> by lazy {        trash.createPagingFlow()            .cachedIn(viewModelScope)    }}
// Factory is required to pass timeline trash to the view modelprivate class TrashViewModelFactory(    private val trash: TimelineTrash) : ViewModelProvider.NewInstanceFactory() {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {        return TrashViewModel(trash) as T    }}
// Fragmentclass TimelineTrashFrg : Fragment() {
    private val adapter: TimelineTrashAdapter // derived from PagingDataAdapter    private val timelineTrash = PhotoManager.timelineTrash    private val viewModel: TimelineTrashViewModel by viewModels { TimelineTrashViewModelFactory(timeline) }    private var disposable: Disposable? = null
    override fun onStart() {        super.onStart()        // You have to subscribe to the returned Observable to receive updates.        disposable = viewModel.contentObservable            .subscribe { pagingData ->                // Update adapter                adapter.submitData(this.lifecycle, pagingData)            }    }
    override fun onStop() {        super.onStop()        // You must call dispose() on disposable that returned by the subscribe() method,        // when it is no longer needed, for example in your fragment’s onStop() or onPause().        disposable?.dispose()    }}

As an alternative to Observable, Flow is also available for getting media items of the timeline trash:

    override fun onStart() {        super.onStart()        lifecycleScope.launch {            viewModel.contentFlow                .collectLatest {                    adapter.submitData(it)                }        }    }

Links: PagingData\<T>, PhotoItem.

Restore Photos/Videos from the Trash#

Media files uploaded to the cloud and then deleted from Timeline are moved to the trash.

To restore photos from trash back to the timeline, use the following code example.

var disposable: Disposable? = null
// A list of items to remove from the cloud storage.var photoList: List<PhotoList> = listOf()
// ...
// Returns completable, which completes when the operation ends.// You have to subscribe to the returned completable in order to receive updates.disposable = PhotoManager.timelineTrash.operations        .restorePhotos(photoItems)        .observeOn(AndroidSchedulers.mainThread())        .subscribeBy(                onComplete = {                    // Handle it                },                onError = { // it: Throwable                    // Handle it                }        )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it no is longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()

Links: PhotoItem.

Permanent Removal of Photos/Videos from the Trash#

To permanently delete photos from the trash bin, use the following code example.

var disposable: Disposable? = null
// A list of items to remove from the cloud storage.var photoList: List<PhotoList> = listOf()
// ...
// Returns completable, which completes when the operation ends.// You have to subscribe to the returned completable in order to receive updates.disposable = PhotoManager.timelineTrash.operations        .deletePhotosPermanently(photoItems)        .observeOn(AndroidSchedulers.mainThread())        .subscribeBy(                onComplete = {                    // Handle it                },                onError = { // it: Throwable                    // Handle it                }        )
// ...
// You must call dispose() on disposable that returned by subscribe() method,// when it is no longer needed, for example in your fragment’s onStop() or onPause().disposable?.dispose()

Links: PhotoItem.