mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Add a My Blogs tab with option to add new blogs
Clicking the plus in the toolbar open the `CreateBlogActivity` which allows the user to create a new blog. Only the first identity is considered, but support for more identities can be easily added later. The actual list of blogs in the My Blogs tab will be done in the next commit.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.ActivityComponent;
|
||||
import org.briarproject.android.fragment.BaseFragment;
|
||||
|
||||
public class BlogListFragment extends BaseFragment {
|
||||
|
||||
public final static String TAG = BlogListFragment.class.getName();
|
||||
|
||||
static BlogListFragment newInstance(int num) {
|
||||
BlogListFragment f = new BlogListFragment();
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putInt("num", num);
|
||||
f.setArguments(args);
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
View v = inflater.inflate(R.layout.fragment_blogs_list, container,
|
||||
false);
|
||||
|
||||
TextView numView = (TextView) v.findViewById(R.id.num);
|
||||
String num = String.valueOf(getArguments().getInt("num"));
|
||||
numView.setText(num);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -78,7 +78,7 @@ public class BlogsFragment extends BaseFragment {
|
||||
}
|
||||
|
||||
|
||||
private static class TabAdapter extends FragmentStatePagerAdapter {
|
||||
private class TabAdapter extends FragmentStatePagerAdapter {
|
||||
private String[] titles;
|
||||
|
||||
TabAdapter(FragmentManager fm, String[] titles) {
|
||||
@@ -94,9 +94,10 @@ public class BlogsFragment extends BaseFragment {
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
switch (position) {
|
||||
// TODO add your fragments here
|
||||
case 1:
|
||||
return new MyBlogsFragment();
|
||||
default:
|
||||
return MyBlogsFragment.newInstance(position);
|
||||
return BlogListFragment.newInstance(position);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.design.widget.TextInputEditText;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.TextView.OnEditorActionListener;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.ActivityComponent;
|
||||
import org.briarproject.android.BriarActivity;
|
||||
import org.briarproject.api.blogs.Blog;
|
||||
import org.briarproject.api.blogs.BlogManager;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.api.blogs.BlogConstants.MAX_BLOG_DESC_LENGTH;
|
||||
import static org.briarproject.api.blogs.BlogConstants.MAX_BLOG_TITLE_LENGTH;
|
||||
|
||||
public class CreateBlogActivity extends BriarActivity
|
||||
implements OnEditorActionListener, OnClickListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(CreateBlogActivity.class.getName());
|
||||
|
||||
private TextInputEditText titleInput, descInput;
|
||||
private Button button;
|
||||
private ProgressBar progress;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject
|
||||
protected volatile IdentityManager identityManager;
|
||||
@Inject
|
||||
volatile BlogManager blogManager;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(state);
|
||||
|
||||
setContentView(R.layout.activity_create_blog);
|
||||
|
||||
TextInputLayout titleLayout =
|
||||
(TextInputLayout) findViewById(R.id.titleLayout);
|
||||
if (titleLayout != null) {
|
||||
titleLayout.setCounterMaxLength(MAX_BLOG_TITLE_LENGTH);
|
||||
}
|
||||
titleInput = (TextInputEditText) findViewById(R.id.titleInput);
|
||||
TextWatcher nameEntryWatcher = new TextWatcher() {
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
}
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count,
|
||||
int after) {
|
||||
}
|
||||
@Override
|
||||
public void onTextChanged(CharSequence text, int start,
|
||||
int lengthBefore, int lengthAfter) {
|
||||
enableOrDisableCreateButton();
|
||||
}
|
||||
};
|
||||
titleInput.setOnEditorActionListener(this);
|
||||
titleInput.addTextChangedListener(nameEntryWatcher);
|
||||
|
||||
TextInputLayout descLayout =
|
||||
(TextInputLayout) findViewById(R.id.descLayout);
|
||||
if (descLayout != null) {
|
||||
descLayout.setCounterMaxLength(MAX_BLOG_DESC_LENGTH);
|
||||
}
|
||||
descInput = (TextInputEditText) findViewById(R.id.descInput);
|
||||
if (descInput != null) {
|
||||
descInput.addTextChangedListener(nameEntryWatcher);
|
||||
}
|
||||
|
||||
button = (Button) findViewById(R.id.createBlogButton);
|
||||
if (button != null) {
|
||||
button.setOnClickListener(this);
|
||||
}
|
||||
|
||||
progress = (ProgressBar) findViewById(R.id.createBlogProgressBar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectActivity(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
private void enableOrDisableCreateButton() {
|
||||
if (progress == null) return; // Not created yet
|
||||
button.setEnabled(validateTitle() && validateDescription());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
|
||||
descInput.requestFocus();
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean validateTitle() {
|
||||
String name = titleInput.getText().toString();
|
||||
int length = StringUtils.toUtf8(name).length;
|
||||
return length <= MAX_BLOG_TITLE_LENGTH && length > 0;
|
||||
}
|
||||
|
||||
private boolean validateDescription() {
|
||||
String name = descInput.getText().toString();
|
||||
int length = StringUtils.toUtf8(name).length;
|
||||
return length <= MAX_BLOG_DESC_LENGTH && length > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (view == button) {
|
||||
hideSoftKeyboard(view);
|
||||
if (!validateTitle()) return;
|
||||
button.setVisibility(GONE);
|
||||
progress.setVisibility(VISIBLE);
|
||||
addBlog(titleInput.getText().toString(),
|
||||
descInput.getText().toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void addBlog(final String title, final String description) {
|
||||
runOnDbThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
Collection<LocalAuthor> authors =
|
||||
identityManager.getLocalAuthors();
|
||||
// take first identity, don't support more for now
|
||||
LocalAuthor author = authors.iterator().next();
|
||||
Blog f = blogManager.addBlog(author, title, description);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Storing blog took " + duration + " ms");
|
||||
displayBlog(f);
|
||||
} catch (DbException e) {
|
||||
// TODO show error, e.g. blog with same title exists
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
finishOnUiThread();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayBlog(final Blog b) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO
|
||||
/* Intent i = new Intent(CreateBlogActivity.this,
|
||||
BlogActivity.class);
|
||||
i.putExtra(GROUP_ID, b.getId().getBytes());
|
||||
i.putExtra(BLOG_NAME, b.getName());
|
||||
startActivity(i);
|
||||
*/ Toast.makeText(CreateBlogActivity.this,
|
||||
R.string.blogs_my_blogs_created, LENGTH_LONG).show();
|
||||
supportFinishAfterTransition();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.app.ActivityOptionsCompat;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
@@ -13,6 +19,8 @@ import org.briarproject.android.fragment.BaseFragment;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.support.v4.app.ActivityOptionsCompat.makeCustomAnimation;
|
||||
|
||||
public class MyBlogsFragment extends BaseFragment {
|
||||
|
||||
public final static String TAG = MyBlogsFragment.class.getName();
|
||||
@@ -21,31 +29,52 @@ public class MyBlogsFragment extends BaseFragment {
|
||||
public MyBlogsFragment() {
|
||||
}
|
||||
|
||||
static MyBlogsFragment newInstance(int num) {
|
||||
MyBlogsFragment f = new MyBlogsFragment();
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putInt("num", num);
|
||||
f.setArguments(args);
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
setHasOptionsMenu(true);
|
||||
View v = inflater.inflate(R.layout.fragment_blogs_my, container,
|
||||
false);
|
||||
|
||||
TextView numView = (TextView) v.findViewById(R.id.num);
|
||||
String num = String.valueOf(getArguments().getInt("num"));
|
||||
numView.setText(num);
|
||||
numView.setText("My Blogs");
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
listener.getActivityComponent().inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.blogs_my_actions, menu);
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(final MenuItem item) {
|
||||
// Handle presses on the action bar items
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_create_blog:
|
||||
Intent intent =
|
||||
new Intent(getContext(), CreateBlogActivity.class);
|
||||
ActivityOptionsCompat options =
|
||||
makeCustomAnimation(getActivity(),
|
||||
android.R.anim.slide_in_left,
|
||||
android.R.anim.slide_out_right);
|
||||
ActivityCompat.startActivity(getActivity(), intent,
|
||||
options.toBundle());
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
|
||||
Reference in New Issue
Block a user