Git Product home page Git Product logo

oxforddictionarysample's Introduction

Oxford Dictionary Sample

Screenshot

Tools Used

  • Android Studio
  • Swagger Codegen

Libraries Used

Useful Reading

This sample makes use of code generated from the Oxford Dictionary API Swagger documentation - take a look at https://github.com/psh/oxford-dictionary-api-code-gen for the steps to generate clients in a variety of other languages.

API Calls

Retrofit REST calls to the Oxford Dictionary API look like you are making a simple method call, for instance

entriesApi.getDictionaryEntries("en", searchTerm, BuildConfig.APP_ID, BuildConfig.APP_KEY);

RxJava makes it a breeze to process the highly nested data structure that is returned from the service call

entriesApi.getDictionaryEntries("en", searchTerm, BuildConfig.APP_ID, BuildConfig.APP_KEY)
    .doOnSubscribe(d -> hideKeyboard())
    .flatMap(re -> Observable.fromIterable(re.getResults()))
    .flatMap(he -> Observable.fromIterable(he.getLexicalEntries()))
    .flatMap(le -> Observable.fromIterable(le.getEntries()).map(e -> new CategorizedEntry(searchTerm, le.getLexicalCategory(), e)))
    .flatMap(ce -> Observable.fromIterable(ce.entry.getSenses()).map(s -> new Definition(ce.category, ce.word, ce.entry, s)))
    .toList()
    .observeOn(AndroidSchedulers.mainThread())
    .map(this::createAdapter)
    .subscribe(this::updateRecyclerView);

There are a number of points to note:

  1. The Observable chain doesnt execute until a call is made to subscribe(). The doOnSubscribe() lambda is therefore calls immediately before the service call kicks off.
  2. The Retrofit interface was configured to automatically run on a background thread, so we switch back to Android's main thread again for the last couple of steps (by calling observeOn()) before we touch any GUI components.
  3. The APP_ID and the APP_KEY are externally defined in the build.gradle file.
  4. Walking down the chain of data from RetrieveEntry to HeadwordEntry to LexicalEntry to Sense would normally result in a deeply nested set of loops. Transforming that processing into an Observable chain makes the code much more readable.
  5. For simplicity this sample omits error handling. That said, it's pretty easy to supply an additional Consumer to the subscribe() call that will allow you to catch errors that might arise.

Final Display

Writing the adapter for a RecyclerView can be a lot of work. The Renderers library allows you to focus on the parts of the process that are most important leaving the library to take care of the rest. This sample doesnt quite do the library justice as there is only a single type of row in the RecyclerView!

oxforddictionarysample's People

Contributors

psh avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.