aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt63
1 files changed, 63 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt
new file mode 100644
index 0000000..9c94bf1
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt
@@ -0,0 +1,63 @@
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
19package org.pacien.tincapp.activities.configure.tools
20
21import android.support.annotation.LayoutRes
22import android.support.annotation.StringRes
23import android.support.v7.app.AlertDialog
24import android.view.View
25import java8.util.concurrent.CompletableFuture
26import org.pacien.tincapp.R
27import org.pacien.tincapp.activities.BaseDialogFragment
28import org.pacien.tincapp.activities.common.ProgressModal
29import org.pacien.tincapp.extensions.Java.exceptionallyAccept
30import java.util.regex.Pattern
31
32/**
33 * @author pacien
34 */
35abstract class ConfigurationToolDialogFragment : BaseDialogFragment() {
36 private val networkNamePattern by lazy { Pattern.compile("^[^\\x00/]*$")!! }
37
38 protected fun makeDialog(@LayoutRes layout: Int, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) =
39 makeDialog(inflate(layout), title, applyButton, applyAction)
40
41 protected fun makeDialog(view: View, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) =
42 AlertDialog.Builder(parentActivity)
43 .setTitle(title)
44 .setView(view)
45 .setPositiveButton(applyButton) { _, _ -> applyAction(view) }
46 .setNegativeButton(R.string.generic_action_cancel) { _, _ -> Unit }
47 .create()!!
48
49 protected fun execAction(@StringRes label: Int, action: CompletableFuture<Unit>) {
50 ProgressModal.show(parentActivity, getString(label)).let { progressDialog ->
51 action
52 .whenComplete { _, _ -> progressDialog.dismiss() }
53 .thenAccept { parentActivity.notify(R.string.configure_tools_message_network_configuration_written) }
54 .exceptionallyAccept { parentActivity.runOnUiThread { parentActivity.showErrorDialog(it.cause!!.localizedMessage) } }
55 }
56 }
57
58 protected fun validateNetName(netName: String): CompletableFuture<Unit> =
59 if (networkNamePattern.matcher(netName).matches())
60 CompletableFuture.completedFuture(Unit)
61 else
62 CompletableFuture.failedFuture(IllegalArgumentException(getString(R.string.configure_tools_message_invalid_network_name)))
63}