ContactsFragment.kt 4.29 KB
Newer Older
1
2
package org.dpppt.android.app.contacts

3
4
import android.content.Intent
import android.net.Uri
5
6
7
8
9
10
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
11
import androidx.navigation.fragment.findNavController
12
13
import mobi.lab.mvvm.MvvmFragment
import org.dpppt.android.app.R
14
import org.dpppt.android.app.common.util.DateUtils
15
16
import org.dpppt.android.app.common.util.FragmentBindingHolder
import org.dpppt.android.app.common.util.ViewBindingHolder
17
18
import org.dpppt.android.app.common.util.ViewModelFactory
import org.dpppt.android.app.common.util.addUrlSpans
19
import org.dpppt.android.app.common.util.exhaustive
20
import org.dpppt.android.app.common.util.setTextWithBoldSpans
21
import org.dpppt.android.app.common.util.setTextWithClickableSpan
22
23
import org.dpppt.android.app.databinding.FragmentContactsBinding
import org.dpppt.android.app.di.Injector
24
import org.dpppt.android.app.main.model.InfectedStatus
Aleks Krause's avatar
Aleks Krause committed
25
import org.dpppt.android.app.util.startActivitySafely
26
27
28
import org.dpppt.android.app.viewmodel.TracingViewModel
import javax.inject.Inject

29
class ContactsFragment : MvvmFragment(R.layout.fragment_contacts), ViewBindingHolder<FragmentContactsBinding> by FragmentBindingHolder() {
30
31
32
33
34
35

    @Inject
    lateinit var factory: ViewModelFactory

    override val viewModel: ContactsViewModel by viewModels { factory }

36
    private val tracingViewModel: TracingViewModel by viewModels { factory }
37
38
39
40
41

    init {
        Injector.inject(this)
    }

42
43
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return createBinding(FragmentContactsBinding.inflate(inflater), this)
44
45
46
47
48
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViewModel()
49
        initClickableText()
50
51
52
53
54
55
        requireBinding {
            buttonBack.setOnClickListener { viewModel.backClicked() }
            buttonBackHome.setOnClickListener { viewModel.backClicked() }
            learnMoreView.setOnClickListener { viewModel.learnMoreClicked() }
            healthBoardRecommendationsView.setOnClickListener { viewModel.healthBoardRecommendationsClicked() }
        }
56
57
58
59
    }

    private fun initViewModel() {
        tracingViewModel.appStatusLiveData.observe(requireActivity(), Observer(viewModel::contactsChanged))
60
        viewModel.infectedStatus.onEachNotNull(this::showContacts)
61
62
63
64
65
66
67
        viewModel.action.onEachEvent {
            handleAction(it)
        }
    }

    private fun handleAction(action: ContactsViewModel.Action) {
        when (action) {
68
            is ContactsViewModel.Action.NavigateHome -> returnHomeWithoutRestart()
Harri Kirik's avatar
Harri Kirik committed
69
            is ContactsViewModel.Action.OpenContactsInfoUrl -> startActivitySafely(Intent(Intent.ACTION_VIEW).setData(Uri.parse(action.url)))
Aleks Krause's avatar
Aleks Krause committed
70
71
72
73
74
            is ContactsViewModel.Action.OpenHealthBoardRecommendationsUrl -> startActivitySafely(
                Intent(Intent.ACTION_VIEW).setData(
                    Uri.parse(action.url)
                )
            )
75
        }.exhaustive
76
77
    }

78
79
80
81
    private fun returnHomeWithoutRestart() {
        findNavController().popBackStack()
    }

82
    private fun showContacts(contacts: InfectedStatus) {
83
        if (contacts !is InfectedStatus.HasContacts) {
84
            return
85
        }
86
87
        requireBinding {
            titleView.text = resources.getQuantityString(R.plurals.contacts_detected_title, contacts.num, contacts.num)
88
            setupStayHomeText(contacts)
89
        }
90
    }
91

92
93
94
95
96
97
    private fun FragmentContactsBinding.setupStayHomeText(contacts: InfectedStatus.HasContacts) {
        val dateString = DateUtils.getFormattedLocalDate(contacts.stayHomeUntilDate)
        val message = getString(R.string.contacts_detected_text_stay_home, dateString)
        descriptionStayHomeView.setTextWithBoldSpans(message, dateString)
    }

98
    private fun initClickableText() {
99
100
        requireBinding {
            descriptionCallView.addUrlSpans()
101
102
103
104
            healthBoardRecommendationsView.setTextWithClickableSpan(
                getString(R.string.contacts_health_board_recommendations),
                getString(R.string.contacts_health_board_recommendations_clickable_part),
            ) { viewModel.healthBoardRecommendationsClicked() }
105
106
        }
    }
107
}