Android Open Source - vocabulary-list Card List Reader Impl






From Project

Back to project page vocabulary-list.

License

The source code is released under:

Apache License

If you think the Android project vocabulary-list listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rulych.vocabularylist.model.persistence.impl;
//ww  w  . jav  a2  s  . com
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Contents;
import com.google.android.gms.drive.DriveFile;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.rulych.vocabularylist.model.Card;
import com.rulych.vocabularylist.model.persistence.impl.exception.CouldNotReadFileException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.functions.Func0;
import rx.schedulers.Schedulers;
import rx.util.async.Async;
import timber.log.Timber;

import static com.google.android.gms.drive.DriveApi.ContentsResult;
import static com.google.android.gms.drive.DriveFile.MODE_READ_ONLY;

public class CardListReaderImpl implements CardListReader {

    private Gson mGson;
    private GoogleApiClient mApiClient;
    private DriveFile mDriveFile;

    public CardListReaderImpl(Gson gson, GoogleApiClient apiClient, DriveFile driveFile) {
        mGson = gson;
        mApiClient = apiClient;
        mDriveFile = driveFile;
    }

    public Observable<List<Card>> read() {
        return Async.start(new CardObservableOnSubscribe(mDriveFile), Schedulers.io());
    }

    private class CardObservableOnSubscribe implements Func0<List<Card>> {
        private final DriveFile mDriveFile;
        private Contents mContents;

        public CardObservableOnSubscribe(DriveFile driveFile) {
            mDriveFile = driveFile;
        }

        @Override public List<Card> call() {
            getFileContents();
            return readFile();
        }

        private void getFileContents() {
            Timber.d("Got file with ID: %s", mDriveFile.getDriveId().encodeToString());
            ContentsResult result = mDriveFile.openContents(mApiClient, MODE_READ_ONLY, null)
                    .await();
            if (!result.getStatus().isSuccess()) {
                throw new CouldNotReadFileException("Error while trying to open file: (%d) %s",
                        result.getStatus().getStatusCode(),
                        result.getStatus().getStatusMessage());
            }

            mContents = result.getContents();
        }

        private List<Card> readFile() {
            InputStreamReader inputStreamReader = null;
            try {
                InputStream inputStream = mContents.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);

                Type type = new TypeToken<List<Card>>() {}.getType();
                List<Card> cards = mGson.fromJson(inputStreamReader, type);
                if (cards == null) {
                    cards = new ArrayList<Card>();
                }

                return cards;
            } finally {
                try {
                    inputStreamReader.close();
                } catch (Exception e) {
                    // Nothing!
                }
            }
        }
    }

}




Java Source Code List

com.rulych.vocabularylist.AppModule.java
com.rulych.vocabularylist.VocabularyListApplication.java
com.rulych.vocabularylist.activities.VocabularyListActivity.java
com.rulych.vocabularylist.adapters.CardAdapter.java
com.rulych.vocabularylist.fragments.BaseCardFragment.java
com.rulych.vocabularylist.fragments.CardFragment.java
com.rulych.vocabularylist.fragments.DriveApiFragment.java
com.rulych.vocabularylist.fragments.NewCardFragment.java
com.rulych.vocabularylist.fragments.VocabularyListFragment.java
com.rulych.vocabularylist.model.Card.java
com.rulych.vocabularylist.model.comparation.CardComparator.java
com.rulych.vocabularylist.model.persistence.CardDAO.java
com.rulych.vocabularylist.model.persistence.PersistenceModule.java
com.rulych.vocabularylist.model.persistence.impl.CardDAOImpl.java
com.rulych.vocabularylist.model.persistence.impl.CardListReaderImpl.java
com.rulych.vocabularylist.model.persistence.impl.CardListReader.java
com.rulych.vocabularylist.model.persistence.impl.CardListWriterImpl.java
com.rulych.vocabularylist.model.persistence.impl.CardListWriter.java
com.rulych.vocabularylist.model.persistence.impl.FileGetterImpl.java
com.rulych.vocabularylist.model.persistence.impl.FileGetter.java
com.rulych.vocabularylist.model.persistence.impl.exception.CardNotFoundException.java
com.rulych.vocabularylist.model.persistence.impl.exception.CouldNotGetFileException.java
com.rulych.vocabularylist.model.persistence.impl.exception.CouldNotReadFileException.java
com.rulych.vocabularylist.model.persistence.impl.exception.CouldNotWriteFileException.java
com.rulych.vocabularylist.util.BaseRuntimeException.java
com.rulych.vocabularylist.util.FloatingActionButtonAnimator.java
com.rulych.vocabularylist.util.ThreadLoggingTree.java
com.rulych.vocabularylist.util.UtilModule.java
com.rulych.vocabularylist.util.impl.FloatingActionButtonAnimatorImpl.java