Giter VIP home page Giter VIP logo

Comments (3)

hayribakici avatar hayribakici commented on June 2, 2024 2

This is a good idea, however I suggest, that the cursorAfter attribute shouldn't be in the Page-object, rather in its own CursorPage-object. This would reflect more the spotify object specification. @rinukkusu @chances , what do you think about having a new object CursorPage that extends the Page-object?

from spotify-dart.

stefanoromanello avatar stefanoromanello commented on June 2, 2024

This is how I solved this while they release a new fixed version, edit the following files of spotify-dart:

_models.g.dart

Paging<T> _$PagingFromJson<T>(Map<String, dynamic> json) {
  return Paging<T>()
    ..href = json['href'] as String
    ..itemsNative = itemsNativeFromJson(json['items'] as List)
    ..limit = json['limit'] as int
    ..next = json['next'] as String
    ..offset = json['offset'] as int
    ..previous = json['previous'] as String
    ..cursorsAfter = json['cursors'] != null ? json['cursors']['after'] as String : null
    ..total = json['total'] as int;
}

paging.dart

@JsonSerializable(createToJson: false)
class Paging<T> extends Object {
  Paging();

  factory Paging.fromJson(Map<String, dynamic> json) => _$PagingFromJson(json);

  /// A link to the Web API endpoint returning the full result of the request.
  String href;

  /// The requested data
  ///
  /// Note this is the raw JSON value. Use a [Page]'s [Page.items] to get the
  /// requested data as a deserialized list.
  @JsonKey(
      name: 'items', fromJson: itemsNativeFromJson, toJson: itemsNativeToJson)
  Iterable<dynamic> itemsNative;

  /// The maximum number of items in the response (as set in the query or by
  /// default).
  int limit;

  /// URL to the next page of items. ([null] if none)
  String next;

  /// The offset of the items returned (as set in the query or by default).
  int offset;

  /// URL to the previous page of items. (null if none)
  String previous;

  /// The total number of items available to return.
  int total;

  //Cursor used for followers endpoint
  String cursorsAfter;
}

endpoint_paging.dart

class Page<T> {
  final Paging<T> _paging;
  Iterable<T> _items;
  Object _container;

  Page(this._paging, ParserFunction<T> pageItemParser, [Object pageContainer]) {
    _items = _paging.itemsNative.map(pageItemParser);
    _container = pageContainer;
  }

  /// The offset-based paging object is a container for a set of objects. It
  /// contains a key called items (whose value is an array of the requested
  /// objects) along with other keys like previous, next and limit that can be
  /// useful in future calls.
  Paging<T> get metadata => _paging;

  /// The requested data
  Iterable<T> get items => _items;

  /// The object containing this page, if applicable
  Object get container => _container;

  bool get isLast {
    if (_paging.offset != null)
      return _paging.offset + _paging.limit >= _paging.total;
    else
      return _paging.next==null;
  }

  int get nextOffset {
    if (_paging.offset != null)
      return _paging.offset + _paging.limit;
    else
      return null;  
  }

  int get totalItems => _paging.total; 

  String get cursorsAfter => _paging.cursorsAfter;
  
}

And to obtain the follower here a simple method to use the API:

void getUserFollowing() async {
    BundledPages bundeled = await _spotifyApi.me.following(FollowingType.artist);

    int limit = 5;
    bool readData = true;
    List<Page<Object>> pages;
    Page<dynamic> currentPage;

    while (readData) {
      currentPage != null
          ? pages = await bundeled.getPageAfter(limit, currentPage.cursorsAfter)
          : pages = await bundeled.first(limit);
      currentPage = pages.first;
      readData = !currentPage.isLast;

      List<Artist> artists = currentPage.items.toList().cast<Artist>();
      for (var artist in artists) {
        print(artist.name);
      }
    }
  }

from spotify-dart.

rinukkusu avatar rinukkusu commented on June 2, 2024

@hayribakici sounds cleaner to me, yes!

from spotify-dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.