Search in sources :

Example 1 with Item

use of email.schaal.ocreader.database.model.Item in project ocreader by schaal.

the class ListActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_list);
    setSupportActionBar(binding.toolbarLayout.toolbar);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    binding.swipeRefreshLayout.setColorSchemeResources(R.color.primary);
    binding.swipeRefreshLayout.setOnRefreshListener(this);
    profileDrawerItem = new ProfileDrawerItem().withName(preferences.getString(Preferences.USERNAME.getKey(), getString(R.string.app_name))).withEmail(Preferences.URL.getString(preferences));
    updateUserProfile();
    IProfile profileSettingsItem = new ProfileSettingDrawerItem().withName(getString(R.string.account_settings)).withIconTinted(true).withIcon(R.drawable.ic_settings).withTag((Runnable) () -> {
        Intent loginIntent = new Intent(ListActivity.this, LoginActivity.class);
        startActivityForResult(loginIntent, LoginActivity.REQUEST_CODE);
    });
    accountHeader = new AccountHeaderBuilder().withActivity(this).withHeaderBackground(R.drawable.header_background).addProfiles(profileDrawerItem, profileSettingsItem).withCurrentProfileHiddenInList(true).withProfileImagesClickable(false).withSavedInstance(savedInstanceState).withOnAccountHeaderListener((view, profile, current) -> {
        if (profile instanceof Tagable) {
            Tagable tagable = (Tagable) profile;
            if (tagable.getTag() instanceof Runnable) {
                ((Runnable) tagable.getTag()).run();
                return false;
            }
        }
        return true;
    }).build();
    refreshDrawerItem = new PrimaryDrawerItem().withName(getString(R.string.action_sync)).withSelectable(false).withIconTintingEnabled(true).withIcon(R.drawable.ic_refresh).withIdentifier(REFRESH_DRAWER_ITEM_ID).withTag((Runnable) () -> SyncService.startSync(ListActivity.this));
    IDrawerItem settingsDrawerItem = new PrimaryDrawerItem().withName(R.string.settings).withIcon(R.drawable.ic_settings).withIconTintingEnabled(true).withSelectable(false).withTag((Runnable) () -> {
        startDrawer.closeDrawer();
        Intent settingsIntent = new Intent(ListActivity.this, SettingsActivity.class);
        startActivity(settingsIntent);
    });
    DrawerBuilder startDrawerBuilder = new DrawerBuilder().withActivity(this).withAccountHeader(accountHeader).addStickyDrawerItems(settingsDrawerItem, refreshDrawerItem).withOnDrawerListener(new Drawer.OnDrawerListener() {

        @Override
        public void onDrawerOpened(View drawerView) {
            drawerManager.getStartAdapter().updateUnreadCount(getRealm(), isShowOnlyUnread());
        }

        @Override
        public void onDrawerClosed(View drawerView) {
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
        }
    }).withOnDrawerItemClickListener((view, position, drawerItem) -> {
        if (drawerItem.getTag() instanceof TreeItem) {
            TreeItem item = (TreeItem) drawerItem.getTag();
            onStartDrawerItemClicked(item);
            return false;
        } else if (drawerItem.getTag() instanceof Runnable) {
            ((Runnable) drawerItem.getTag()).run();
        }
        return true;
    }).withSavedInstance(savedInstanceState);
    DrawerBuilder endDrawerBuilder = new DrawerBuilder().withActivity(this).withDrawerGravity(Gravity.END).withSavedInstance(savedInstanceState).withShowDrawerOnFirstLaunch(true).withOnDrawerListener(new Drawer.OnDrawerListener() {

        @Override
        public void onDrawerOpened(View drawerView) {
            drawerManager.getEndAdapter().updateUnreadCount(getRealm(), isShowOnlyUnread());
        }

        @Override
        public void onDrawerClosed(View drawerView) {
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
        }
    }).withOnDrawerItemClickListener((view, position, drawerItem) -> {
        if (drawerItem.getTag() instanceof Feed) {
            Feed feed = (Feed) drawerItem.getTag();
            onEndDrawerItemClicked(feed);
            return false;
        }
        return true;
    });
    startDrawerBuilder.withToolbar(binding.toolbarLayout.toolbar);
    startDrawer = startDrawerBuilder.build();
    drawerManager = new DrawerManager(this, startDrawer, endDrawerBuilder.append(startDrawer), unreadSwitchListener);
    layoutManager = new LinearLayoutManager(this);
    adapter = new ErrorAdapter(this, getRealm(), drawerManager.getState(), this, this);
    binding.fabMarkAllAsRead.setOnClickListener(new View.OnClickListener() {

        private void onCompletion(View view) {
            adapter.updateItems(false);
            view.setEnabled(true);
            binding.fabMarkAllAsRead.toggleSync();
        }

        @Override
        public void onClick(final View view) {
            if (binding.fabMarkAllAsRead.isSync()) {
                SyncService.startSync(ListActivity.this);
                binding.fabMarkAllAsRead.toggleSync();
            } else {
                Queries.markTemporaryFeedAsRead(getRealm(), () -> onCompletion(view), error -> {
                    Log.e(TAG, "Failed to mark temporary feed as read", error);
                    onCompletion(view);
                });
            }
        }
    });
    binding.fabMarkAllAsRead.setOnLongClickListener(v -> {
        Toast.makeText(ListActivity.this, R.string.mark_all_as_read, Toast.LENGTH_SHORT).show();
        return true;
    });
    binding.itemsRecyclerview.setAdapter(adapter);
    binding.itemsRecyclerview.setLayoutManager(layoutManager);
    if (savedInstanceState == null && getIntent().hasExtra(SyncService.EXTRA_ID)) {
        drawerManager.getState().restore(getRealm(), getIntent().getIntExtra(SyncService.EXTRA_ID, -10), null, false);
    } else {
        drawerManager.getState().restoreInstanceState(getRealm(), getPreferences(MODE_PRIVATE));
    }
    binding.itemsRecyclerview.addItemDecoration(new DividerItemDecoration(this, R.dimen.divider_inset));
    adapter.updateItems(false);
    if (savedInstanceState != null) {
        layoutManager.onRestoreInstanceState(savedInstanceState.getParcelable(LAYOUT_MANAGER_STATE));
        adapter.onRestoreInstanceState(savedInstanceState);
        if (adapter.getSelectedItemsCount() > 0) {
            actionMode = startActionMode(this);
        }
    }
    drawerManager.reloadAdapters(getRealm(), isShowOnlyUnread());
    // noinspection ConstantConditions
    getSupportActionBar().setTitle(drawerManager.getState().getTreeItem().getName());
}
Also used : ActionMode(android.view.ActionMode) Bundle(android.os.Bundle) OnCheckedChangeListener(com.mikepenz.materialdrawer.interfaces.OnCheckedChangeListener) Libs(com.mikepenz.aboutlibraries.Libs) DividerItemDecoration(email.schaal.ocreader.view.DividerItemDecoration) Nameable(com.mikepenz.materialdrawer.model.interfaces.Nameable) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) ActivityListBinding(email.schaal.ocreader.databinding.ActivityListBinding) Tagable(com.mikepenz.materialdrawer.model.interfaces.Tagable) Base64InputStream(android.util.Base64InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Item(email.schaal.ocreader.database.model.Item) TemporaryFeed(email.schaal.ocreader.database.model.TemporaryFeed) View(android.view.View) ProfileSettingDrawerItem(com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem) PreferenceManager(android.preference.PreferenceManager) SyncType(email.schaal.ocreader.service.SyncType) Log(android.util.Log) AccountHeader(com.mikepenz.materialdrawer.AccountHeader) ContextCompat(android.support.v4.content.ContextCompat) AllUnreadFolder(email.schaal.ocreader.database.model.AllUnreadFolder) PrimaryDrawerItem(com.mikepenz.materialdrawer.model.PrimaryDrawerItem) BroadcastReceiver(android.content.BroadcastReceiver) Queries(email.schaal.ocreader.database.Queries) ErrorAdapter(email.schaal.ocreader.view.ErrorAdapter) DrawerBuilder(com.mikepenz.materialdrawer.DrawerBuilder) Nullable(android.support.annotation.Nullable) Snackbar(android.support.design.widget.Snackbar) Context(android.content.Context) Intent(android.content.Intent) BitmapFactory(android.graphics.BitmapFactory) IProfile(com.mikepenz.materialdrawer.model.interfaces.IProfile) NonNull(android.support.annotation.NonNull) IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) TreeItem(email.schaal.ocreader.database.model.TreeItem) MenuItem(android.view.MenuItem) SyncService(email.schaal.ocreader.service.SyncService) Drawer(com.mikepenz.materialdrawer.Drawer) Toast(android.widget.Toast) Menu(android.view.Menu) User(email.schaal.ocreader.database.model.User) DrawerManager(email.schaal.ocreader.view.drawer.DrawerManager) DrawerLayout(android.support.v4.widget.DrawerLayout) ProfileDrawerItem(com.mikepenz.materialdrawer.model.ProfileDrawerItem) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) LibsBuilder(com.mikepenz.aboutlibraries.LibsBuilder) Feed(email.schaal.ocreader.database.model.Feed) LoadMoreAdapter(email.schaal.ocreader.view.LoadMoreAdapter) CompoundButton(android.widget.CompoundButton) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Gravity(android.view.Gravity) SharedPreferences(android.content.SharedPreferences) Bitmap(android.graphics.Bitmap) DataBindingUtil(android.databinding.DataBindingUtil) Base64(android.util.Base64) ItemViewHolder(email.schaal.ocreader.view.ItemViewHolder) AccountHeaderBuilder(com.mikepenz.materialdrawer.AccountHeaderBuilder) Activity(android.app.Activity) PrimaryDrawerItem(com.mikepenz.materialdrawer.model.PrimaryDrawerItem) IDrawerItem(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem) ProfileSettingDrawerItem(com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem) ErrorAdapter(email.schaal.ocreader.view.ErrorAdapter) TreeItem(email.schaal.ocreader.database.model.TreeItem) ProfileDrawerItem(com.mikepenz.materialdrawer.model.ProfileDrawerItem) AccountHeaderBuilder(com.mikepenz.materialdrawer.AccountHeaderBuilder) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) IProfile(com.mikepenz.materialdrawer.model.interfaces.IProfile) TemporaryFeed(email.schaal.ocreader.database.model.TemporaryFeed) Feed(email.schaal.ocreader.database.model.Feed) DrawerManager(email.schaal.ocreader.view.drawer.DrawerManager) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) Drawer(com.mikepenz.materialdrawer.Drawer) DividerItemDecoration(email.schaal.ocreader.view.DividerItemDecoration) View(android.view.View) DrawerBuilder(com.mikepenz.materialdrawer.DrawerBuilder) Tagable(com.mikepenz.materialdrawer.model.interfaces.Tagable)

