mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 06:09:55 +01:00
@@ -21,9 +21,7 @@ public interface BlogController extends BaseController {
|
|||||||
void loadBlogPost(MessageId m,
|
void loadBlogPost(MessageId m,
|
||||||
ResultExceptionHandler<BlogPostItem, DbException> handler);
|
ResultExceptionHandler<BlogPostItem, DbException> handler);
|
||||||
|
|
||||||
void isMyBlog(ResultExceptionHandler<Boolean, DbException> handler);
|
void loadBlog(ResultExceptionHandler<BlogItem, DbException> handler);
|
||||||
|
|
||||||
void canDeleteBlog(ResultExceptionHandler<Boolean, DbException> handler);
|
|
||||||
|
|
||||||
void deleteBlog(ResultExceptionHandler<Void, DbException> handler);
|
void deleteBlog(ResultExceptionHandler<Void, DbException> handler);
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.briarproject.android.blogs;
|
|||||||
import org.briarproject.android.controller.ActivityLifecycleController;
|
import org.briarproject.android.controller.ActivityLifecycleController;
|
||||||
import org.briarproject.android.controller.handler.ResultExceptionHandler;
|
import org.briarproject.android.controller.handler.ResultExceptionHandler;
|
||||||
import org.briarproject.api.blogs.Blog;
|
import org.briarproject.api.blogs.Blog;
|
||||||
|
import org.briarproject.api.blogs.BlogPostHeader;
|
||||||
import org.briarproject.api.db.DbException;
|
import org.briarproject.api.db.DbException;
|
||||||
import org.briarproject.api.event.BlogPostAddedEvent;
|
import org.briarproject.api.event.BlogPostAddedEvent;
|
||||||
import org.briarproject.api.event.Event;
|
import org.briarproject.api.event.Event;
|
||||||
@@ -13,6 +14,7 @@ import org.briarproject.api.sync.GroupId;
|
|||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -102,8 +104,8 @@ public class BlogControllerImpl extends BaseControllerImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void isMyBlog(
|
public void loadBlog(
|
||||||
final ResultExceptionHandler<Boolean, DbException> handler) {
|
final ResultExceptionHandler<BlogItem, DbException> handler) {
|
||||||
if (groupId == null) throw new IllegalStateException();
|
if (groupId == null) throw new IllegalStateException();
|
||||||
runOnDbThread(new Runnable() {
|
runOnDbThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@@ -111,7 +113,12 @@ public class BlogControllerImpl extends BaseControllerImpl
|
|||||||
try {
|
try {
|
||||||
LocalAuthor a = identityManager.getLocalAuthor();
|
LocalAuthor a = identityManager.getLocalAuthor();
|
||||||
Blog b = blogManager.getBlog(groupId);
|
Blog b = blogManager.getBlog(groupId);
|
||||||
handler.onResult(b.getAuthor().getId().equals(a.getId()));
|
boolean ours = a.getId().equals(b.getAuthor().getId());
|
||||||
|
boolean removable = blogManager.canBeRemoved(groupId);
|
||||||
|
BlogItem blog = new BlogItem(b,
|
||||||
|
Collections.<BlogPostHeader>emptyList(),
|
||||||
|
ours, removable);
|
||||||
|
handler.onResult(blog);
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
if (LOG.isLoggable(WARNING))
|
if (LOG.isLoggable(WARNING))
|
||||||
LOG.log(WARNING, e.toString(), e);
|
LOG.log(WARNING, e.toString(), e);
|
||||||
@@ -122,24 +129,6 @@ public class BlogControllerImpl extends BaseControllerImpl
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void canDeleteBlog(
|
|
||||||
final ResultExceptionHandler<Boolean, DbException> handler) {
|
|
||||||
if (groupId == null) throw new IllegalStateException();
|
|
||||||
runOnDbThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
handler.onResult(blogManager.canBeRemoved(groupId));
|
|
||||||
} catch (DbException e) {
|
|
||||||
if (LOG.isLoggable(WARNING))
|
|
||||||
LOG.log(WARNING, e.toString(), e);
|
|
||||||
handler.onException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteBlog(
|
public void deleteBlog(
|
||||||
final ResultExceptionHandler<Void, DbException> handler) {
|
final ResultExceptionHandler<Void, DbException> handler) {
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import org.briarproject.android.sharing.SharingStatusBlogActivity;
|
|||||||
import org.briarproject.android.util.BriarRecyclerView;
|
import org.briarproject.android.util.BriarRecyclerView;
|
||||||
import org.briarproject.api.blogs.BlogPostHeader;
|
import org.briarproject.api.blogs.BlogPostHeader;
|
||||||
import org.briarproject.api.db.DbException;
|
import org.briarproject.api.db.DbException;
|
||||||
|
import org.briarproject.api.identity.Author;
|
||||||
import org.briarproject.api.sync.GroupId;
|
import org.briarproject.api.sync.GroupId;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -121,8 +122,7 @@ public class BlogFragment extends BaseFragment implements
|
|||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
checkIfThisIsMyBlog();
|
loadBlog();
|
||||||
checkIfBlogCanBeDeleted();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -252,15 +252,17 @@ public class BlogFragment extends BaseFragment implements
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkIfThisIsMyBlog() {
|
private void loadBlog() {
|
||||||
blogController.isMyBlog(
|
blogController.loadBlog(
|
||||||
new UiResultExceptionHandler<Boolean, DbException>(
|
new UiResultExceptionHandler<BlogItem, DbException>(
|
||||||
getActivity()) {
|
getActivity()) {
|
||||||
@Override
|
@Override
|
||||||
public void onResultUi(Boolean isMyBlog) {
|
public void onResultUi(BlogItem blog) {
|
||||||
if (isMyBlog) {
|
setToolbarTitle(blog.getBlog().getAuthor());
|
||||||
|
if (blog.isOurs())
|
||||||
showWriteButton();
|
showWriteButton();
|
||||||
}
|
if (blog.canBeRemoved())
|
||||||
|
showDeleteButton();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -271,23 +273,13 @@ public class BlogFragment extends BaseFragment implements
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkIfBlogCanBeDeleted() {
|
private void setToolbarTitle(Author a) {
|
||||||
blogController.canDeleteBlog(
|
String title = getString(R.string.blogs_personal_blog, a.getName());
|
||||||
new UiResultExceptionHandler<Boolean, DbException>(
|
getActivity().setTitle(title);
|
||||||
getActivity()) {
|
|
||||||
@Override
|
|
||||||
public void onResultUi(Boolean canBeDeleted) {
|
|
||||||
if (canBeDeleted) {
|
|
||||||
showDeleteButton();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
// safe title in intent, so it can be restored automatically
|
||||||
public void onExceptionUi(DbException exception) {
|
Intent intent = getActivity().getIntent();
|
||||||
// TODO: Decide how to handle errors in the UI
|
intent.putExtra(BLOG_NAME, title);
|
||||||
finish();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showWriteButton() {
|
private void showWriteButton() {
|
||||||
|
|||||||
@@ -5,15 +5,16 @@ import org.briarproject.api.blogs.BlogPostHeader;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
class BlogListItem {
|
class BlogItem {
|
||||||
|
|
||||||
private final Blog blog;
|
private final Blog blog;
|
||||||
private final int postCount;
|
private final int postCount;
|
||||||
private final long timestamp;
|
private final long timestamp;
|
||||||
private final int unread;
|
private final int unread;
|
||||||
private final boolean ours;
|
private final boolean ours, removable;
|
||||||
|
|
||||||
BlogListItem(Blog blog, Collection<BlogPostHeader> headers, boolean ours) {
|
BlogItem(Blog blog, Collection<BlogPostHeader> headers, boolean ours,
|
||||||
|
boolean removable) {
|
||||||
this.blog = blog;
|
this.blog = blog;
|
||||||
if (headers.isEmpty()) {
|
if (headers.isEmpty()) {
|
||||||
postCount = 0;
|
postCount = 0;
|
||||||
@@ -35,6 +36,7 @@ class BlogListItem {
|
|||||||
this.unread = unread;
|
this.unread = unread;
|
||||||
}
|
}
|
||||||
this.ours = ours;
|
this.ours = ours;
|
||||||
|
this.removable = removable;
|
||||||
}
|
}
|
||||||
|
|
||||||
Blog getBlog() {
|
Blog getBlog() {
|
||||||
@@ -64,4 +66,8 @@ class BlogListItem {
|
|||||||
boolean isOurs() {
|
boolean isOurs() {
|
||||||
return ours;
|
return ours;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean canBeRemoved() {
|
||||||
|
return removable;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -29,11 +29,11 @@ import static org.briarproject.android.blogs.BlogActivity.BLOG_NAME;
|
|||||||
class BlogListAdapter extends
|
class BlogListAdapter extends
|
||||||
RecyclerView.Adapter<BlogListAdapter.BlogViewHolder> {
|
RecyclerView.Adapter<BlogListAdapter.BlogViewHolder> {
|
||||||
|
|
||||||
private SortedList<BlogListItem> blogs = new SortedList<>(
|
private SortedList<BlogItem> blogs = new SortedList<>(
|
||||||
BlogListItem.class, new SortedList.Callback<BlogListItem>() {
|
BlogItem.class, new SortedList.Callback<BlogItem>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(BlogListItem a, BlogListItem b) {
|
public int compare(BlogItem a, BlogItem b) {
|
||||||
if (a == b) return 0;
|
if (a == b) return 0;
|
||||||
// The blog with the newest message comes first
|
// The blog with the newest message comes first
|
||||||
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
|
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
|
||||||
@@ -66,14 +66,14 @@ class BlogListAdapter extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean areContentsTheSame(BlogListItem a, BlogListItem b) {
|
public boolean areContentsTheSame(BlogItem a, BlogItem b) {
|
||||||
return a.getBlog().equals(b.getBlog()) &&
|
return a.getBlog().equals(b.getBlog()) &&
|
||||||
a.getTimestamp() == b.getTimestamp() &&
|
a.getTimestamp() == b.getTimestamp() &&
|
||||||
a.getUnreadCount() == b.getUnreadCount();
|
a.getUnreadCount() == b.getUnreadCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean areItemsTheSame(BlogListItem a, BlogListItem b) {
|
public boolean areItemsTheSame(BlogItem a, BlogItem b) {
|
||||||
return a.getBlog().equals(b.getBlog());
|
return a.getBlog().equals(b.getBlog());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -93,7 +93,7 @@ class BlogListAdapter extends
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(BlogViewHolder ui, int position) {
|
public void onBindViewHolder(BlogViewHolder ui, int position) {
|
||||||
final BlogListItem item = getItem(position);
|
final BlogItem item = getItem(position);
|
||||||
|
|
||||||
// Avatar
|
// Avatar
|
||||||
ui.avatar.setText(item.getName().substring(0, 1));
|
ui.avatar.setText(item.getName().substring(0, 1));
|
||||||
@@ -145,14 +145,14 @@ class BlogListAdapter extends
|
|||||||
return blogs.size();
|
return blogs.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public BlogListItem getItem(int position) {
|
public BlogItem getItem(int position) {
|
||||||
return blogs.get(position);
|
return blogs.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public BlogListItem getItem(GroupId g) {
|
public BlogItem getItem(GroupId g) {
|
||||||
for (int i = 0; i < blogs.size(); i++) {
|
for (int i = 0; i < blogs.size(); i++) {
|
||||||
BlogListItem item = blogs.get(i);
|
BlogItem item = blogs.get(i);
|
||||||
if (item.getBlog().getGroup().getId().equals(g)) {
|
if (item.getBlog().getGroup().getId().equals(g)) {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
@@ -160,17 +160,17 @@ class BlogListAdapter extends
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAll(Collection<BlogListItem> items) {
|
public void addAll(Collection<BlogItem> items) {
|
||||||
blogs.addAll(items);
|
blogs.addAll(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateItem(BlogListItem item) {
|
void updateItem(BlogItem item) {
|
||||||
BlogListItem oldItem = getItem(item.getBlog().getGroup().getId());
|
BlogItem oldItem = getItem(item.getBlog().getGroup().getId());
|
||||||
int position = blogs.indexOf(oldItem);
|
int position = blogs.indexOf(oldItem);
|
||||||
blogs.updateItemAt(position, item);
|
blogs.updateItemAt(position, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(BlogListItem item) {
|
public void remove(BlogItem item) {
|
||||||
blogs.remove(item);
|
blogs.remove(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user