Parcourir la source

pagination

tags/0.0.5
Robin Thoni il y a 9 ans
Parent
révision
95533c1a86

+ 0
- 13
app/src/androidTest/java/com/rthoni/camotion/ApplicationTest.java Voir le fichier

@@ -1,13 +0,0 @@
1
-package com.rthoni.camotion;
2
-
3
-import android.app.Application;
4
-import android.test.ApplicationTestCase;
5
-
6
-/**
7
- * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8
- */
9
-public class ApplicationTest extends ApplicationTestCase<Application> {
10
-    public ApplicationTest() {
11
-        super(Application.class);
12
-    }
13
-}

+ 1
- 1
app/src/main/java/com/rthoni/camotion/ui/fragments/CamotionFragment.java Voir le fichier

@@ -2,7 +2,7 @@ package com.rthoni.camotion.ui.fragments;
2 2
 
3 3
 import com.luticate.auth.dbo.LuFullLoginDbo;
4 4
 import com.luticate.utils.dbo.LuDbo;
5
-import com.luticate.utils.ui.ViewPaginationFragment;
5
+import com.luticate.utils.ui.fragments.ViewPaginationFragment;
6 6
 import com.rthoni.camotion.dbo.LocationDbo;
7 7
 
8 8
 /**

+ 0
- 13
luticateauth/src/androidTest/java/com/luticate/auth/ApplicationTest.java Voir le fichier

@@ -1,13 +0,0 @@
1
-package com.luticate.auth;
2
-
3
-import android.app.Application;
4
-import android.test.ApplicationTestCase;
5
-
6
-/**
7
- * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8
- */
9
-public class ApplicationTest extends ApplicationTestCase<Application> {
10
-    public ApplicationTest() {
11
-        super(Application.class);
12
-    }
13
-}

+ 0
- 13
luticateutils/src/androidTest/java/com/rthoni/utils/ApplicationTest.java Voir le fichier

@@ -1,13 +0,0 @@
1
-package com.luticate.utils;
2
-
3
-import android.app.Application;
4
-import android.test.ApplicationTestCase;
5
-
6
-/**
7
- * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8
- */
9
-public class ApplicationTest extends ApplicationTestCase<Application> {
10
-    public ApplicationTest() {
11
-        super(Application.class);
12
-    }
13
-}

luticateutils/src/main/java/com/luticate/utils/ui/PaginationFragment.java → luticateutils/src/main/java/com/luticate/utils/ui/fragments/AbstractPaginationFragment.java Voir le fichier

@@ -1,4 +1,4 @@
1
-package com.luticate.utils.ui;
1
+package com.luticate.utils.ui.fragments;
2 2
 
3 3
 import android.animation.Animator;
4 4
 import android.animation.AnimatorListenerAdapter;
@@ -8,21 +8,18 @@ import android.os.Bundle;
8 8
 import android.view.LayoutInflater;
9 9
 import android.view.View;
10 10
 import android.view.ViewGroup;
11
-import android.widget.LinearLayout;
12
-import android.widget.TextView;
13 11
 
14
-import com.luticate.utils.R;
15 12
 import com.luticate.utils.business.LuPromise;
16 13
 import com.luticate.utils.dbo.LuDbo;
17 14
 import com.luticate.utils.dbo.LuMultipleDbo;
15
+import com.luticate.utils.ui.views.AbstractPaginationView;
18 16
 
19
-import java.util.List;
20 17
 import java.util.Vector;
21 18
 
22 19
 /**
23 20
  * Created by robin on 12/6/15.
24 21
  */
