diff --git a/app/src/main/java/com/readrops/app/activities/ManageFeedsActivity.java b/app/src/main/java/com/readrops/app/activities/ManageFeedsActivity.java index 1e60ddac..fff0c264 100644 --- a/app/src/main/java/com/readrops/app/activities/ManageFeedsActivity.java +++ b/app/src/main/java/com/readrops/app/activities/ManageFeedsActivity.java @@ -2,21 +2,40 @@ package com.readrops.app.activities; import android.arch.lifecycle.ViewModelProvider; import android.os.Bundle; +import android.support.annotation.Nullable; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; +import android.support.v7.util.DiffUtil; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.text.InputType; +import android.util.Log; +import android.view.Menu; import android.view.MenuItem; +import android.widget.Toast; +import com.afollestad.materialdialogs.MaterialDialog; import com.mikepenz.fastadapter.FastAdapter; -import com.mikepenz.fastadapter.IInterceptor; import com.mikepenz.fastadapter.adapters.ModelAdapter; +import com.mikepenz.fastadapter.commons.utils.DiffCallback; +import com.mikepenz.fastadapter.commons.utils.DiffCallbackImpl; +import com.mikepenz.fastadapter.commons.utils.FastAdapterDiffUtil; +import com.readrops.app.database.entities.Folder; import com.readrops.app.views.EditFeedDialog; import com.readrops.app.views.FeedWithFolderItem; import com.readrops.app.viewmodels.ManageFeedsViewModel; import com.readrops.app.R; import com.readrops.app.database.pojo.FeedWithFolder; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +import io.reactivex.Scheduler; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.observers.DisposableCompletableObserver; +import io.reactivex.schedulers.Schedulers; + public class ManageFeedsActivity extends AppCompatActivity { private RecyclerView recyclerView; @@ -50,13 +69,55 @@ public class ManageFeedsActivity extends AppCompatActivity { }); fastAdapter = FastAdapter.with(itemAdapter); - recyclerView.setAdapter(fastAdapter); viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()).create(ManageFeedsViewModel.class); viewModel.getFeedsWithFolder().observe(this, feedWithFolders -> { - itemAdapter.add(feedWithFolders); - fastAdapter.notifyAdapterDataSetChanged(); + List items = new ArrayList<>(); + for (FeedWithFolder feedWithFolder : feedWithFolders) { + + FeedWithFolderItem folderItem = new FeedWithFolderItem(feedWithFolder); + folderItem.setListener(new FeedWithFolderItem.ManageFeedsListener() { + @Override + public void onEdit(FeedWithFolder feedWithFolder) { + openEditFeedDialog(feedWithFolder); + } + + @Override + public void onDelete(FeedWithFolder feedWithFolder) { + + } + }); + + items.add(folderItem); + } + + if (items.size() > 0) { + DiffUtil.DiffResult diffResult = FastAdapterDiffUtil.calculateDiff(itemAdapter, items, new DiffCallback(){ + @Override + public boolean areItemsTheSame(FeedWithFolderItem oldItem, FeedWithFolderItem newItem) { + return oldItem.getModel().getFeed().getId() == newItem.getModel().getFeed().getId(); + } + + @Override + public boolean areContentsTheSame(FeedWithFolderItem oldItem, FeedWithFolderItem newItem) { + FeedWithFolder feedWithFolder1 = oldItem.getModel(); + FeedWithFolder feedWithFolder2 = newItem.getModel(); + + return feedWithFolder1.getFeed().getName().equals(feedWithFolder1.getFeed().getName()) && + feedWithFolder1.getFeed().getUrl().equals(feedWithFolder2.getFeed().getUrl()) && + feedWithFolder1.getFolder().getName().equals(feedWithFolder2.getFolder().getName()); + } + + @Nullable + @Override + public Object getChangePayload(FeedWithFolderItem oldItem, int oldItemPosition, FeedWithFolderItem newItem, int newItemPosition) { + return null; + } + }); + + FastAdapterDiffUtil.set(itemAdapter, diffResult); + } }); } @@ -71,16 +132,49 @@ public class ManageFeedsActivity extends AppCompatActivity { transaction.add(editFeedDialog, "").commit(); } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.feeds_menu, menu); + return super.onCreateOptionsMenu(menu); + } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; + case R.id.add_folder: + addFolder(); + return true; } return super.onOptionsItemSelected(item); } + private void addFolder() { + new MaterialDialog.Builder(this) + .title(R.string.add_folder) + .positiveText(R.string.validate) + .input(R.string.folder, 0, (dialog, input) -> { + Folder folder = new Folder(input.toString()); + viewModel.addFolder(folder) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new DisposableCompletableObserver() { + @Override + public void onComplete() { + Toast.makeText(getApplication(), "folder inserted", Toast.LENGTH_LONG).show(); + } + + @Override + public void onError(Throwable e) { + Toast.makeText(getApplication(), "error on folder insertion", Toast.LENGTH_LONG).show(); + } + }); + }) + .show(); + } + @Override public void onBackPressed() { finish(); diff --git a/app/src/main/java/com/readrops/app/repositories/ARepository.java b/app/src/main/java/com/readrops/app/repositories/ARepository.java index 86846bb2..3a441c80 100644 --- a/app/src/main/java/com/readrops/app/repositories/ARepository.java +++ b/app/src/main/java/com/readrops/app/repositories/ARepository.java @@ -42,14 +42,7 @@ public abstract class ARepository { public abstract void deleteFeed(Feed feed); - public Completable addFolder(String name) { - return Completable.create(emitter -> { - Folder folder = new Folder(name); - database.folderDao().insert(folder); - - emitter.onComplete(); - }); - } + public abstract Completable addFolder(Folder folder); public Completable deleteFolder(Folder folder) { return Completable.create(emitter -> { diff --git a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java index d7dc4d02..a3e19879 100644 --- a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java @@ -6,6 +6,7 @@ import android.graphics.Bitmap; import android.support.v7.graphics.Palette; import android.util.Patterns; +import com.readrops.app.database.entities.Folder; import com.readrops.app.database.pojo.FeedWithFolder; import com.readrops.app.database.pojo.ItemWithFeed; import com.readrops.app.database.entities.Feed; @@ -30,6 +31,8 @@ import java.util.HashMap; import java.util.List; import java.util.TreeMap; +import io.reactivex.Completable; + public class LocalFeedRepository extends ARepository implements QueryCallback { private static final String TAG = LocalFeedRepository.class.getSimpleName(); @@ -112,6 +115,14 @@ public class LocalFeedRepository extends ARepository implements QueryCallback { }); } + @Override + public Completable addFolder(Folder folder) { + return Completable.create(emitter -> { + database.folderDao().insert(folder); + emitter.onComplete(); + }); + } + @Override public void onSyncSuccess(AFeed feed, RSSNetwork.RSSType type) { switch (type) { diff --git a/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsViewModel.java index 958dd71d..e09df8c2 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/ManageFeedsViewModel.java @@ -12,6 +12,8 @@ import com.readrops.app.repositories.LocalFeedRepository; import java.util.List; +import io.reactivex.Completable; + public class ManageFeedsViewModel extends AndroidViewModel { private Database db; @@ -39,4 +41,8 @@ public class ManageFeedsViewModel extends AndroidViewModel { public LiveData> getFolders() { return folders; } + + public Completable addFolder(Folder folder) { + return repository.addFolder(folder); + } } diff --git a/app/src/main/res/drawable/ic_new_folder.xml b/app/src/main/res/drawable/ic_new_folder.xml new file mode 100644 index 00000000..43d025db --- /dev/null +++ b/app/src/main/res/drawable/ic_new_folder.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/menu/feeds_menu.xml b/app/src/main/res/menu/feeds_menu.xml new file mode 100644 index 00000000..a43b74bd --- /dev/null +++ b/app/src/main/res/menu/feeds_menu.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file