Back to exit distracting free reading

pull/50/head
M66B 6 years ago
parent be5b608967
commit ee7b41f7b4

@ -25,6 +25,9 @@ import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
abstract class ActivityBase extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener { abstract class ActivityBase extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -69,4 +72,22 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
if ("theme".equals(key) || "debug".equals(key)) if ("theme".equals(key) || "debug".equals(key))
recreate(); recreate();
} }
private List<IBackPressedListener> backPressedListeners = new ArrayList<>();
public void addBackPressedListener(IBackPressedListener listener) {
backPressedListeners.add(listener);
}
@Override
public void onBackPressed() {
for (IBackPressedListener listener : backPressedListeners)
if (listener.onBackPressed())
return;
super.onBackPressed();
}
public interface IBackPressedListener {
boolean onBackPressed();
}
} }

@ -200,31 +200,39 @@ public class FragmentMessage extends FragmentEx {
}); });
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
private boolean addresses;
@Override @Override
public void onClick(View view) { public void onClick(View view) {
free = (top_navigation.getVisibility() != View.GONE); tvCount.setTag(tvCount.getVisibility());
tvError.setTag(tvError.getVisibility());
tvCc.setTag(grpAddresses.getVisibility());
rvAttachment.setTag(grpAddresses.getVisibility());
free = true;
getActivity().invalidateOptionsMenu(); getActivity().invalidateOptionsMenu();
grpHeader.setVisibility(free ? View.GONE : View.VISIBLE); grpHeader.setVisibility(free ? View.GONE : View.VISIBLE);
if (free) { grpAddresses.setVisibility(View.GONE);
tvCount.setTag(tvCount.getVisibility()); grpAttachments.setVisibility(View.GONE);
tvError.setTag(tvError.getVisibility()); tvCount.setVisibility(View.GONE);
fab.setImageResource(R.drawable.baseline_fullscreen_exit_24); tvError.setVisibility(View.GONE);
addresses = (grpAddresses.getVisibility() != View.GONE); fab.setVisibility(View.GONE);
grpAddresses.setVisibility(View.GONE); }
grpAttachments.setVisibility(View.GONE); });
tvCount.setVisibility(View.GONE);
tvError.setVisibility(View.GONE); ((ActivityBase) getActivity()).addBackPressedListener(new ActivityBase.IBackPressedListener() {
} else { @Override
fab.setImageResource(R.drawable.baseline_fullscreen_24); public boolean onBackPressed() {
if (addresses) if (free && isVisible()) {
grpAddresses.setVisibility(View.VISIBLE); free = false;
if (rvAttachment.getAdapter().getItemCount() > 0) getActivity().invalidateOptionsMenu();
grpAttachments.setVisibility(View.VISIBLE); grpHeader.setVisibility(free ? View.GONE : View.VISIBLE);
tvCount.setVisibility((int) tvCount.getVisibility()); grpAddresses.setVisibility((int) tvCc.getTag());
tvError.setVisibility((int) tvError.getVisibility()); rvAttachment.setVisibility((int) rvAttachment.getTag());
tvCount.setVisibility((int) tvCount.getTag());
tvError.setVisibility((int) tvError.getTag());
fab.setVisibility(View.VISIBLE);
return true;
} }
return false;
} }
}); });
@ -259,6 +267,7 @@ public class FragmentMessage extends FragmentEx {
grpMessage.setVisibility(View.GONE); grpMessage.setVisibility(View.GONE);
tvCount.setVisibility(View.GONE); tvCount.setVisibility(View.GONE);
tvError.setVisibility(View.GONE); tvError.setVisibility(View.GONE);
fab.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE); pbWait.setVisibility(View.VISIBLE);
rvAttachment.setHasFixedSize(false); rvAttachment.setHasFixedSize(false);
@ -272,7 +281,15 @@ public class FragmentMessage extends FragmentEx {
} }
@Override @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) { public void onSaveInstanceState(Bundle outState) {
outState.putBoolean("free", free);
outState.putInt("addresses", grpAddresses.getVisibility());
outState.putInt("attachments", rvAttachment.getVisibility());
super.onSaveInstanceState(outState);
}
@Override
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
// Get arguments // Get arguments
@ -376,11 +393,20 @@ public class FragmentMessage extends FragmentEx {
}); });
} }
if (savedInstanceState != null) {
free = savedInstanceState.getBoolean("free");
grpAddresses.setTag(savedInstanceState.getInt("addresses"));
rvAttachment.setTag(savedInstanceState.getInt("attachments"));
}
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
grpHeader.setVisibility(View.VISIBLE); grpHeader.setVisibility(free ? View.GONE : View.VISIBLE);
if (free)
grpAddresses.setVisibility(View.GONE);
grpMessage.setVisibility(View.VISIBLE); grpMessage.setVisibility(View.VISIBLE);
tvCount.setVisibility(message.count > 1 ? View.VISIBLE : View.GONE); tvCount.setVisibility(!free && message.count > 1 ? View.VISIBLE : View.GONE);
tvError.setVisibility(message.error == null ? View.GONE : View.VISIBLE); tvError.setVisibility(free || message.error == null ? View.GONE : View.VISIBLE);
fab.setVisibility(free ? View.GONE : View.VISIBLE);
} }
}); });

@ -292,5 +292,5 @@
android:id="@+id/grpMessage" android:id="@+id/grpMessage"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:constraint_referenced_ids="scroll,fab,bottom_navigation" /> app:constraint_referenced_ids="scroll,bottom_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

Loading…
Cancel
Save