From 6181665938f9dbdbbb3c36af345a801875dbcb18 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Mon, 29 Apr 2019 19:39:29 +0400 Subject: [PATCH] Made reconnect to the network without need restart app --- app/build.gradle | 3 -- .../influence/contracts/CoreContracts.kt | 5 +- .../influence/presenters/MainPresenter.java | 24 --------- .../influence/presenters/MainPresenter.kt | 23 +++++++++ .../influence/views/MainActivity.java | 50 +++++++++++++++++++ .../res/drawable/ic_autorenew_white_24dp.xml | 22 ++++++++ app/src/main/res/menu/main_actionbar_menu.xml | 26 ++++++++++ app/src/main/res/values-ru-rRU/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 9 files changed, 129 insertions(+), 28 deletions(-) delete mode 100644 app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.java create mode 100644 app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.kt create mode 100644 app/src/main/res/drawable/ic_autorenew_white_24dp.xml create mode 100644 app/src/main/res/menu/main_actionbar_menu.xml diff --git a/app/build.gradle b/app/build.gradle index 772d671..9e4d154 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -61,7 +61,4 @@ dependencies { } repositories { mavenCentral() -} -configurations { - all*.exclude group: 'com.google.guava', module:'listenablefuture' } \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt b/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt index 3a2c099..4764bad 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt @@ -54,7 +54,10 @@ interface CoreContracts { fun onDestroy() } - interface IMainViewContract// + interface IMainViewContract { + fun showSnackbar(message: String) + fun showProgressBar(state: Boolean) + } // -----ChatActivity----- diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.java b/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.java deleted file mode 100644 index 82797d4..0000000 --- a/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.java +++ /dev/null @@ -1,24 +0,0 @@ -package io.github.chronosx88.influence.presenters; - -import io.github.chronosx88.influence.contracts.CoreContracts; -import io.github.chronosx88.influence.logic.MainLogic; - -public class MainPresenter implements CoreContracts.IMainPresenterContract { - private CoreContracts.IMainLogicContract logic; - private CoreContracts.IMainViewContract view; - - public MainPresenter(CoreContracts.IMainViewContract view) { - this.view = view; - logic = new MainLogic(); - } - - @Override - public void initPeer() { - logic.initPeer(); - } - - @Override - public void onDestroy() { - logic.shutdownPeer(); - } -} diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.kt b/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.kt new file mode 100644 index 0000000..73ab727 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.kt @@ -0,0 +1,23 @@ +package io.github.chronosx88.influence.presenters + +import io.github.chronosx88.influence.R +import io.github.chronosx88.influence.contracts.CoreContracts +import io.github.chronosx88.influence.helpers.AppHelper +import io.github.chronosx88.influence.logic.MainLogic + +class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreContracts.IMainPresenterContract { + private val logic: CoreContracts.IMainLogicContract = MainLogic() + + override fun initPeer() { + if (AppHelper.getPeerDHT() == null) { + logic.initPeer() + } else { + view.showSnackbar(AppHelper.getContext().getString(R.string.node_already_running)) + view.showProgressBar(false) + } + } + + override fun onDestroy() { + logic.shutdownPeer() + } +} diff --git a/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java b/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java index 006dfa9..90db9f3 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java @@ -2,16 +2,23 @@ package io.github.chronosx88.influence.views; import android.app.ProgressDialog; import android.os.Bundle; +import android.view.Menu; import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; import android.widget.Toast; import com.google.android.material.bottomnavigation.BottomNavigationView; +import com.google.android.material.snackbar.Snackbar; import com.google.gson.JsonObject; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; + +import org.jetbrains.annotations.NotNull; + import io.github.chronosx88.influence.R; import io.github.chronosx88.influence.contracts.CoreContracts; import io.github.chronosx88.influence.contracts.observer.IObserver; @@ -130,4 +137,47 @@ public class MainActivity extends AppCompatActivity implements IObserver, CoreCo } } } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.main_actionbar_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if(item.getItemId() == R.id.action_reconnect_network) { + progressDialog.show(); + presenter.initPeer(); + } + return true; + } + + @Override + public void showSnackbar(@NotNull String message) { + Snackbar.make(getRootView(), message, Snackbar.LENGTH_LONG) + .show(); + } + + @Override + public void showProgressBar(boolean state) { + if(state) { + progressDialog.show(); + } else { + progressDialog.dismiss(); + } + } + + private View getRootView() { + final ViewGroup contentViewGroup = findViewById(android.R.id.content); + View rootView = null; + + if(contentViewGroup != null) + rootView = contentViewGroup.getChildAt(0); + + if(rootView == null) + rootView = getWindow().getDecorView().getRootView(); + + return rootView; + } } diff --git a/app/src/main/res/drawable/ic_autorenew_white_24dp.xml b/app/src/main/res/drawable/ic_autorenew_white_24dp.xml new file mode 100644 index 0000000..a23db6d --- /dev/null +++ b/app/src/main/res/drawable/ic_autorenew_white_24dp.xml @@ -0,0 +1,22 @@ + + + + + diff --git a/app/src/main/res/menu/main_actionbar_menu.xml b/app/src/main/res/menu/main_actionbar_menu.xml new file mode 100644 index 0000000..eb81b5b --- /dev/null +++ b/app/src/main/res/menu/main_actionbar_menu.xml @@ -0,0 +1,26 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-ru-rRU/strings.xml b/app/src/main/res/values-ru-rRU/strings.xml index 05dbfe9..2e0fb2e 100644 --- a/app/src/main/res/values-ru-rRU/strings.xml +++ b/app/src/main/res/values-ru-rRU/strings.xml @@ -7,4 +7,6 @@ Отмена Ваше имя пользователя Имя пользователя + Переподключиться к сети + Узел уже запущен \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2594e0b..cae914e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,4 +6,6 @@ Cancel Your username Username + Reconnect to the network + Node already running