Search in sources :

Example 1 with QuantumFluxBatchDispatcher

use of me.himanshusoni.quantumflux.model.util.QuantumFluxBatchDispatcher in project QuantumFlux by himanshu-soni.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // For sync adapter
    mAccount = SyncUtils.createSyncAccount(this, "Dummy Account");
    SyncUtils.startPeriodicSync(mAccount);
    // delete all previous data
    QuantumFlux.deleteAll(Book.class);
    QuantumFlux.deleteAll(Author.class);
    QuantumFlux.deleteAll(Publisher.class);
    Book book = new Book();
    book.name = "Sorcerer's Stone";
    book.isbn = "122342564";
    Drawable drawable = getResources().getDrawable(R.drawable.hpsc);
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    book.bookCover = stream.toByteArray();
    QuantumFlux.insert(book);
    Author author = new Author();
    author.name = "J.K. Rollings";
    author.save();
    Publisher publisher = new Publisher();
    publisher.name = "Bloomsbury";
    QuantumFlux.insert(publisher);
    ImageView cover = (ImageView) findViewById(R.id.cover);
    TextView bookName = (TextView) findViewById(R.id.book_name);
    TextView authorName = (TextView) findViewById(R.id.author_name);
    TextView pubName = (TextView) findViewById(R.id.publisher_name);
    Book firstBook = Select.from(Book.class).first();
    Author firstAuthor = Select.from(Author.class).first();
    Publisher firstPublisher = Select.from(Publisher.class).first();
    bookName.setText(firstBook.name);
    authorName.setText(firstAuthor.name);
    pubName.setText(firstPublisher.name);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(firstBook.bookCover, 0, firstBook.bookCover.length);
    cover.setImageBitmap(decodedByte);
    // update
    Author first = Select.from(Author.class).first();
    first.name = "J. K. Rowling";
    first.update();
    authorName.setText(first.name);
    // update from QuantumFlux
    Book isbnBook = Select.from(Book.class).whereEquals("isbn", "122342564").first();
    isbnBook.isbn = "122342567";
    QuantumFlux.update(isbnBook);
    // batch insert example
    int batchSize = 100;
    QuantumFluxBatchDispatcher<Book> dispatcher = new QuantumFluxBatchDispatcher<Book>(this, Book.class, batchSize);
    for (int i = 0; i < batchSize; i++) {
        Book b = new Book();
        b.name = "Book " + i;
        b.isbn = "ISBN" + b.hashCode();
        dispatcher.add(b);
    }
    dispatcher.release(true);
    int count = Select.from(Book.class).queryAsCount();
    Toast.makeText(this, "Total Books : " + count, Toast.LENGTH_LONG).show();
    // To notify sync
    try {
        Book newBook = new Book();
        newBook.name = "Chamber of Secrets";
        QuantumFluxSyncHelper.insert(getContentResolver().acquireContentProviderClient(SyncConstants.AUTHORITY), newBook);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    SyncUtils.refreshManually(mAccount);
}
Also used : BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Publisher(me.himanshusoni.quantumflux.sample.data.Publisher) QuantumFluxBatchDispatcher(me.himanshusoni.quantumflux.model.util.QuantumFluxBatchDispatcher) Bitmap(android.graphics.Bitmap) Book(me.himanshusoni.quantumflux.sample.data.Book) Author(me.himanshusoni.quantumflux.sample.data.Author) TextView(android.widget.TextView) ImageView(android.widget.ImageView) RemoteException(android.os.RemoteException)

Aggregations

Bitmap (android.graphics.Bitmap)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Drawable (android.graphics.drawable.Drawable)1 RemoteException (android.os.RemoteException)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 QuantumFluxBatchDispatcher (me.himanshusoni.quantumflux.model.util.QuantumFluxBatchDispatcher)1 Author (me.himanshusoni.quantumflux.sample.data.Author)1 Book (me.himanshusoni.quantumflux.sample.data.Book)1 Publisher (me.himanshusoni.quantumflux.sample.data.Publisher)1