Add feed update

This commit is contained in:
Shinokuni 2019-02-26 22:06:12 +00:00
parent 50ede215fb
commit 4c538af36e
9 changed files with 68 additions and 15 deletions

View File

@ -21,6 +21,7 @@ public class ManageFeedsActivity extends AppCompatActivity {
private RecyclerView recyclerView; private RecyclerView recyclerView;
private ModelAdapter<FeedWithFolder, FeedWithFolderItem> itemAdapter; private ModelAdapter<FeedWithFolder, FeedWithFolderItem> itemAdapter;
private FastAdapter fastAdapter;
private ManageFeedsViewModel viewModel; private ManageFeedsViewModel viewModel;
@Override @Override
@ -48,11 +49,15 @@ public class ManageFeedsActivity extends AppCompatActivity {
return folderItem; return folderItem;
}); });
FastAdapter fastAdapter = FastAdapter.with(itemAdapter); fastAdapter = FastAdapter.with(itemAdapter);
recyclerView.setAdapter(fastAdapter); recyclerView.setAdapter(fastAdapter);
viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()).create(ManageFeedsViewModel.class); viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()).create(ManageFeedsViewModel.class);
viewModel.getFeedsWithFolder().observe(this, feedWithFolders -> itemAdapter.add(feedWithFolders)); viewModel.getFeedsWithFolder().observe(this, feedWithFolders -> {
itemAdapter.add(feedWithFolders);
fastAdapter.notifyAdapterDataSetChanged();
});
} }
private void openEditFeedDialog(FeedWithFolder feedWithFolder) { private void openEditFeedDialog(FeedWithFolder feedWithFolder) {

View File

@ -21,6 +21,9 @@ public interface FeedDao {
@Insert @Insert
long insert(Feed feed); long insert(Feed feed);
@Update
void update(Feed feed);
@Query("Select count(*) from Feed") @Query("Select count(*) from Feed")
int getFeedCount(); int getFeedCount();
@ -33,8 +36,12 @@ public interface FeedDao {
@Query("Update Feed set folder_id = :folderId Where id = :feedId") @Query("Update Feed set folder_id = :folderId Where id = :feedId")
void updateFeedFolder(int feedId, int folderId); void updateFeedFolder(int feedId, int folderId);
@Query("Update Feed set name = :feedName, url = :feedUrl, folder_id = :folderId Where id = :feedId")
void updateFeedFields(int feedId, String feedName, String feedUrl, int folderId);
@Query("Select Feed.name as feed_name, Feed.id as feed_id, Folder.name as folder_name, Folder.id as folder_id," + @Query("Select Feed.name as feed_name, Feed.id as feed_id, Folder.name as folder_name, Folder.id as folder_id," +
"Feed.description as feed_description, Feed.icon_url as feed_icon_url, Feed.url as feed_url" + "Feed.description as feed_description, Feed.icon_url as feed_icon_url, Feed.url as feed_url, Feed.folder_id as feed_folder_id" +
" from Feed Inner Join Folder on Feed.folder_id = Folder.id Order by Feed.name") " from Feed Inner Join Folder on Feed.folder_id = Folder.id Order by Feed.name")
LiveData<List<FeedWithFolder>> getAllFeedsWithFolder(); LiveData<List<FeedWithFolder>> getAllFeedsWithFolder();
} }

View File

@ -4,6 +4,7 @@ import android.app.Application;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.views.SimpleCallback; import com.readrops.app.views.SimpleCallback;
import com.readrops.app.database.Database; import com.readrops.app.database.Database;
import com.readrops.app.database.entities.Feed; import com.readrops.app.database.entities.Feed;
@ -35,6 +36,10 @@ public abstract class ARepository {
public abstract void addFeed(ParsingResult result); public abstract void addFeed(ParsingResult result);
public abstract void updateFeed(Feed feed);
public abstract void updateFeedWithFolder(FeedWithFolder feedWithFolder);
public abstract void deleteFeed(Feed feed); public abstract void deleteFeed(Feed feed);
public Completable addFolder(String name) { public Completable addFolder(String name) {

View File

@ -6,6 +6,7 @@ import android.graphics.Bitmap;
import android.support.v7.graphics.Palette; import android.support.v7.graphics.Palette;
import android.util.Patterns; import android.util.Patterns;
import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.database.pojo.ItemWithFeed; import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.database.entities.Feed; import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Item; import com.readrops.app.database.entities.Item;
@ -85,6 +86,25 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
}); });
} }
@Override
public void updateFeed(Feed feed) {
executor.execute(() -> {
try {
database.feedDao().update(feed);
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
@Override
public void updateFeedWithFolder(FeedWithFolder feedWithFolder) {
executor.execute(() -> {
Feed feed = feedWithFolder.getFeed();
database.feedDao().updateFeedFields(feed.getId(), feed.getName(), feed.getUrl(), feed.getFolderId());
});
}
@Override @Override
public void deleteFeed(Feed feed) { public void deleteFeed(Feed feed) {
executor.execute(() -> { executor.execute(() -> {

View File

@ -1,13 +1,13 @@
package com.readrops.app.viewmodels; package com.readrops.app.viewmodels;
import android.app.Application; import android.app.Application;
import android.app.ListActivity;
import android.arch.lifecycle.AndroidViewModel; import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData; import android.arch.lifecycle.LiveData;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.readrops.app.database.Database; import com.readrops.app.database.Database;
import com.readrops.app.database.pojo.FeedWithFolder; import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.repositories.LocalFeedRepository;
import java.util.List; import java.util.List;
@ -15,10 +15,12 @@ public class ManageFeedsViewModel extends AndroidViewModel {
private Database db; private Database db;
private LiveData<List<FeedWithFolder>> feedsWithFolder; private LiveData<List<FeedWithFolder>> feedsWithFolder;
private LocalFeedRepository repository;
public ManageFeedsViewModel(@NonNull Application application) { public ManageFeedsViewModel(@NonNull Application application) {
super(application); super(application);
db = Database.getInstance(application); db = Database.getInstance(application);
repository = new LocalFeedRepository(application);
feedsWithFolder = db.feedDao().getAllFeedsWithFolder(); feedsWithFolder = db.feedDao().getAllFeedsWithFolder();
} }
@ -26,4 +28,8 @@ public class ManageFeedsViewModel extends AndroidViewModel {
public LiveData<List<FeedWithFolder>> getFeedsWithFolder() { public LiveData<List<FeedWithFolder>> getFeedsWithFolder() {
return feedsWithFolder; return feedsWithFolder;
} }
public void updateFeedWithFolder(FeedWithFolder feedWithFolder) {
repository.updateFeedWithFolder(feedWithFolder);
}
} }

View File

@ -1,19 +1,14 @@
package com.readrops.app.views; package com.readrops.app.views;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders; import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText; import android.support.design.widget.TextInputEditText;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.Spinner; import android.widget.Spinner;
import com.readrops.app.R; import com.readrops.app.R;
@ -26,7 +21,6 @@ public class EditFeedDialog extends DialogFragment {
private TextInputEditText feedName; private TextInputEditText feedName;
private TextInputEditText feedUrl; private TextInputEditText feedUrl;
private Spinner folder; private Spinner folder;
private Button validate;
private FeedWithFolder feedWithFolder; private FeedWithFolder feedWithFolder;
private ManageFeedsViewModel viewModel; private ManageFeedsViewModel viewModel;
@ -35,7 +29,6 @@ public class EditFeedDialog extends DialogFragment {
@Override @Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
viewModel = ViewModelProviders.of(getActivity()).get(ManageFeedsViewModel.class); viewModel = ViewModelProviders.of(getActivity()).get(ManageFeedsViewModel.class);
feedWithFolder = getArguments().getParcelable("feedWithFolder"); feedWithFolder = getArguments().getParcelable("feedWithFolder");
View v = getActivity().getLayoutInflater().inflate(R.layout.edit_feed_layout, null); View v = getActivity().getLayoutInflater().inflate(R.layout.edit_feed_layout, null);
@ -43,7 +36,11 @@ public class EditFeedDialog extends DialogFragment {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.edit_feed)) .setTitle(getString(R.string.edit_feed))
.setPositiveButton(getString(R.string.validate), (dialog, which) -> { .setPositiveButton(getString(R.string.validate), (dialog, which) -> {
Feed feed = feedWithFolder.getFeed();
feed.setName(feedName.getText().toString().trim());
feed.setUrl(feedUrl.getText().toString().trim());
viewModel.updateFeedWithFolder(feedWithFolder);
}); });
builder.setView(v); builder.setView(v);

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="18dp"> android:padding="18dp">
@ -42,13 +43,23 @@
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
<TextView
android:id="@+id/edit_feed_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_feed_url_textinputlayout"
android:text="@string/folder"
android:textAppearance="@style/TextAppearance.Design.Hint"/>
<Spinner <Spinner
android:id="@+id/edit_feed_folder_spinner" android:id="@+id/edit_feed_folder_spinner"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.514"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_feed_url_textinputlayout" /> app:layout_constraintTop_toBottomOf="@+id/edit_feed_folder" />
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>

View File

@ -26,5 +26,6 @@
<string name="feed_folder">Dossier du flux</string> <string name="feed_folder">Dossier du flux</string>
<string name="feed_name">Nom du flux</string> <string name="feed_name">Nom du flux</string>
<string name="edit_feed">Modifier le flux</string> <string name="edit_feed">Modifier le flux</string>
<string name="folder">dossier</string>
</resources> </resources>

View File

@ -28,4 +28,5 @@
<string name="feed_folder">Feed folder</string> <string name="feed_folder">Feed folder</string>
<string name="feed_name">Feed name</string> <string name="feed_name">Feed name</string>
<string name="edit_feed">Edit feed</string> <string name="edit_feed">Edit feed</string>
<string name="folder">Folder</string>
</resources> </resources>