]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.kt
Fix crash when adding domain settings with null domain. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / DomainsListFragment.kt
1 /*
2  * Copyright © 2017-2020,2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser Android is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser Android.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.fragments
21
22 import android.content.Context
23 import android.os.Bundle
24 import android.view.LayoutInflater
25 import android.view.View
26 import android.view.ViewGroup
27 import android.widget.AdapterView
28 import android.widget.AdapterView.OnItemClickListener
29 import android.widget.ListView
30
31 import androidx.fragment.app.Fragment
32
33 import com.google.android.material.floatingactionbutton.FloatingActionButton
34
35 import com.stoutner.privacybrowser.R
36 import com.stoutner.privacybrowser.activities.DomainsActivity
37
38 class DomainsListFragment : Fragment() {
39     // Declare the class variables.
40     private lateinit var dismissSnackbarInterface: DismissSnackbarInterface
41     private lateinit var saveDomainSettingsInterface: SaveDomainSettingsInterface
42
43     // Define the public dismiss snackbar interface.
44     interface DismissSnackbarInterface {
45         fun dismissSnackbar()
46     }
47
48     // Define the public save domain interface.
49     interface SaveDomainSettingsInterface {
50         fun saveDomainSettings(view: View)
51     }
52
53     override fun onAttach(context: Context) {
54         // Run the default commands.
55         super.onAttach(context)
56
57         // Populate the interfaces.
58         dismissSnackbarInterface = context as DismissSnackbarInterface
59         saveDomainSettingsInterface = context as SaveDomainSettingsInterface
60     }
61
62     override fun onCreateView(layoutInflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
63         // Inflate the layout.  The fragment will take care of attaching the root automatically.
64         val domainsListFragmentView = layoutInflater.inflate(R.layout.domains_list_fragment, container, false)
65
66         // Get a handle for the domains list view.
67         val domainsListView = domainsListFragmentView.findViewById<ListView>(R.id.domains_listview)
68
69         // Get a handle for the support fragment manager.
70         val supportFragmentManager = requireActivity().supportFragmentManager
71
72         // Handle clicks on the domains list view.
73         domainsListView.onItemClickListener = OnItemClickListener { _: AdapterView<*>, _: View, _: Int, id: Long ->
74             // Dismiss the snackbar if it is visible.
75             dismissSnackbarInterface.dismissSnackbar()
76
77             // Save the current domain settings if operating in two-paned mode and a domain is currently selected.
78             if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled) {
79                 // Get a handle for the domain settings fragment.
80                 val domainSettingsFragment = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container)!!
81
82                 // Get a handle for the domain settings fragment view.
83                 val domainSettingsFragmentView = domainSettingsFragment.requireView()
84
85                 // Save the domain settings.
86                 saveDomainSettingsInterface.saveDomainSettings(domainSettingsFragmentView)
87             }
88
89             // Store the new current domain database ID, converting it from long to int to match the format of the domains database.
90             DomainsActivity.currentDomainDatabaseId = id.toInt()
91
92             // Create an arguments bundle.
93             val argumentsBundle = Bundle()
94
95             // Add the current domain database ID to the arguments bundle.
96             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId)
97
98             // Create a new domains settings fragment instance.
99             val domainSettingsFragment = DomainSettingsFragment()
100
101             // Add the arguments bundle to the domain settings fragment.
102             domainSettingsFragment.arguments = argumentsBundle
103
104             // Check to see if the device is in two paned mode.
105             if (DomainsActivity.twoPanedMode) {  // The device in in two-paned mode.
106                 // Enable the delete menu item if the system is not waiting for a snackbar to be dismissed.
107                 if (!DomainsActivity.dismissingSnackbar) {
108                     // Enable the delete menu item.
109                     DomainsActivity.deleteMenuItem.isEnabled = true
110                 }
111
112                 // Display the domain settings fragment.
113                 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit()
114             } else { // The device in in single-paned mode
115                 // Save the domains listview position.
116                 DomainsActivity.domainsListViewPosition = domainsListView.firstVisiblePosition
117
118                 // Show the delete menu item if the system is not waiting for a snackbar to be dismissed.
119                 if (!DomainsActivity.dismissingSnackbar)
120                     DomainsActivity.deleteMenuItem.isVisible = true
121
122                 // Get a handle for the add domain floating action button.
123                 val addDomainFab = requireActivity().findViewById<FloatingActionButton>(R.id.add_domain_fab)
124
125                 // Hide the add domain FAB.
126                 addDomainFab.hide()
127
128                 // Display the domain settings fragment.
129                 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit()
130             }
131         }
132
133         // Return the domains list fragment.
134         return domainsListFragmentView
135     }
136 }