-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
RepoFragment.kt
121 lines (109 loc) · 4.33 KB
/
RepoFragment.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
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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 com.android.example.github.ui.repo
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.doOnPreDraw
import androidx.databinding.DataBindingComponent
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.transition.TransitionInflater
import com.android.example.github.AppExecutors
import com.android.example.github.R
import com.android.example.github.binding.FragmentDataBindingComponent
import com.android.example.github.databinding.RepoFragmentBinding
import com.android.example.github.di.Injectable
import com.android.example.github.ui.common.RetryCallback
import com.android.example.github.util.autoCleared
import javax.inject.Inject
/**
* The UI Controller for displaying a Github Repo's information with its contributors.
*/
class RepoFragment : Fragment(), Injectable {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
val repoViewModel: RepoViewModel by viewModels {
viewModelFactory
}
@Inject
lateinit var appExecutors: AppExecutors
// mutable for testing
var dataBindingComponent: DataBindingComponent = FragmentDataBindingComponent(this)
var binding by autoCleared<RepoFragmentBinding>()
private val params by navArgs<RepoFragmentArgs>()
private var adapter by autoCleared<ContributorAdapter>()
private fun initContributorList(viewModel: RepoViewModel) {
viewModel.contributors.observe(viewLifecycleOwner, Observer { listResource ->
// we don't need any null checks here for the adapter since LiveData guarantees that
// it won't call us if fragment is stopped or not started.
if (listResource?.data != null) {
adapter.submitList(listResource.data)
} else {
adapter.submitList(emptyList())
}
})
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val dataBinding = DataBindingUtil.inflate<RepoFragmentBinding>(
inflater,
R.layout.repo_fragment,
container,
false
)
dataBinding.retryCallback = object : RetryCallback {
override fun retry() {
repoViewModel.retry()
}
}
binding = dataBinding
sharedElementReturnTransition = TransitionInflater.from(requireContext())
.inflateTransition(R.transition.move)
return dataBinding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
repoViewModel.setId(params.owner, params.name)
binding.lifecycleOwner = viewLifecycleOwner
binding.repo = repoViewModel.repo
val adapter = ContributorAdapter(dataBindingComponent, appExecutors) {
contributor, imageView ->
val extras = FragmentNavigatorExtras(
imageView to contributor.login
)
findNavController().navigate(
RepoFragmentDirections.showUser(contributor.login, contributor.avatarUrl),
extras
)
}
this.adapter = adapter
binding.contributorList.adapter = adapter
postponeEnterTransition()
binding.contributorList.doOnPreDraw {
startPostponedEnterTransition()
}
initContributorList(repoViewModel)
}
}