Example 2 with Item

use of email.schaal.ocreader.database.model.Item in project ocreader by schaal.

the class ItemTypeAdapter method fromJson.

@Override
public Item fromJson(@NonNull JsonReader in) throws IOException {
    if (in.peek() == JsonReader.Token.NULL) {
        in.nextNull();
        return null;
    }
    final NullableJsonReader reader = new NullableJsonReader(in);
    final Item.Builder builder = new Item.Builder();
    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        switch(name) {
            case "id":
                builder.setId(in.nextLong());
                break;
            case "guid":
                builder.setGuid(in.nextString());
                break;
            case "guidHash":
                builder.setGuidHash(in.nextString());
                break;
            case "url":
                builder.setUrl(reader.nextString());
                break;
            case "title":
                builder.setTitle(StringUtils.cleanString(in.nextString()));
                break;
            case "author":
                builder.setAuthor(StringUtils.emptyToNull(in.nextString()));
                break;
            case "pubDate":
                builder.setPubDate(new Date(in.nextLong() * 1000));
                break;
            case "body":
                builder.setBody(in.nextString());
                break;
            case "enclosureMime":
                if (in.peek() != JsonReader.Token.NULL)
                    builder.setEnclosureMime(StringUtils.emptyToNull(in.nextString()));
                else
                    in.skipValue();
                break;
            case "enclosureLink":
                if (in.peek() != JsonReader.Token.NULL)
                    builder.setEnclosureLink(StringUtils.emptyToNull(in.nextString()));
                else
                    in.skipValue();
                break;
            case "publishedAt":
                builder.setPubDate(parseDate(in.nextString()));
                break;
            case "updatedAt":
                builder.setUpdatedAt(parseDate(in.nextString()));
                break;
            case "enclosure":
                parseEnclosure(reader, builder);
                break;
            case "feedId":
                builder.setFeedId(in.nextLong());
                break;
            case "isUnread":
            case "unread":
                builder.setUnread(in.nextBoolean());
                break;
            case "starred":
            case "isStarred":
                builder.setStarred(in.nextBoolean());
                break;
            case "lastModified":
                builder.setLastModified(in.nextLong());
                break;
            case "rtl":
                in.skipValue();
                break;
            case "fingerprint":
                builder.setFingerprint(reader.nextString());
                break;
            case "contentHash":
                // ignore for now, old items don't have this set yet.
                // item.setContentHash(in.nextString());
                in.skipValue();
                break;
            case "updatedDate":
                if (in.peek() == JsonReader.Token.NUMBER)
                    builder.setUpdatedAt(new Date(in.nextLong() * 1000));
                else
                    in.skipValue();
                break;
            default:
                Log.w(TAG, "Unknown value in item json: " + name);
                in.skipValue();
                break;
        }
    }
    in.endObject();
    return builder.build();
}
Also used : Item(email.schaal.ocreader.database.model.Item) Date(java.util.Date)

