summaryrefslogtreecommitdiff
path: root/app/src/main/java/foundation/e/advancedprivacy/features/trackers/trackerdetails/TrackerDetailsFragment.kt
blob: 481c809325b9e47a55d8a3f3502613b7e0d564fa (plain)
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
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
/*
 * Copyright (C) 2023 MURENA SAS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.advancedprivacy.features.trackers.trackerdetails

import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.core.content.ContextCompat.getColor
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.divider.MaterialDividerItemDecoration
import com.google.android.material.snackbar.Snackbar
import foundation.e.advancedprivacy.R
import foundation.e.advancedprivacy.common.NavToolbarFragment
import foundation.e.advancedprivacy.databinding.TrackerdetailsFragmentBinding
import foundation.e.advancedprivacy.features.trackers.setupDisclaimerBlock
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf

class TrackerDetailsFragment : NavToolbarFragment(R.layout.trackerdetails_fragment) {

    private val args: TrackerDetailsFragmentArgs by navArgs()
    private val viewModel: TrackerDetailsViewModel by viewModel { parametersOf(args.trackerId) }

    private lateinit var binding: TrackerdetailsFragmentBinding

    override fun getTitle(): CharSequence {
        return ""
    }

    private fun displayToast(message: String) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT)
            .show()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding = TrackerdetailsFragmentBinding.bind(view)

        binding.blockAllToggle.setOnClickListener {
            viewModel.onToggleBlockAll(binding.blockAllToggle.isChecked)
        }

        binding.apps.apply {
            layoutManager = LinearLayoutManager(requireContext())
            setHasFixedSize(true)
            addItemDecoration(
                MaterialDividerItemDecoration(requireContext(), LinearLayoutManager.VERTICAL).apply {
                    dividerColor = getColor(requireContext(), R.color.divider)
                }
            )
            adapter = TrackerAppsAdapter(viewModel)
        }

        setupDisclaimerBlock(binding.disclaimerBlockTrackers.root, viewModel::onClickLearnMore)

        listenViewModel()
    }

    private fun listenViewModel() {
        with(viewLifecycleOwner) {
            lifecycleScope.launch {
                repeatOnLifecycle(Lifecycle.State.STARTED) {
                    viewModel.singleEvents.collect(::handleEvents)
                }
            }

            lifecycleScope.launch {
                repeatOnLifecycle(Lifecycle.State.STARTED) {
                    viewModel.doOnStartedState()
                }
            }

            lifecycleScope.launch {
                repeatOnLifecycle(Lifecycle.State.STARTED) {
                    render(viewModel.state.value)
                    viewModel.state.collect(::render)
                }
            }
        }
    }

    private fun handleEvents(event: TrackerDetailsViewModel.SingleEvent) {
        when (event) {
            is TrackerDetailsViewModel.SingleEvent.ErrorEvent ->
                displayToast(getString(event.errorResId))
            is TrackerDetailsViewModel.SingleEvent.ToastTrackersControlDisabled ->
                Snackbar.make(
                    binding.root,
                    R.string.apptrackers_tracker_control_disabled_message,
                    Snackbar.LENGTH_LONG
                ).show()
            is TrackerDetailsViewModel.SingleEvent.OpenUrl -> {
                try {
                    startActivity(Intent(Intent.ACTION_VIEW, event.url))
                } catch (e: ActivityNotFoundException) {
                    Toast.makeText(
                        requireContext(),
                        R.string.error_no_activity_view_url,
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }

    private fun render(state: TrackerDetailsState) {
        setTitle(state.tracker?.label)
        binding.subtitle.text = getString(R.string.trackerdetails_subtitle, state.tracker?.label)
        binding.dataAppCount.apply {
            primaryMessage.setText(R.string.trackerdetails_app_count_primary)
            number.text = state.detectedCount.toString()
            secondaryMessage.setText(R.string.trackerdetails_app_count_secondary)
        }

        binding.dataBlockedLeaks.apply {
            primaryMessage.setText(R.string.trackerdetails_blocked_leaks_primary)
            number.text = state.blockedCount.toString()
            secondaryMessage.text = getString(R.string.trackerdetails_blocked_leaks_secondary, state.leakedCount.toString())
        }

        binding.blockAllToggle.isChecked = state.isBlockAllActivated

        binding.apps.post {
            (binding.apps.adapter as TrackerAppsAdapter?)?.updateDataSet(state.appList)
        }
    }
}