tui: improve mode selection
authorNiki Roo <niki@nikiroo.be>
Sun, 10 Mar 2019 17:36:24 +0000 (18:36 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 10 Mar 2019 17:36:24 +0000 (18:36 +0100)
src/be/nikiroo/fanfix/reader/tui/TuiReaderApplication.java
src/be/nikiroo/fanfix/reader/tui/TuiReaderMainWindow.java

index c08b6c2cc0c42f909c5bbd74cc318ac0b7eac606..52bc531ad803202904f9e78076d5da5df837b7c2 100644 (file)
@@ -22,6 +22,7 @@ import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.library.BasicLibrary;
 import be.nikiroo.fanfix.reader.BasicReader;
 import be.nikiroo.fanfix.reader.Reader;
+import be.nikiroo.fanfix.reader.tui.TuiReaderMainWindow.Mode;
 import be.nikiroo.utils.Progress;
 
 /**
@@ -45,7 +46,6 @@ class TuiReaderApplication extends TApplication implements Reader {
 
        private Reader reader;
        private TuiReaderMainWindow main;
-       private String source;
 
        // start reading if meta present
        public TuiReaderApplication(Reader reader, BackendType backend)
@@ -65,7 +65,7 @@ class TuiReaderApplication extends TApplication implements Reader {
                init(reader);
 
                showMain();
-               setSource(source);
+               main.setMode(Mode.SOURCE, source);
        }
 
        @Override
@@ -174,12 +174,6 @@ class TuiReaderApplication extends TApplication implements Reader {
                }
        }
 
-       private void setSource(String source) {
-               this.source = source;
-               showMain();
-               main.setSource(source);
-       }
-
        private void init(Reader reader) {
                this.reader = reader;
 
@@ -286,7 +280,6 @@ class TuiReaderApplication extends TApplication implements Reader {
                        return true;
                case MENU_LIBRARY:
                        showMain();
-                       setSource(source);
                        return true;
                }
 
index 126cee58071c5105c0bdda3959338d2cf114ac47..e4323f08fe21ce836fa20d30b1275a2d3aea187b 100644 (file)
@@ -35,16 +35,24 @@ class TuiReaderMainWindow extends TWindow {
        public static final TCommand CMD_SEARCH = new TCommand(MENU_SEARCH) {
        };
 
+       public enum Mode {
+               SOURCE, AUTHOR,
+       }
+
        private TList list;
        private List<MetaData> listKeys;
        private List<String> listItems;
        private Reader reader;
-       private String source;
+
+       private Mode mode = Mode.SOURCE;
+       private String target = null;
        private String filter = "";
+
        private List<TSizeConstraint> sizeConstraints = new ArrayList<TSizeConstraint>();
 
        // TODO: because no way to find out the current index!!
        private TComboBox select;
+       private TComboBox option;
 
        /**
         * Create a new {@link TuiReaderMainWindow} without any stories in the list.
@@ -132,15 +140,15 @@ class TuiReaderMainWindow extends TWindow {
        private void addSelect() {
                // TODO: make a full list
                final List<String> options = new ArrayList<String>();
-               options.add("(all opt)");
+               options.add("(show all)");
                options.add("Sources");
                options.add("Author");
 
                // TODO
-               final List<String> sources = new ArrayList<String>();
-               sources.add("(show all)");
+               final List<String> selects = new ArrayList<String>();
+               selects.add("(show all)");
                for (String source : reader.getLibrary().getSources()) {
-                       sources.add(source);
+                       selects.add(source);
                }
 
                TLabel lblSelect = addLabel("Select: ", 0, 0);
@@ -153,31 +161,60 @@ class TuiReaderMainWindow extends TWindow {
                // TODO: setWidth() does not impact the display width, only the control
                // and the down arrow on the right
                // TODO: width 1 +resize + click on down arrow = bad format exception
-               select = addComboBox(0, 0, 10, sources, 0,
-                               Math.min(sources.size() + 1, getHeight() - 1 - 1),
+               select = addComboBox(0, 0, 10, selects, 0,
+                               Math.min(selects.size() + 1, getHeight() - 1 - 1),
                                new TAction() {
                                        @Override
                                        public void DO() {
-                                               // TODO: wut?
+                                               // TODO: detect (show all)
                                                if (select.getText().equals("(show all)")) {
-                                                       setSource(null);
+                                                       setMode(mode, null);
                                                } else {
-                                                       setSource(select.getText());
+                                                       setMode(mode, select.getText());
                                                }
-                                               refreshStories();
                                        }
                                });
 
-               final TComboBox option = addComboBox(0, 0, 10, options, 0,
-                               Math.min(sources.size() + 1, getHeight() - 1 - 1),
+               option = addComboBox(0, 0, 10, options, 0,
+                               Math.min(selects.size() + 1, getHeight() - 1 - 1),
                                new TAction() {
                                        @Override
                                        public void DO() {
-                                               // TODO Not working:
-                                               sources.clear();
-                                               sources.add("(show all)");
-                                               for (String source : reader.getLibrary().getSources()) {
-                                                       sources.add(source);
+                                               // TODO clear not working!!
+
+                                               String smode = option.getText();
+                                               Mode mode;
+                                               if (smode == null || smode.equals("(show all)")) {
+                                                       mode = null;
+                                                       select.setVisible(false);
+                                                       select.setEnabled(false);
+                                               } else if (smode.equals("Sources")) {
+                                                       mode = Mode.SOURCE;
+                                                       select.setVisible(true);
+                                                       select.setEnabled(true);
+                                                       selects.clear();
+                                                       selects.add("(show all)");
+                                                       for (String source : reader.getLibrary()
+                                                                       .getSources()) {
+                                                               selects.add(source);
+                                                       }
+                                               } else {
+                                                       mode = Mode.AUTHOR;
+                                                       select.setVisible(true);
+                                                       select.setEnabled(true);
+                                                       selects.clear();
+                                                       selects.add("(show all)");
+                                                       for (String author : reader.getLibrary()
+                                                                       .getAuthors()) {
+                                                               selects.add(author);
+                                                       }
+                                               }
+
+                                               // TODO: detect (show all)
+                                               if (select.getText().equals("(show all)")) {
+                                                       setMode(mode, null);
+                                               } else {
+                                                       setMode(mode, select.getText());
                                                }
                                        }
                                });
@@ -205,18 +242,30 @@ class TuiReaderMainWindow extends TWindow {
         * Will take the current settings into account (filter, source...).
         */
        public void refreshStories() {
-               List<MetaData> metas = reader.getLibrary().getListBySource(source);
+               List<MetaData> metas;
+               if (mode == Mode.SOURCE) {
+                       metas = reader.getLibrary().getListBySource(target);
+               } else if (mode == Mode.AUTHOR) {
+                       metas = reader.getLibrary().getListByAuthor(target);
+               } else {
+                       metas = reader.getLibrary().getList();
+               }
+
                setMetas(metas);
        }
 
        /**
-        * Change the source filter and display all stories matching this source.
+        * Change the author/source filter and display all stories matching this
+        * target.
         * 
-        * @param source
-        *            the new source or NULL for all sources
+        * @param mode
+        *            the new mode or NULL for no sorting
+        * @param target
+        *            the actual target for the given mode, or NULL for all of them
         */
-       public void setSource(String source) {
-               this.source = source;
+       public void setMode(Mode mode, String target) {
+               this.mode = mode;
+               this.target = target;
                refreshStories();
        }