mvp for logger warning
This commit is contained in:
@@ -1,22 +1,25 @@
|
|||||||
package net.headlezz.notificationlogger;
|
package net.headlezz.notificationlogger;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.design.widget.FloatingActionButton;
|
import android.support.design.widget.FloatingActionButton;
|
||||||
|
import android.support.design.widget.Snackbar;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
|
import android.support.v7.preference.PreferenceManager;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import net.headlezz.notificationlogger.createnotification.NotificationCreationFragment;
|
import net.headlezz.notificationlogger.createnotification.NotificationCreationFragment;
|
||||||
|
import net.headlezz.notificationlogger.presenter.LoggerWarningPresenter;
|
||||||
|
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener, LoggerWarningPresenter.LoggerWarningView {
|
||||||
|
|
||||||
@Bind(R.id.toolbar)
|
@Bind(R.id.toolbar)
|
||||||
Toolbar mToolbar;
|
Toolbar mToolbar;
|
||||||
@@ -24,14 +27,21 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Bind(R.id.main_menu_fab_add_notification)
|
@Bind(R.id.main_menu_fab_add_notification)
|
||||||
FloatingActionButton mFAB;
|
FloatingActionButton mFAB;
|
||||||
|
|
||||||
|
SharedPreferences mSharedPrefs;
|
||||||
|
|
||||||
|
LoggerWarningPresenter mLoggerWarningPresenter;
|
||||||
|
Snackbar mLoggerWarningView;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.main_activity);
|
setContentView(R.layout.main_activity);
|
||||||
ButterKnife.bind(this);
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
setSupportActionBar(mToolbar);
|
setSupportActionBar(mToolbar);
|
||||||
|
|
||||||
|
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
|
mLoggerWarningPresenter = new LoggerWarningPresenter(this);
|
||||||
|
|
||||||
mFAB.setOnClickListener(new View.OnClickListener() {
|
mFAB.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
@@ -39,7 +49,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if(getSupportFragmentManager().findFragmentById(R.id.main_menu_fragment_container) == null) {
|
if (getSupportFragmentManager().findFragmentById(R.id.main_menu_fragment_container) == null) {
|
||||||
getSupportFragmentManager().beginTransaction()
|
getSupportFragmentManager().beginTransaction()
|
||||||
.add(R.id.main_menu_fragment_container, new NotificationListFragment())
|
.add(R.id.main_menu_fragment_container, new NotificationListFragment())
|
||||||
.commit();
|
.commit();
|
||||||
@@ -53,6 +63,19 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
mFAB.setVisibility(View.GONE);
|
mFAB.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
mSharedPrefs.registerOnSharedPreferenceChangeListener(this);
|
||||||
|
mLoggerWarningPresenter.setCurrentLoggerPreference(mSharedPrefs.getBoolean("pref_logging_enabled", true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
mSharedPrefs.unregisterOnSharedPreferenceChangeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
getMenuInflater().inflate(R.menu.main_activity, menu);
|
getMenuInflater().inflate(R.menu.main_activity, menu);
|
||||||
@@ -61,11 +84,11 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
if(item.getItemId() == R.id.menu_settings) {
|
if (item.getItemId() == R.id.menu_settings) {
|
||||||
Intent intent = new Intent(this, PreferenceActivity.class);
|
Intent intent = new Intent(this, PreferenceActivity.class);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
return true;
|
return true;
|
||||||
} else if(item.getItemId() == android.R.id.home) {
|
} else if (item.getItemId() == android.R.id.home) {
|
||||||
onBackPressed();
|
onBackPressed();
|
||||||
}
|
}
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
@@ -87,11 +110,48 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onBackPressed() {
|
||||||
// make fab visible and change title if we come back to first fragment
|
// make fab visible and change title if we come back to first fragment
|
||||||
if(getSupportFragmentManager().getBackStackEntryCount() == 1) {
|
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
|
||||||
mFAB.setVisibility(View.VISIBLE);
|
mFAB.setVisibility(View.VISIBLE);
|
||||||
getSupportActionBar().setTitle(getString(R.string.app_name));
|
getSupportActionBar().setTitle(getString(R.string.app_name));
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
|
||||||
}
|
}
|
||||||
super.onBackPressed();
|
super.onBackPressed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
|
if (key.equals("pref_logging_enabled"))
|
||||||
|
mLoggerWarningPresenter.setCurrentLoggerPreference(sharedPreferences.getBoolean("pref_logging_enabled", true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showLoggerWarning() {
|
||||||
|
if (mLoggerWarningView == null) {
|
||||||
|
mLoggerWarningView = Snackbar.make(findViewById(R.id.viewholder), R.string.main_snack_logging_disabled_warning, Snackbar.LENGTH_INDEFINITE);
|
||||||
|
mLoggerWarningView.setAction(R.string.main_snack_logging_disabled_warning_action, new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
mSharedPrefs.edit().putBoolean("pref_logging_enabled", true).apply();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// fix for FAB not moving down after dismiss
|
||||||
|
mLoggerWarningView.getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onViewAttachedToWindow(View v) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewDetachedFromWindow(View v) {
|
||||||
|
mFAB.setTranslationY(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
mLoggerWarningView.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void hideLoggerWarning() {
|
||||||
|
if (mLoggerWarningView != null && mLoggerWarningView.isShownOrQueued())
|
||||||
|
mLoggerWarningView.dismiss();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package net.headlezz.notificationlogger.presenter;
|
||||||
|
|
||||||
|
public class LoggerWarningPresenter {
|
||||||
|
|
||||||
|
public interface LoggerWarningView {
|
||||||
|
void showLoggerWarning();
|
||||||
|
void hideLoggerWarning();
|
||||||
|
}
|
||||||
|
|
||||||
|
LoggerWarningView mView;
|
||||||
|
|
||||||
|
public LoggerWarningPresenter(LoggerWarningView lwv) {
|
||||||
|
mView = lwv;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentLoggerPreference(boolean loggingEnabled) {
|
||||||
|
if(loggingEnabled)
|
||||||
|
mView.hideLoggerWarning();
|
||||||
|
else
|
||||||
|
mView.showLoggerWarning();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/viewholder"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
<string name="menu_item_settings">Settings</string>
|
<string name="menu_item_settings">Settings</string>
|
||||||
<string name="title_settings">Settings</string>
|
<string name="title_settings">Settings</string>
|
||||||
|
|
||||||
|
<string name="main_snack_logging_disabled_warning">Logging is disabled.</string>
|
||||||
|
<string name="main_snack_logging_disabled_warning_action">Enable</string>
|
||||||
|
|
||||||
<string name="pref_category_notification_settings">Notification Settings</string>
|
<string name="pref_category_notification_settings">Notification Settings</string>
|
||||||
<string name="pref_category_support">Support</string>
|
<string name="pref_category_support">Support</string>
|
||||||
<string name="pref_logging_enabled_title">Notification Logging</string>
|
<string name="pref_logging_enabled_title">Notification Logging</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user