Example 3 with Item

use of email.schaal.ocreader.database.model.Item in project ocreader by schaal.

the class JsonTest method TestItemToJson.

@Test
public void TestItemToJson() throws IOException {
    Moshi moshi = new Moshi.Builder().add(Item.class, new ItemTypeAdapter()).build();
    Item item = new Item.Builder().setId(1).setContentHash("oijoijo").setUnreadChanged(true).setUnread(false).build();
    String itemJson = moshi.adapter(Item.class).toJson(item);
    ReducedItem reducedItem = moshi.adapter(ReducedItem.class).fromJson(itemJson);
    ReducedItem expectedReducedItem = new ReducedItem();
    expectedReducedItem.id = 1;
    expectedReducedItem.contentHash = "oijoijo";
    expectedReducedItem.isUnread = false;
    expectedReducedItem.isStarred = null;
    assertEquals(expectedReducedItem, reducedItem);
}
Also used : Item(email.schaal.ocreader.database.model.Item) ItemTypeAdapter(email.schaal.ocreader.api.json.ItemTypeAdapter) Moshi(com.squareup.moshi.Moshi) Test(org.junit.Test)

Example 4 with Item

use of email.schaal.ocreader.database.model.Item in project ocreader by schaal.

the class APIv12 method markItems.

private boolean markItems(@NonNull final MarkAction action, final Realm realm) throws IOException {
    final RealmResults<Item> results = realm.where(Item.class).equalTo(action.getChangedKey(), true).equalTo(action.getKey(), action.getValue()).findAll();
    if (results.size() == 0) {
        // Nothing to do, countdown and return
        return true;
    }
    ItemIds ids = null;
    ItemMap itemMap = null;
    if (action == MarkAction.MARK_UNREAD || action == MarkAction.MARK_READ) {
        ids = new ItemIds(results);
    } else {
        itemMap = new ItemMap(results);
    }
    Response<Void> response;
    switch(action) {
        case MARK_READ:
            response = api.markItemsRead(ids).execute();
            break;
        case MARK_UNREAD:
            response = api.markItemsUnread(ids).execute();
            break;
        case MARK_STARRED:
            response = api.markItemsStarred(itemMap).execute();
            break;
        case MARK_UNSTARRED:
            response = api.markItemsUnstarred(itemMap).execute();
            break;
        default:
            throw new IllegalArgumentException("Unkown mark action");
    }
    if (response.isSuccessful()) {
        realm.executeTransaction(realm1 -> {
            if (action == MarkAction.MARK_READ || action == MarkAction.MARK_UNREAD) {
                for (Item item : results) {
                    item.setUnreadChanged(false);
                }
            } else {
                for (Item item : results) {
                    item.setStarredChanged(false);
                }
            }
        });
    }
    return response.isSuccessful();
}
Also used : Item(email.schaal.ocreader.database.model.Item) ItemMap(email.schaal.ocreader.api.json.v12.ItemMap) ItemIds(email.schaal.ocreader.api.json.v12.ItemIds)