25
-public abstract class PaginationFragment<Dbo extends LuDbo> extends Fragment {
22
+public abstract class AbstractPaginationFragment<Dbo extends LuDbo> extends Fragment {
26 23
 
27 24
     protected int _page = 0;
28 25
 
@@ -43,6 +40,14 @@ public abstract class PaginationFragment<Dbo extends LuDbo> extends Fragment {
43 40
     @Override
44 41
     public void onStart() {
45 42
         super.onStart();
43
+        View v = getView();
44
+        AbstractPaginationView paginationView = (AbstractPaginationView) v.findViewById(getPaginationViewId());
45
+        paginationView.setOnPageChanged(new LuPromise.LuConsumer<Integer>() {
46
+            @Override
47
+            public void execute(Integer page) {
48
+                loadPage(page);
49
+            }
50
+        });
46 51
         loadPage(_page);
47 52
     }
48 53
 
@@ -91,6 +96,9 @@ public abstract class PaginationFragment<Dbo extends LuDbo> extends Fragment {
91 96
 
92 97
     public void loadPage(final int page)
93 98
     {
99
+        final View v = getView();
100
+        if (!isAdded() || v == null)
101
+            return;
94 102
         showProgress(true);
95 103
         _items.setData(new Vector<Dbo>());
96 104
         getLoadPagePromise(page, _perPage, _query).then(new LuPromise.LuConsumer<LuMultipleDbo<Dbo>>() {
@@ -99,7 +107,9 @@ public abstract class PaginationFragment<Dbo extends LuDbo> extends Fragment {
99 107
                 _items = items;
100 108
                 _page = page;
101 109
                 _maxPage = Math.max(items.getCount() / _perPage, 1);
102
-                updatePages();
110
+                AbstractPaginationView paginationView = (AbstractPaginationView) v.findViewById(getPaginationViewId());
111
+                paginationView.setMaxPage(_maxPage);
112
+                paginationView.setPage(_page);
103 113
                 onPageChanged();
104 114
                 showProgress(false);
105 115
             }
@@ -112,44 +122,7 @@ public abstract class PaginationFragment<Dbo extends LuDbo> extends Fragment {
112 122
         });
113 123
     }
114 124
 
115
-    protected List<Integer> getPages()
116
-    {
117
-        List<Integer> pages = new Vector<>();
118
-        int start = Math.max(0, _page - 2);
119
-        int end = Math.min(start + 5, _maxPage);
120
-        for (int i = start; i < end; ++i)
121
-        {
122
-            pages.add(i);
123
-        }
124
-        return pages;
125
-    }
126
-
127
-    protected void updatePages()
128
-    {
129
-        View v = getView();
130
-        ViewGroup layout = (ViewGroup) v.findViewById(R.id.pagesLayout);
131
-        if (layout != null)
132
-        {
133
-            layout.removeAllViews();
134
-            for (final int page : getPages())
135
-            {
136
-                TextView tv = new TextView(getActivity());
137
-                tv.setText(" " + String.valueOf(page + 1) + " ");
138
-                tv.setOnClickListener(new View.OnClickListener() {
139
-                    @Override
140
-                    public void onClick(View v) {
141
-                        loadPage(page);
142
-                    }
143
-                });
144
-                tv.setTextAppearance(getActivity(), android.R.style.TextAppearance_DeviceDefault_Large);
145
-                layout.addView(tv);
146
-            }
147
-        }
148
-    }
149
-
150
-    protected void onPageChanged()
151
-    {
152
-    }
125
+    protected abstract void onPageChanged();
153 126
 
154 127
     protected abstract LuPromise<LuMultipleDbo<Dbo>> getLoadPagePromise(int page, int perPage, String query);
155 128
 
@@ -158,4 +131,6 @@ public abstract class PaginationFragment<Dbo extends LuDbo> extends Fragment {
158 131
     protected abstract int getLoaderViewId();
159 132
 
160 133
     protected abstract int getLayoutId();
134
+
135
+    protected abstract int getPaginationViewId();
161 136
 }

luticateutils/src/main/java/com/luticate/utils/ui/ViewPaginationFragment.java → luticateutils/src/main/java/com/luticate/utils/ui/fragments/ViewPaginationFragment.java Voir le fichier

@@ -1,4 +1,4 @@
1
-package com.luticate.utils.ui;
1
+package com.luticate.utils.ui.fragments;
2 2
 
3 3
 import android.view.View;
4 4
 import android.widget.LinearLayout;
@@ -9,7 +9,7 @@ import com.luticate.utils.dbo.LuDbo;
9 9
 /**
10 10
  * Created by robin on 12/6/15.
11 11
  */
12
-public abstract class ViewPaginationFragment<Dbo extends LuDbo> extends PaginationFragment<Dbo> {
12
+public abstract class ViewPaginationFragment<Dbo extends LuDbo> extends AbstractPaginationFragment<Dbo> {
13 13
 
14 14
     @Override
15 15
     protected int getLoaderViewId() {
@@ -26,10 +26,15 @@ public abstract class ViewPaginationFragment<Dbo extends LuDbo> extends Paginati
26 26
         return R.layout.view_pagination_fragment_layout;
27 27
     }
28 28
 
29
+    @Override
30
+    protected int getPaginationViewId() {
31
+        return R.id.paginationView;
32
+    }
33
+
29 34
     @Override
30 35
     protected void onPageChanged() {
31
-        super.onPageChanged();
32
-        LinearLayout layout = (LinearLayout) getView().findViewById(R.id.itemsLayoutScrollView);
36
+        View view = getView();
37
+        LinearLayout layout = (LinearLayout) view.findViewById(R.id.itemsLayoutScrollView);
33 38
         layout.removeAllViews();
34 39
         for (Dbo item : _items.getData())
35 40
         {

+ 133
- 0
luticateutils/src/main/java/com/luticate/utils/ui/views/AbstractPaginationView.java Voir le fichier

@@ -0,0 +1,133 @@
1
+package com.luticate.utils.ui.views;
2
+
3
+import android.content.Context;
4
+import android.util.AttributeSet;
5
+import android.widget.LinearLayout;
6
+
7
+import com.luticate.utils.business.LuPromise;
8
+
9
+import java.util.List;
10
+import java.util.Vector;
11
+
12
+/**
13
+ * Created by robin on 12/8/15.
14
+ */
15
+public abstract class AbstractPaginationView extends LinearLayout {
16
+
17
+    private LuPromise.LuConsumer<Integer> _onPageChanged = null;
18
+
19
+    private int _page = 0;
20
+
21
+    private int _maxPage = 1;
22
+
23
+    private int _contextPagesCount = 2;
24
+
25
+    public AbstractPaginationView(Context context) {
26
+        super(context);
27
+        init();
28
+    }
29
+
30
+    public AbstractPaginationView(Context context, AttributeSet attrs) {
31
+        super(context, attrs);
32
+        init(attrs);
33
+    }
34
+
35
+    public AbstractPaginationView(Context context, AttributeSet attrs, int defStyleAttr) {
36
+        super(context, attrs, defStyleAttr);
37
+        init(attrs);
38
+    }
39
+
40
+    private void init(AttributeSet attrs)
41
+    {
42
+        init();
43
+        //_contextPagesCount = attrs.getAttributeIntValue(R.styleable.DefaultPaginationView_contextPagesCount, _contextPagesCount);
44
+    }
45
+
46
+    private void init()
47
+    {
48
+
49
+    }
50
+
51
+    @Override
52
+    protected void onAttachedToWindow() {
53
+        super.onAttachedToWindow();
54
+
55
+        if (isInEditMode())
56
+        {
57
+            onUpdatePages();
58
+        }
59
+    }
60
+
61
+    protected List<Integer> getPages()
62
+    {
63
+        List<Integer> pages = new Vector<>();
64
+        int start = Math.max(0, getPage() - getContextPagesCount());
65
+        int end = Math.min(start + getContextPagesCount() + 1, getMaxPage());
66
+        for (int i = start; i < end; ++i)
67
+        {
68
+            pages.add(i);
69
+        }
70
+        return pages;
71
+    }
72
+
73
+    protected abstract void onUpdatePages();
74
+
75
+    public int getMaxPage() {
76
+        return isInEditMode() ? 50 : _maxPage;
77
+    }
78
+
79
+    public void setMaxPage(int maxPage) {
80
+        _maxPage = maxPage;
81
+        onUpdatePages();
82
+    }
83
+
84
+    public int getContextPagesCount() {
85
+        return _contextPagesCount;
86
+    }
87
+
88
+    public void setContextPagesCount(int contextPagesCount) {
89
+        _contextPagesCount = contextPagesCount;
90
+        onUpdatePages();
91
+    }
92
+
93
+    public void setPage(int page)
94
+    {
95
+        _page = page;
96
+        onUpdatePages();
97
+    }
98
+
99
+    public int getPage()
100
+    {
101
+        return isInEditMode() ? 24 : _page;
102
+    }
103
+
104
+    public void setOnPageChanged(LuPromise.LuConsumer<Integer> onPageChanged) {
105
+        _onPageChanged = onPageChanged;
106
+    }
107
+
108
+    public void loadPage(int page)
109
+    {
110
+        if (_onPageChanged != null)
111
+        {
112
+            _onPageChanged.execute(page);
113
+        }
114
+    }
115
+
116
+    public void loadNextPage()
117
+    {
118
+        int page = getPage();
119
+        if (page < getMaxPage() - 1)
120
+        {
121
+            loadPage(page + 1);
122
+        }
123
+    }
124
+
125
+    public void loadPreviousPage()
126
+    {
127
+        int page = getPage();
128
+        if (page > 0)
129
+        {
130
+            loadPage(page - 1);
131
+        }
132
+    }
133
+}

+ 53
- 0
luticateutils/src/main/java/com/luticate/utils/ui/views/DefaultPaginationView.java Voir le fichier

@@ -0,0 +1,53 @@
1
+package com.luticate.utils.ui.views;
2
+
3
+import android.content.Context;
4
+import android.util.AttributeSet;
5
+import android.view.View;
6
+import android.view.ViewGroup;
7
+import android.widget.TextView;
8
+
9
+import com.luticate.utils.R;
10
+
11
+/**
12
+ * Created by robin on 12/8/15.
13
+ */
14
+public class DefaultPaginationView extends AbstractPaginationView {
15
+    public DefaultPaginationView(Context context) {
16
+        super(context);
17
+        init();
18
+    }
19
+
20
+    public DefaultPaginationView(Context context, AttributeSet attrs) {
21
+        super(context, attrs);
22
+        init();
23
+    }
24
+
25
+    public DefaultPaginationView(Context context, AttributeSet attrs, int defStyleAttr) {
26
+        super(context, attrs, defStyleAttr);
27
+        init();
28
+    }
29
+
30
+    public void init()
31
+    {
32
+        inflate(getContext(), R.layout.default_pagination_view_layout, this);
33
+    }
34
+
35
+    @Override
36
+    protected void onUpdatePages() {
37
+        ViewGroup layout = (ViewGroup) findViewById(R.id.pagesLayout);
38
+        layout.removeAllViews();
39
+        for (final int page : getPages())
40
+        {
41
+            TextView tv = new TextView(getContext());
42
+            tv.setText(" " + String.valueOf(page + 1) + " ");
43
+            tv.setOnClickListener(new View.OnClickListener() {
44
+                    @Override
45
+                    public void onClick(View v) {
46
+                            loadPage(page);
47
+                        }
48
+                });
49
+            tv.setTextAppearance(getContext(), android.R.style.TextAppearance_DeviceDefault_Large);
50
+            layout.addView(tv);
51
+        }
52
+    }
53
+}

+ 30
- 0
luticateutils/src/main/res/layout/default_pagination_view_layout.xml Voir le fichier

@@ -0,0 +1,30 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+              android:layout_width="match_parent"
4
+              android:layout_height="wrap_content"
5
+              android:gravity="center">
6
+
7
+    <TextView
8
+        android:layout_width="wrap_content"
9
+        android:layout_height="wrap_content"
10
+        android:id="@+id/previousPageView"
11
+        android:textAppearance="?android:attr/textAppearanceLarge"
12
+        android:layout_margin="2dp"
13
+        android:text=" &lt; "/>
14
+
15
+    <LinearLayout
16
+        android:id="@+id/pagesLayout"
17
+        android:layout_width="wrap_content"
18
+        android:layout_height="match_parent"
19
+        android:layout_gravity="center_horizontal"
20
+        android:orientation="horizontal">
21
+    </LinearLayout>
22
+
23
+    <TextView
24
+        android:layout_width="wrap_content"
25
+        android:layout_height="wrap_content"
26
+        android:id="@+id/nextPageView"
27
+        android:textAppearance="?android:attr/textAppearanceLarge"
28
+        android:layout_margin="2dp"
29
+        android:text=" &gt; "/>
30
+</LinearLayout>

+ 3
- 34
luticateutils/src/main/res/layout/view_pagination_fragment_layout.xml Voir le fichier

@@ -2,6 +2,7 @@
2 2
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 3
               android:layout_width="match_parent"
4 4
               android:layout_height="match_parent"
5
+              xmlns:app="http://schemas.android.com/apk/res-auto"
5 6
               android:orientation="vertical">
6 7
 
7 8
     <!-- Loading progress -->
@@ -39,45 +40,13 @@
39 40
                 android:layout_width="match_parent"
40 41
                 android:layout_height="match_parent"
41 42
                 android:orientation="vertical">
42
-
43
-                <ProgressBar
44
-                    style="?android:attr/progressBarStyleLarge"
45
-                    android:layout_width="wrap_content"
46
-                    android:layout_height="wrap_content"
47
-                    android:layout_marginBottom="8dp"/>
48 43
             </LinearLayout>
49 44
 
50
-
51
-            <LinearLayout
45
+            <com.luticate.utils.ui.views.DefaultPaginationView
52 46
                 android:layout_width="match_parent"
53 47
                 android:layout_height="wrap_content"
54 48
                 android:layout_below="@+id/itemsLayoutScrollView"
55
-                android:gravity="center">
56
-
57
-                <TextView
58
-                    android:layout_width="wrap_content"
59
-                    android:layout_height="wrap_content"
60
-                    android:id="@+id/previousPageView"
61
-                    android:textAppearance="?android:attr/textAppearanceLarge"
62
-                    android:layout_margin="2dp"
63
-                    android:text=" &lt; "/>
64
-
65
-                <LinearLayout
66
-                    android:id="@+id/pagesLayout"
67
-                    android:layout_width="wrap_content"
68
-                    android:layout_height="match_parent"
69
-                    android:layout_gravity="center_horizontal"
70
-                    android:orientation="horizontal">
71
-                </LinearLayout>
72
-
73
-                <TextView
74
-                    android:layout_width="wrap_content"
75
-                    android:layout_height="wrap_content"
76
-                    android:id="@+id/nextPageView"
77
-                    android:textAppearance="?android:attr/textAppearanceLarge"
78
-                    android:layout_margin="2dp"
79
-                    android:text=" &gt; "/>
80
-            </LinearLayout>
49
+                android:id="@+id/paginationView"/>
81 50
         </RelativeLayout>
82 51
     </ScrollView>
83 52
 </LinearLayout>

+ 6
- 0
luticateutils/src/main/res/values/attrs.xml Voir le fichier

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<resources>
3
+    <declare-styleable name="DefaultPaginationView">
4
+        <attr name="contextPagesCount" format="integer" />
5
+    </declare-styleable>
6
+</resources>

Chargement…
Annuler
Enregistrer