SpaceExploreActivity.kt 4.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Copyright (c) 2021 New Vector Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package im.vector.app.features.spaces

import android.content.Context
import android.content.Intent
import android.os.Bundle
Valere's avatar
Valere committed
22
import androidx.fragment.app.Fragment
Valere's avatar
Valere committed
23
import androidx.fragment.app.FragmentManager
24
import com.airbnb.mvrx.MvRx
25
import com.airbnb.mvrx.viewModel
26
import im.vector.app.R
27
import im.vector.app.core.di.ScreenComponent
28
29
30
import im.vector.app.core.extensions.commitTransaction
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.databinding.ActivitySimpleBinding
Valere's avatar
Valere committed
31
import im.vector.app.features.matrixto.MatrixToBottomSheet
32
33
import im.vector.app.features.spaces.explore.SpaceDirectoryArgs
import im.vector.app.features.spaces.explore.SpaceDirectoryFragment
34
35
36
37
import im.vector.app.features.spaces.explore.SpaceDirectoryState
import im.vector.app.features.spaces.explore.SpaceDirectoryViewEvents
import im.vector.app.features.spaces.explore.SpaceDirectoryViewModel
import javax.inject.Inject
38

Valere's avatar
Valere committed
39
class SpaceExploreActivity : VectorBaseActivity<ActivitySimpleBinding>(), SpaceDirectoryViewModel.Factory, MatrixToBottomSheet.InteractionListener {
40
41
42
43
44
45

    @Inject lateinit var spaceDirectoryViewModelFactory: SpaceDirectoryViewModel.Factory

    override fun injectWith(injector: ScreenComponent) {
        injector.inject(this)
    }
46
47

    override fun getBinding(): ActivitySimpleBinding = ActivitySimpleBinding.inflate(layoutInflater)
48
49
50
51

    override fun getTitleRes(): Int = R.string.space_explore_activity_title

    val sharedViewModel: SpaceDirectoryViewModel by viewModel()
52

Valere's avatar
Valere committed
53
54
55
56
57
58
59
    private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
        override fun onFragmentAttached(fm: FragmentManager, f: Fragment, context: Context) {
            if (f is MatrixToBottomSheet) {
                f.interactionListener = this@SpaceExploreActivity
            }
            super.onFragmentAttached(fm, f, context)
        }
Valere's avatar
Valere committed
60
61
62
63
64
65
66

        override fun onFragmentDetached(fm: FragmentManager, f: Fragment) {
            if (f is MatrixToBottomSheet) {
                f.interactionListener = null
            }
            super.onFragmentDetached(fm, f)
        }
Valere's avatar
Valere committed
67
68
    }

69
70
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
Valere's avatar
Valere committed
71
        supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, false)
72
73
74

        if (isFirstCreation()) {
            val simpleName = SpaceDirectoryFragment::class.java.simpleName
75
            val args = intent?.getParcelableExtra<SpaceDirectoryArgs>(MvRx.KEY_ARG)
76
77
78
            if (supportFragmentManager.findFragmentByTag(simpleName) == null) {
                supportFragmentManager.commitTransaction {
                    replace(R.id.simpleFragmentContainer,
79
                            SpaceDirectoryFragment::class.java,
80
81
82
83
84
85
                            Bundle().apply { this.putParcelable(MvRx.KEY_ARG, args) },
                            simpleName
                    )
                }
            }
        }
86
87
88
89
90
91
92
93
94

        sharedViewModel.observeViewEvents {
            when (it) {
                SpaceDirectoryViewEvents.Dismiss -> {
                    finish()
                }
                is SpaceDirectoryViewEvents.NavigateToRoom -> {
                    navigator.openRoom(this, it.roomId)
                }
Valere's avatar
Valere committed
95
96
97
                is SpaceDirectoryViewEvents.NavigateToMxToBottomSheet -> {
                    MatrixToBottomSheet.withLink(it.link, this).show(supportFragmentManager, "ShowChild")
                }
98
99
            }
        }
100
101
    }

Valere's avatar
Valere committed
102
103
104
105
106
    override fun onDestroy() {
        supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks)
        super.onDestroy()
    }

107
108
109
110
111
112
113
    companion object {
        fun newIntent(context: Context, spaceId: String): Intent {
            return Intent(context, SpaceExploreActivity::class.java).apply {
                putExtra(MvRx.KEY_ARG, SpaceDirectoryArgs(spaceId))
            }
        }
    }
114
115
116

    override fun create(initialState: SpaceDirectoryState): SpaceDirectoryViewModel =
            spaceDirectoryViewModelFactory.create(initialState)
Valere's avatar
Valere committed
117
118
119
120

    override fun navigateToRoom(roomId: String) {
        navigator.openRoom(this, roomId)
    }
121
}