Example 5 with Item

use of email.schaal.ocreader.database.model.Item in project ocreader by schaal.

the class DatabaseTest method testItemInsert.

@Test
public void testItemInsert() {
    Realm realm = null;
    try {
        Feed feed = getTestFeed();
        Item item = getTestItem();
        realm = Realm.getDefaultInstance();
        Queries.insert(realm, feed);
        Queries.insert(realm, item);
        item = realm.where(Item.class).findFirst();
        assertEquals(item.getId(), 1);
        assertEquals(item.getTitle(), ITEM_TITLE);
        assertEquals(item.getBody(), BODY);
        assertEquals(item.getAuthor(), AUTHOR);
        assertNull(item.getEnclosureLink());
    } finally {
        assertNotNull(realm);
        realm.close();
    }
}
Also used : TestGenerator.getTestItem(email.schaal.ocreader.TestGenerator.getTestItem) Item(email.schaal.ocreader.database.model.Item) Realm(io.realm.Realm) Feed(email.schaal.ocreader.database.model.Feed) TestGenerator.getTestFeed(email.schaal.ocreader.TestGenerator.getTestFeed) Test(org.junit.Test)

Aggregations

Item (email.schaal.ocreader.database.model.Item)9 Intent (android.content.Intent)2 Bundle (android.os.Bundle)2 MenuItem (android.view.MenuItem)2 PrimaryDrawerItem (com.mikepenz.materialdrawer.model.PrimaryDrawerItem)2 ProfileDrawerItem (com.mikepenz.materialdrawer.model.ProfileDrawerItem)2 ProfileSettingDrawerItem (com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem)2 IDrawerItem (com.mikepenz.materialdrawer.model.interfaces.IDrawerItem)2 Test (org.junit.Test)2 Activity (android.app.Activity)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 DataBindingUtil (android.databinding.DataBindingUtil)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 PreferenceManager (android.preference.PreferenceManager)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 Snackbar (android.support.design.widget.Snackbar)1