Skip to content

Commit 7bc86cd

Browse files
committed
added save all to editor
1 parent 18451ba commit 7bc86cd

6 files changed

Lines changed: 87 additions & 1 deletion

File tree

main/src/main/java/org/xedox/webaide/EditorActivity.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.os.Bundle;
55
import android.os.Handler;
66
import android.os.Looper;
7+
import android.view.Menu;
8+
import android.view.MenuItem;
79
import android.view.View;
810
import android.widget.FrameLayout;
911
import android.widget.TextView;
@@ -15,6 +17,7 @@
1517
import com.google.android.material.tabs.TabLayout;
1618
import androidx.viewpager2.widget.ViewPager2;
1719
import com.google.android.material.appbar.MaterialToolbar;
20+
import java.util.Map;
1821
import org.xedox.utils.BaseActivity;
1922
import org.xedox.utils.dialog.ErrorDialog;
2023
import org.xedox.webaide.editor.EditorManager;
@@ -37,7 +40,7 @@ public class EditorActivity extends BaseActivity {
3740
private View nav;
3841
private TextView navTitle;
3942
private Handler handler = new Handler(Looper.getMainLooper());
40-
43+
4144
@Override
4245
protected void onCreate(Bundle savedInstanceState) {
4346
super.onCreate(savedInstanceState);
@@ -69,6 +72,17 @@ public void handleBackPressed() {
6972
startActivity(intent);
7073
finish();
7174
}
75+
76+
@Override
77+
public boolean onOptionsItemSelected(MenuItem item) {
78+
return editorManager.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
79+
}
80+
81+
82+
@Override
83+
public boolean onCreateOptionsMenu(Menu menu) {
84+
return onCreateOptionsMenu(R.menu.editor, menu);
85+
}
7286

7387
public MaterialToolbar getToolbar() {
7488
return this.toolbar;

main/src/main/java/org/xedox/webaide/editor/EditorManager.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.xedox.webaide.editor;
22

3+
import android.view.MenuItem;
34
import android.view.View;
45
import android.widget.ImageButton;
56
import androidx.viewpager2.widget.ViewPager2;
@@ -38,6 +39,7 @@ public EditorManager(EditorActivity context) {
3839
undo.setVisibility(hasItems ? View.VISIBLE : View.GONE);
3940
redo.setVisibility(hasItems ? View.VISIBLE : View.GONE);
4041
emptyPager.setVisibility(hasItems ? View.GONE : View.VISIBLE);
42+
context.updateItemVisibility(R.id.save_all, hasItems);
4143
});
4244
viewPager.setAdapter(editorStateAdapter);
4345
tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager, this::handleMediator);
@@ -53,6 +55,7 @@ public void onTabSelected(TabLayout.Tab tab) {
5355
boolean isFile = f instanceof FileFragment;
5456
undo.setEnabled(isFile);
5557
redo.setEnabled(isFile);
58+
context.updateItemEnabled(R.id.save_all, isFile);
5659
}
5760

5861
@Override
@@ -102,6 +105,15 @@ private void handleMediator(TabLayout.Tab tab, int position) {
102105
});
103106
});
104107
}
108+
109+
public boolean onOptionsItemSelected(MenuItem item) {
110+
int id = item.getItemId();
111+
if(id == R.id.save_all) {
112+
editorStateAdapter.saveAll();
113+
return true;
114+
}
115+
return false;
116+
}
105117

106118
public void openFile(Node file) {
107119
try {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- drawable/content_save.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"><path android:fillColor="#000000" android:pathData="M15,9H5V5H15M12,19A3,3 0 0,1 9,16A3,3 0 0,1 12,13A3,3 0 0,1 15,16A3,3 0 0,1 12,19M17,3H5C3.89,3 3,3.9 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V7L17,3Z" /></vector>

main/src/main/res/menu/editor.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<item
5+
android:id="@+id/save_all"
6+
android:title="@+id/save_all" />
7+
8+
</menu>

main/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<string name="settings">Settings</string>
3535
<string name="exit">Exit</string>
3636
<string name="close_it">Close this</string>
37+
<string name="save_all">Save all</string>
3738
<string name="close_other">Close others</string>
3839
<string name="close_all">Close all</string>
3940
<string name="you_no_have_open_files">You have no open files</string>

utils/src/main/java/org/xedox/utils/BaseActivity.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.xedox.utils;
22

3+
import android.util.Log;
4+
import android.view.Menu;
5+
import android.view.MenuItem;
36
import android.view.View;
47
import androidx.activity.OnBackPressedCallback;
58
import androidx.appcompat.app.AppCompatActivity;
@@ -8,8 +11,13 @@
811
import androidx.core.view.WindowCompat;
912
import androidx.core.view.WindowInsetsCompat;
1013
import com.google.android.material.snackbar.Snackbar;
14+
import java.lang.reflect.Method;
15+
import java.util.HashMap;
16+
import java.util.Map;
1117

1218
public class BaseActivity extends AppCompatActivity {
19+
protected final Map<Integer, Boolean> menuItemsVisibility = new HashMap<>();
20+
protected final Map<Integer, Boolean> menuItemsEnabled = new HashMap<>();
1321

1422
@Override
1523
protected void onStart() {
@@ -28,6 +36,16 @@ public void handleOnBackPressed() {
2836
applyWindowInsets(findViewById(android.R.id.content));
2937
}
3038

39+
public void updateItemVisibility(int itemId, boolean visible) {
40+
menuItemsVisibility.put(itemId, visible);
41+
invalidateOptionsMenu();
42+
}
43+
44+
public void updateItemEnabled(int itemId, boolean enabled) {
45+
menuItemsEnabled.put(itemId, enabled);
46+
invalidateOptionsMenu();
47+
}
48+
3149
public void applyWindowInsets(View view) {
3250
ViewCompat.setOnApplyWindowInsetsListener(
3351
view,
@@ -45,6 +63,38 @@ public void applyWindowInsets(View view) {
4563
});
4664
}
4765

66+
public boolean onCreateOptionsMenu(int menuId, Menu menu) {
67+
getMenuInflater().inflate(menuId, menu);
68+
for (Map.Entry<Integer, Boolean> entry : menuItemsVisibility.entrySet()) {
69+
MenuItem item = menu.findItem(entry.getKey());
70+
if (item != null) {
71+
item.setVisible(entry.getValue());
72+
}
73+
}
74+
for (Map.Entry<Integer, Boolean> entry : menuItemsEnabled.entrySet()) {
75+
MenuItem item = menu.findItem(entry.getKey());
76+
if (item != null) {
77+
item.setEnabled(entry.getValue());
78+
}
79+
}
80+
return true;
81+
}
82+
83+
@Override
84+
public boolean onPrepareOptionsMenu(Menu menu) {
85+
if (menu != null && menu.getClass().getSimpleName().equals("MenuBuilder")) {
86+
try {
87+
Method m =
88+
menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
89+
m.setAccessible(true);
90+
m.invoke(menu, true);
91+
} catch (Exception e) {
92+
Log.e("OverflowMenu", "Error forcing menu icons to show", e);
93+
}
94+
}
95+
return super.onPrepareOptionsMenu(menu);
96+
}
97+
4898
public void showSnackbar(int text, int type) {
4999
Snackbar.make(findViewById(android.R.id.content), text, type)
50100
.setAction(R.string.ok, null)

0 commit comments

Comments
 (0)