Skip to content

Commit 850c7bf

Browse files
committed
imporoved EachPost
1 parent 4bdd0db commit 850c7bf

13 files changed

+1089
-9
lines changed

lib/src/app2.dart

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import 'blocs/database_helper.dart';
1111
import 'package:sqflite/sqflite.dart';
1212
import 'package:cached_network_image/cached_network_image.dart';
1313

14+
15+
1416
//WPCLIENT_START
1517
import 'package:hawalnir1/wordpress_client.dart';
1618

@@ -66,3 +68,14 @@ class HawalnirHome2State extends State {
6668
);
6769
}
6870
}
71+
72+
73+
Future<List<Post>> fetchPostsFromDatabase() async {
74+
var dbHelper = DatabaseHelper() ;
75+
Future<List<Post>> posts = dbHelper.getPostList();
76+
debugPrint(posts.toString()) ;
77+
return posts;
78+
79+
}
80+
81+
//DataBase

lib/src/blocs/database_helper.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:sqflite/sqflite.dart';
33
import 'dart:async';
44
import 'dart:io';
55
import 'package:path_provider/path_provider.dart';
6-
import 'item-model.dart';
6+
import '../models/post.dart';
77

88
class DatabaseHelper {
99
static DatabaseHelper _databaseHelper; // Singleton DB helper
@@ -92,7 +92,7 @@ Future<int> insertPost(Post post) async {
9292

9393
List<Post> postList = List<Post>() ;
9494
for(int i = 0; i < count; i++ ) {
95-
postList.add(Post.fromMapObject(postMapList[i]));
95+
postList.add(Post.fromMap(postMapList[i]));
9696
}
9797
return postList; //from here Code has been cloned to Projects
9898
}

lib/src/client.dart

+342
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
4+
import 'package:http/http.dart';
5+
import 'package:logging/logging.dart';
6+
7+
import 'models/category.dart';
8+
import 'models/media.dart';
9+
import 'models/post.dart';
10+
import 'models/users.dart';
11+
import 'config.dart';
12+
typedef void APIErrorHandler(String endpoint, int statusCode, String response);
13+
14+
15+
class WordpressClient {
16+
final Logger _logger = new Logger('API');
17+
String _baseURL;
18+
Client _client;
19+
APIErrorHandler _errorHandler;
20+
21+
WordpressClient(this._baseURL, this._client, [this._errorHandler = null]);
22+
23+
/// Get all available categories.
24+
///
25+
/// If [hideEmpty] is false then ALL categories will be returned, and
26+
/// [excludeIDs] can be used to ignore specific category IDs
27+
Future<List<Category>> listCategories(
28+
{bool hideEmpty: true, List<int> excludeIDs: null}) async {
29+
String _endpoint = '/wp/v2/categories';
30+
31+
// Build query string
32+
String queryString = '';
33+
if (hideEmpty) {
34+
queryString = _addParamToQueryString(queryString, 'hide_empty', 'true');
35+
}
36+
if (excludeIDs != null && excludeIDs.length > 0) {
37+
queryString =
38+
_addParamToQueryString(queryString, 'exclude', excludeIDs.join(','));
39+
}
40+
41+
// Append the query string
42+
_endpoint += queryString;
43+
44+
// Retrieve the data
45+
List<Map> categoryMaps = await _get(_endpoint);
46+
47+
List<Category> categories = new List();
48+
categories = categoryMaps
49+
.map((categoryMap) => new Category.fromMap(categoryMap))
50+
.toList();
51+
52+
return categories;
53+
}
54+
55+
/// Get all available posts.
56+
///
57+
/// If [categoryIDs] list is provided then only posts within those categories
58+
/// will be returned. Use [injectObjects] to have full objects injected
59+
/// rather than just the object ID (i.e. a posts's featured media). The [page]
60+
/// and [perPage] parameters allow for pagination.
61+
Future<List<Post>> listPosts(
62+
{List<int> categoryIDs: null,
63+
bool injectObjects: false,
64+
List<int> excludeIDs: null,
65+
int page: 1,
66+
int perPage: 10 }) async {
67+
String _endpoint = '/wp/v2/posts?_embed';
68+
69+
// Build query string starting with pagination
70+
String queryString = '&per_page=$perPage';
71+
//queryString = _addParamToQueryString(queryString, 'page', page.toString());
72+
73+
// If category IDs were sent, limit to those
74+
if (categoryIDs != null && categoryIDs.length > 0) {
75+
queryString = _addParamToQueryString(
76+
queryString, 'categories', categoryIDs.join(','));
77+
}
78+
79+
// Exclude posts?
80+
if (excludeIDs != null && excludeIDs.length > 0) {
81+
queryString =
82+
_addParamToQueryString(queryString, 'exclude', excludeIDs.join(','));
83+
}
84+
85+
// Append the query string
86+
_endpoint += queryString;
87+
//_endpoint =
88+
// Retrieve the data
89+
List<Map> postMaps = await _get(_endpoint);
90+
//print(_endpoint) ;
91+
92+
List<Post> posts = new List();
93+
posts = postMaps.map((postMap) => new Post.fromMap(postMap)).toList();
94+
//print(posts.toString()) ;
95+
// Inject objects if requested
96+
if (injectObjects) {
97+
for (Post p in posts) {
98+
if (p.featuredMediaID != null && p.featuredMediaID > 0) {
99+
p.featuredMedia = await getMedia(p.featuredMediaID);
100+
}
101+
}
102+
}
103+
104+
return posts;
105+
}
106+
107+
/// Get all available media.
108+
///
109+
/// If [mediaIDs] list is provided then only these specific media items
110+
/// will be returned. The [page] and [perPage] parameters allow for pagination.
111+
Future<List<Media>> listMedia({List<int> includeIDs,
112+
int page: 1,
113+
perPage: 10 }) async {
114+
String _endpoint = '/wp/v2/media';
115+
116+
// Build query string starting with pagination
117+
String queryString = '';
118+
queryString = _addParamToQueryString(queryString, 'page', page.toString());
119+
queryString =
120+
_addParamToQueryString(queryString, 'per_page', perPage.toString());
121+
122+
// Requesting specific items
123+
if (includeIDs != null && includeIDs.length > 0) {
124+
queryString =
125+
_addParamToQueryString(queryString, 'include', includeIDs.join(','));
126+
}
127+
128+
// Append the query string
129+
_endpoint += queryString;
130+
131+
// Retrieve the data
132+
List<Map> mediaMaps = await _get(_endpoint);
133+
134+
List<Media> media = new List();
135+
media = mediaMaps.map((mediaMap) => new Media.fromMap(mediaMap)).toList();
136+
137+
return media;
138+
}
139+
140+
141+
142+
143+
144+
//****** */
145+
/// Get all available user.
146+
///
147+
/// If [userIDs] list is provided then only these specific user items
148+
/// will be returned. The [page] and [perPage] parameters allow for pagination.
149+
/* Future<List<User>> listUser() async {
150+
String _endpoint = '/wp/v2/users';
151+
152+
List<Map> userMaps = await _get(_endpoint);
153+
154+
List<User> user = new List();
155+
user = userMaps.map((userMap) => new User.fromMap(userMap)).toList();
156+
157+
return user;
158+
}
159+
*/
160+
Future<List<User>> listUser({List<int> includeIDs,
161+
int page: 1,
162+
int perPage: 10}) async {
163+
String _endpoint = '/wp/v2/users';
164+
165+
// Build query string starting with pagination
166+
String queryString = '';
167+
queryString = _addParamToQueryString(queryString, 'page', page.toString());
168+
queryString =
169+
_addParamToQueryString(queryString, 'per_page', perPage.toString());
170+
171+
// Requesting specific items
172+
if (includeIDs != null && includeIDs.length > 0) {
173+
queryString =
174+
_addParamToQueryString(queryString, 'include', includeIDs.join(','));
175+
}
176+
177+
// Append the query string
178+
_endpoint += queryString;
179+
180+
// Retrieve the data
181+
List<Map> userMaps = await _get(_endpoint);
182+
183+
List<User> user = new List();
184+
user = userMaps.map((userMap) => new User.fromMap(userMap)).toList();
185+
186+
return user;
187+
}
188+
189+
190+
191+
192+
193+
194+
195+
/// Get post
196+
Future<Post> getPost(int postID, {bool injectObjects: true}) async {
197+
if (postID == null) {
198+
return null;
199+
}
200+
201+
String _endpoint = '/wp/v2/posts/$postID?_embed';
202+
203+
// Retrieve the data
204+
Map postMap = await _get(_endpoint);
205+
if (postMap == null) {
206+
return null;
207+
}
208+
209+
Post p = new Post.fromMap(postMap);
210+
211+
// Inject objects if requested
212+
if (injectObjects) {
213+
if (p.featuredMediaID != null && p.featuredMediaID > 0) {
214+
p.featuredMedia = await getMedia(p.featuredMediaID);
215+
}
216+
}
217+
218+
return p;
219+
}
220+
221+
/// Get media item
222+
Future<Media> getMedia(int mediaID) async {
223+
if (mediaID == null) {
224+
return null;
225+
}
226+
227+
String _endpoint = '/wp/v2/media/$mediaID';
228+
229+
// Retrieve the data
230+
Map mediaMap = await _get(_endpoint);
231+
if (mediaMap == null) {
232+
return null;
233+
}
234+
235+
return new Media.fromMap(mediaMap);
236+
}
237+
238+
239+
/// Get media item
240+
Future<Media> getAttMedia(int mediaID) async {
241+
if (mediaID == null) {
242+
return null;
243+
}
244+
245+
String _endpoint = '/wp/v2/media/$mediaID';
246+
247+
// Retrieve the data
248+
Map mediaMap = await _get(_endpoint);
249+
if (mediaMap == null) {
250+
return null;
251+
}
252+
253+
return new Media.fromMap(mediaMap);
254+
}
255+
256+
257+
258+
259+
/// Get User item
260+
Future<User> getUser(int userID) async {
261+
if (userID == null) {
262+
return null;
263+
}
264+
265+
String _endpoint = '/wp/v2/users/$userID';
266+
267+
// Retrieve the user data
268+
Map userMap = await _get(_endpoint);
269+
if (userMap == null) {
270+
return null;
271+
}
272+
273+
return new User.fromMap(userMap);
274+
}
275+
276+
277+
278+
279+
280+
281+
282+
283+
284+
285+
286+
_handleError(String endpoint, int statusCode, String response) {
287+
// If an error handler has been provided use that, otherwise log
288+
if (_errorHandler != null) {
289+
_errorHandler(endpoint, statusCode, response);
290+
return;
291+
}
292+
293+
_logger.log(
294+
Level.SEVERE, "Received $statusCode from '$endpoint' => $response");
295+
}
296+
297+
Future _get(String url) async {
298+
dynamic jsonObj;
299+
String endpoint = '$_baseURL$url';
300+
301+
try {
302+
Response response = await _client.get(endpoint , headers: {"Accept": "application/json"} );
303+
304+
// Error handling
305+
if (response.statusCode != 200) {
306+
_handleError(url, response.statusCode, response.body);
307+
return null;
308+
}
309+
jsonObj = json.decode(response.body);
310+
} catch (e) {
311+
_logger.log(Level.SEVERE, 'Error in GET call to $endpoint', e);
312+
}
313+
314+
if (jsonObj is List) {
315+
// This is needed for Dart 2 type constraints
316+
return jsonObj.map((item) => item as Map).toList();
317+
}
318+
319+
return jsonObj;
320+
}
321+
322+
String _addParamToQueryString(String queryString, String key, String value) {
323+
if (queryString == null) {
324+
queryString = '';
325+
}
326+
327+
// If this is our first parameter, start with '?'
328+
if (queryString.length == 0) {
329+
queryString += '?';
330+
}
331+
332+
// Otherwise, add '&'
333+
else {
334+
queryString += '&';
335+
}
336+
337+
// TODO URL encode
338+
queryString += '$key=$value';
339+
340+
return queryString;
341+
}
342+
}

0 commit comments

Comments
 (0)