FilesFragment.kt 5.17 KB
Newer Older
1 2 3 4 5 6 7 8 9
package chat.rocket.android.files.ui

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
10
import androidx.fragment.app.Fragment
11 12
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.DividerItemDecoration.HORIZONTAL
13 14
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
15 16 17 18 19
import chat.rocket.android.R
import chat.rocket.android.chatroom.ui.ChatRoomActivity
import chat.rocket.android.files.adapter.FilesAdapter
import chat.rocket.android.files.presentation.FilesPresenter
import chat.rocket.android.files.presentation.FilesView
20
import chat.rocket.android.files.uimodel.FileUiModel
21 22 23
import chat.rocket.android.helper.EndlessRecyclerViewScrollListener
import chat.rocket.android.helper.ImageHelper
import chat.rocket.android.player.PlayerActivity
24
import chat.rocket.android.server.domain.AnalyticsTrackingInteractor
25 26 27
import chat.rocket.android.util.extensions.inflate
import chat.rocket.android.util.extensions.showToast
import chat.rocket.android.util.extensions.ui
28 29
import chat.rocket.android.util.helper.analytics.AnalyticsManager
import chat.rocket.android.util.helper.analytics.event.ScreenViewEvent
30 31 32 33 34 35 36 37 38 39 40 41
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_files.*
import javax.inject.Inject

fun newInstance(chatRoomId: String): Fragment {
    return FilesFragment().apply {
        arguments = Bundle(1).apply {
            putString(BUNDLE_CHAT_ROOM_ID, chatRoomId)
        }
    }
}

42
internal const val TAG_FILES_FRAGMENT = "FilesFragment"
43 44 45 46 47
private const val BUNDLE_CHAT_ROOM_ID = "chat_room_id"

class FilesFragment : Fragment(), FilesView {
    @Inject
    lateinit var presenter: FilesPresenter
48 49
    @Inject
    lateinit var analyticsTrackingInteractor: AnalyticsTrackingInteractor
50
    private val adapter: FilesAdapter =
51
        FilesAdapter { fileUiModel -> presenter.openFile(fileUiModel) }
52
    private val linearLayoutManager = LinearLayoutManager(context)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    private lateinit var chatRoomId: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        AndroidSupportInjection.inject(this)

        val bundle = arguments
        if (bundle != null) {
            chatRoomId = bundle.getString(BUNDLE_CHAT_ROOM_ID)
        } else {
            requireNotNull(bundle) { "no arguments supplied when the fragment was instantiated" }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = container?.inflate(R.layout.fragment_files)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupRecyclerView()
        presenter.loadFiles(chatRoomId)
77 78

        if (analyticsTrackingInteractor.get()) {
79
            AnalyticsManager.logScreenView(ScreenViewEvent.Files)
80
        }
81 82
    }

83
    override fun showFiles(dataSet: List<FileUiModel>, total: Long) {
84 85 86 87 88 89 90 91 92
        setupToolbar(total)
        if (adapter.itemCount == 0) {
            adapter.prependData(dataSet)
            if (dataSet.size >= 30) {
                recycler_view.addOnScrollListener(object :
                    EndlessRecyclerViewScrollListener(linearLayoutManager) {
                    override fun onLoadMore(
                        page: Int,
                        totalItemsCount: Int,
93
                        recyclerView: RecyclerView
94 95 96 97 98
                    ) {
                        presenter.loadFiles(chatRoomId)
                    }
                })
            }
divyanshu's avatar
divyanshu committed
99
            group_no_file.isVisible = dataSet.isEmpty()
100 101 102 103 104 105 106 107
        } else {
            adapter.appendData(dataSet)

        }
    }

    override fun playMedia(url: String) {
        ui {
108
            PlayerActivity.play(it, url)
109 110 111 112 113
        }
    }

    override fun openImage(url: String, name: String) {
        ui {
114
            ImageHelper.openImage(root_layout.context, url, name)
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
        }
    }

    override fun openDocument(uri: Uri) {
        ui {
            startActivity(Intent(Intent.ACTION_VIEW, uri))
        }
    }

    override fun showMessage(resId: Int) {
        ui {
            showToast(resId)
        }
    }

    override fun showMessage(message: String) {
        ui {
            showToast(message)
        }
    }

    override fun showGenericErrorMessage() {
        showMessage(getString(R.string.msg_generic_error))
    }

    override fun showLoading() {
        ui { view_loading.isVisible = true }
    }

    override fun hideLoading() {
        ui { view_loading.isVisible = false }
    }

    private fun setupRecyclerView() {
        ui {
            recycler_view.layoutManager = linearLayoutManager
151
            recycler_view.addItemDecoration(DividerItemDecoration(it, HORIZONTAL))
152 153 154 155 156
            recycler_view.adapter = adapter
        }
    }

    private fun setupToolbar(totalFiles: Long) {
157 158 159 160
        (activity as ChatRoomActivity).let {
            it.showToolbarTitle(getString(R.string.title_files_total, totalFiles))
            it.hideToolbarChatRoomIcon()
        }
161 162
    }
}