Advertisement
mrblab

lib/models/article.dart

Feb 8th, 2024
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.76 KB | None | 0 0
  1. import 'package:wordpress_app/config/wp_config.dart';
  2.  
  3. class Article {
  4.   final int? id;
  5.   final String? title;
  6.   final String? content;
  7.   final String? image;
  8.   final String? author;
  9.   final String? avatar;
  10.   final String? category;
  11.   final DateTime? date;
  12.   final String? link;
  13.   final int? catId;
  14.   final List<int>? tags;
  15.   final int? authorId;
  16.   final bool? featured;
  17.   final bool? videoPost;
  18.   final String? videoUrl;
  19.   final String? youtubeUrl;
  20.   final String? viemoUrl;
  21.   final String? views;
  22.  
  23.   Article(
  24.       {this.id,
  25.       this.title,
  26.       this.content,
  27.       this.image,
  28.       this.author,
  29.       this.avatar,
  30.       this.category,
  31.       this.date,
  32.       this.link,
  33.       this.catId,
  34.       this.tags,
  35.       this.authorId,
  36.       this.featured,
  37.       this.videoPost,
  38.       this.videoUrl,
  39.       this.youtubeUrl,
  40.       this.viemoUrl,
  41.       this.views,
  42.   });
  43.  
  44.   factory Article.fromJson(Map<String, dynamic> json) {
  45.     return Article(
  46.       id: json['id'] ?? 0,
  47.       title: json['title']['rendered'] ?? '',
  48.       content: json['content']['rendered'] ?? '',
  49.       image: json['custom']["featured_image"] != false ? json['custom']["featured_image"] : WpConfig.defaultFeatureImage,
  50.       author: json['custom']['author']['name'] ?? '',
  51.       avatar: json['custom']['author']['avatar'] ?? 'https://icon-library.com/images/avatar-icon/avatar-icon-27.jpg',
  52.       date: DateTime.parse("${json['date_gmt']}Z").toLocal(),
  53.       link: json['link'] ?? '',
  54.       category: json["custom"]["categories"][0]["name"] ?? '',
  55.       catId: json["custom"]["categories"][0]["cat_ID"] ?? 0,
  56.       tags: json['tags'] != null ? List<int>.from(json['tags']) : [],
  57.       authorId: json['author']?.toInt() ?? 0,
  58.       featured: _getBool(json['featured']),
  59.       videoPost: _getBool(json['video_post']),
  60.       videoUrl: _getVideoUrl(json['video_url']),
  61.       youtubeUrl: json['youtube_url'] ?? '',
  62.       viemoUrl: json['vimeo_url'] ?? '',
  63.       views:  _getViews(json['custom']['views']?? 0),
  64.     );
  65.   }
  66.  
  67.   factory Article.fromJsonLocal(Map<dynamic, dynamic> json) {
  68.     return Article(
  69.       id: json['id'],
  70.       title: json['title'],
  71.       content: json['content'],
  72.       image: json['image'],
  73.       author: json['author'],
  74.       avatar: json['avatar'],
  75.       date: json['date'],
  76.       link: json['link'],
  77.       category: json['category'],
  78.       catId: json['cat_id'],
  79.       tags: json['tags'],
  80.       authorId: json['author_id'],
  81.       featured: json['featured'],
  82.       videoPost: json['video_post'],
  83.       videoUrl: json['video_url'],
  84.       youtubeUrl: json['youtube_url'],
  85.       viemoUrl: json['vimeo_url']
  86.     );
  87.   }
  88.  
  89.   Map<String, dynamic> toJson() {
  90.     return {
  91.       'id' : id,
  92.       'title': title ?? '',
  93.       'content': content ?? '',
  94.       'image': image ?? '',
  95.       'author': author ?? '',
  96.       'avatar': avatar ?? '',
  97.       'date': date,
  98.       'link': link ?? '',
  99.       'category': category ?? '',
  100.       'cat_id': catId ?? 0,
  101.       'tags': tags ?? [],
  102.       'author_id': authorId ?? 0,
  103.       'featured': featured ?? false,
  104.       'video_post': videoPost ?? false,
  105.       'video_url': videoUrl ?? '',
  106.       'youtube_url': youtubeUrl ?? '',
  107.       'vimeo_url' : viemoUrl ?? ''
  108.     };
  109.   }
  110.  
  111.   static bool _getBool (dynamic value){
  112.     if(value == '1'){
  113.       return true;
  114.     }else{
  115.       return false;
  116.     }
  117.   }
  118.  
  119.   static String _getVideoUrl (dynamic value){
  120.     if(value == null || value == false){
  121.       return '';
  122.     }else{
  123.       if(value['guid'] == null){
  124.         return '';
  125.       }else{
  126.         return value['guid'];
  127.       }
  128.     }
  129.   }
  130.  
  131.   static String _getViews (value){
  132.     if(value is String){
  133.       return value;
  134.     }else if(value is int){
  135.       return value.toString();
  136.     }else{
  137.       return '0';
  138.     }
  139.   }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement