Android Open Source - vocabulary-list Card List Writer 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;
/* www  .j  av a2s  .  co  m*/
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.drive.Contents;
import com.google.android.gms.drive.DriveFile;
import com.google.gson.Gson;

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

import java.io.OutputStreamWriter;
import java.util.List;

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

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

public class CardListWriterImpl implements CardListWriter {

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

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

    public Observable<Void> write(final List<Card> cards) {
        return Async.start(new WriteFileFunc(cards), Schedulers.io());
    }

    private class WriteFileFunc implements Func0<Void> {
        private final List<Card> mCards;
        private Contents mContents;

        public WriteFileFunc(List<Card> cards) {
            mCards = cards;
        }

        @Override public Void call() {
            getFileContents();
            writeFile();
            commitFile();
            return null;
        }

        private void getFileContents() {
            ContentsResult result = mDriveFile.openContents(mApiClient, MODE_WRITE_ONLY, null)
                    .await();
            if (!result.getStatus().isSuccess()) {
                throw new CouldNotWriteFileException("Error while trying to open file: (%d) %s",
                        result.getStatus().getStatusCode(),
                        result.getStatus().getStatusMessage());
            }

            mContents = result.getContents();
        }

        private void writeFile() {
            OutputStreamWriter outputStreamWriter = null;
            try {
                outputStreamWriter = new OutputStreamWriter(mContents.getOutputStream());
                mGson.toJson(mCards, mCards.getClass(), outputStreamWriter);
            } finally {
                try {
                    outputStreamWriter.close();
                } catch (Exception e) {
                    // Nothing!
                }
            }
        }

        private void commitFile() {
            Status status = mDriveFile.commitAndCloseContents(mApiClient, mContents).await();
            if (!status.getStatus().isSuccess()) {
                throw new CouldNotWriteFileException("Error with file commit: (%d) %s",
                        status.getStatus().getStatusCode(),
                        status.getStatus().getStatusMessage());
            }
        }
    }

}




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