diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt new file mode 100644 index 0000000..f255f53 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt | |||
@@ -0,0 +1,84 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2018 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * This program is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation, either version 3 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | package org.pacien.tincapp.activities.start | ||
20 | |||
21 | import android.arch.lifecycle.Observer | ||
22 | import android.os.Bundle | ||
23 | import android.support.v4.app.Fragment | ||
24 | import android.view.LayoutInflater | ||
25 | import android.view.View | ||
26 | import android.view.ViewGroup | ||
27 | import android.widget.AdapterView | ||
28 | import android.widget.ArrayAdapter | ||
29 | import android.widget.TextView | ||
30 | import kotlinx.android.synthetic.main.start_network_list.* | ||
31 | import org.pacien.tincapp.R | ||
32 | import org.pacien.tincapp.context.AppPaths | ||
33 | import org.pacien.tincapp.extensions.hideBottomSeparator | ||
34 | import org.pacien.tincapp.extensions.setElements | ||
35 | |||
36 | /** | ||
37 | * @author pacien | ||
38 | */ | ||
39 | class NetworkListFragment : Fragment() { | ||
40 | private val appPaths = AppPaths | ||
41 | private val networkListViewModel by lazy { NetworkListViewModel() } | ||
42 | private val networkListAdapter by lazy { ArrayAdapter<String>(context, R.layout.start_network_list_item) } | ||
43 | var connectToNetworkAction = { _: String -> Unit } | ||
44 | |||
45 | override fun onCreate(savedInstanceState: Bundle?) { | ||
46 | super.onCreate(savedInstanceState) | ||
47 | networkListViewModel.networkList.observe(this, Observer { updateNetworkList(it.orEmpty()) }) | ||
48 | } | ||
49 | |||
50 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
51 | return inflater.inflate(R.layout.start_network_list, container, false) | ||
52 | } | ||
53 | |||
54 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
55 | val listHeaderView = layoutInflater.inflate(R.layout.start_network_list_header, start_network_list, false) | ||
56 | start_network_list.addHeaderView(listHeaderView, null, false) | ||
57 | start_network_list.hideBottomSeparator() | ||
58 | start_network_list.emptyView = start_network_list_placeholder | ||
59 | start_network_list.onItemClickListener = AdapterView.OnItemClickListener(this::onItemClick) | ||
60 | start_network_list.adapter = networkListAdapter | ||
61 | } | ||
62 | |||
63 | @Suppress("UNUSED_PARAMETER") | ||
64 | private fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = when (view) { | ||
65 | is TextView -> connectToNetworkAction(view.text.toString()) | ||
66 | else -> Unit | ||
67 | } | ||
68 | |||
69 | private fun updateNetworkList(networks: List<String>) { | ||
70 | networkListAdapter.setElements(networks) | ||
71 | if (networks.isEmpty()) updatePlaceholder() | ||
72 | } | ||
73 | |||
74 | private fun updatePlaceholder() { | ||
75 | val placeholderTextResource = when (appPaths.storageAvailable()) { | ||
76 | true -> R.string.start_network_list_empty_none_found | ||
77 | false -> R.string.start_network_list_empty_storage_not_available | ||
78 | } | ||
79 | |||
80 | start_network_list_placeholder.post { | ||
81 | start_network_list_placeholder_text.text = getString(placeholderTextResource) | ||
82 | } | ||
83 | } | ||
84 | } | ||