Add bookmarking as activity

This commit is contained in:
Peter Stuifzand 2018-03-22 22:41:05 +01:00
parent 1d92436a3c
commit 1577bbe308
8 changed files with 433 additions and 10 deletions

View File

@ -34,13 +34,16 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="wrimini"></data>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="wrimini" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
@ -71,7 +74,15 @@
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<activity
android:name=".BookmarkActivity"
android:label="@string/bookmark">
<intent-filter android:label="@string/bookmark">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
</manifest>

View File

@ -0,0 +1,179 @@
package eu.stuifzand.micropub;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import eu.stuifzand.micropub.client.Client;
import eu.stuifzand.micropub.client.Post;
import eu.stuifzand.micropub.databinding.ActivityBookmarkBinding;
import eu.stuifzand.micropub.databinding.ContentBookmarkBinding;
import okhttp3.HttpUrl;
public class BookmarkActivity extends AppCompatActivity {
private AccountManager accountManager;
private PostViewModel postModel;
private Client client;
private Account selectedAccount;
private String authToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
accountManager = AccountManager.get(this);
AccountManager am = AccountManager.get(this);
Bundle options = new Bundle();
postModel = ViewModelProviders.of(BookmarkActivity.this).get(PostViewModel.class);
client = ViewModelProviders.of(BookmarkActivity.this).get(Client.class);
TokenReady callback = (accountType, accountName, token) -> {
Account[] accounts = accountManager.getAccountsByType(accountType);
if (accounts.length == 0)
return;
selectedAccount = accounts[0];
authToken = token;
String micropubBackend = accountManager.getUserData(selectedAccount, "micropub");
if (micropubBackend == null) return;
client.setToken(accountType, accountName, token);
client.loadConfig(HttpUrl.parse(micropubBackend));
final View coordinator = findViewById(R.id.coordinator);
client.getResponse().observe(BookmarkActivity.this, response -> {
Log.i("micropub", "response received " + response.isSuccess());
if (response.isSuccess()) {
postModel.clear();
Snackbar.make(coordinator, R.string.post_successful, Snackbar.LENGTH_LONG)
.setAction("Open", v -> {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(response.getUrl()));
startActivity(browserIntent);
})
.show();
} else {
Snackbar.make(coordinator, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
}
});
client.getMediaResponse().observe(BookmarkActivity.this, response -> {
Log.i("micropub", "media response received " + response.isSuccess());
if (response.isSuccess()) {
postModel.setPhoto(response.getUrl());
Toast.makeText(BookmarkActivity.this, "Photo upload succesful, photo url filled", Toast.LENGTH_SHORT).show();
}
});
};
AuthError onError = (msg) -> {
BookmarkActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(BookmarkActivity.this, msg, Toast.LENGTH_LONG).show();
}
});
};
accountManager.getAuthTokenByFeatures(
"Indieauth",
"token",
null,
this,
options,
null,
new OnTokenAcquired(this, callback, onError),
null
);
// setContentView(R.layout.activity_main);
ActivityBookmarkBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_bookmark);
ContentBookmarkBinding contentBinding = DataBindingUtil.setContentView(this, R.layout.content_bookmark);
binding.setViewModel(postModel);
binding.setClient(client);
contentBinding.setViewModel(postModel);
contentBinding.setClient(client);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Intent intent = getIntent();
if (intent != null) {
Log.i("micropub", intent.toString());
String urlOrNote = intent.getStringExtra(Intent.EXTRA_TEXT);
if (urlOrNote != null) {
HttpUrl url = HttpUrl.parse(urlOrNote);
if (url != null) {
postModel.bookmarkOf.set(urlOrNote);
} else {
postModel.findBookmarkOf(urlOrNote);
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_like, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_send) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (this.getCurrentFocus() != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
inputManager.hideSoftInputFromInputMethod(this.getCurrentFocus().getWindowToken(), 0);
}
sendPost(null);
}
return super.onOptionsItemSelected(item);
}
public void sendPost(View view) {
AccountManager am = AccountManager.get(this);
Bundle options = new Bundle();
TokenReady callback = (accountType, accountName, token) -> {
String micropubBackend = accountManager.getUserData(selectedAccount, "micropub");
if (micropubBackend == null) {
Log.i("micropub", "micropub backend == null");
return;
}
Log.i("micropub", "Sending message to " + micropubBackend);
Post post = postModel.getPost();
client.createPost(post, token, HttpUrl.parse(micropubBackend));
};
AuthError onError = (msg) -> {
BookmarkActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(BookmarkActivity.this, msg, Toast.LENGTH_LONG).show();
}
});
};
accountManager.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(this, callback, onError), null);
}
}

View File

@ -27,6 +27,7 @@ public class PostViewModel extends ViewModel {
public final ObservableField<String> inReplyTo = new ObservableField<>();
public final ObservableField<String> photo = new ObservableField<>();
public final ObservableField<String> likeOf = new ObservableField<>();
public final ObservableField<String> bookmarkOf = new ObservableField<>();
public PostViewModel() {
this.name.set("");
@ -35,6 +36,7 @@ public class PostViewModel extends ViewModel {
this.inReplyTo.set("");
this.photo.set("");
this.likeOf.set("");
this.bookmarkOf.set("");
}
public void clear() {
@ -44,6 +46,7 @@ public class PostViewModel extends ViewModel {
this.inReplyTo.set("");
this.photo.set("");
this.likeOf.set("");
this.bookmarkOf.set("");
}
public void findReplyTo(String urlOrNote) {
@ -65,16 +68,28 @@ public class PostViewModel extends ViewModel {
}
}
public void findBookmarkOf(String urlOrNote) {
Matcher matcher = urlPattern.matcher(urlOrNote);
if (matcher.find()) {
bookmarkOf.set(matcher.group(1));
}
}
public void setPhoto(String url) {
this.photo.set(url);
}
public Post getPost() {
Post post = new Post(null, content.get(), category.get(), HttpUrl.parse(inReplyTo.get()));
Post post = new Post(name.get(), content.get(), category.get(), HttpUrl.parse(inReplyTo.get()));
if (!this.photo.get().equals("")) {
post.setPhoto(this.photo.get());
}
post.setLikeOf(HttpUrl.parse(likeOf.get()));
if (!this.likeOf.get().equals("")) {
post.setLikeOf(HttpUrl.parse(likeOf.get()));
}
if (!this.bookmarkOf.get().equals("")) {
post.setBookmarkOf(HttpUrl.parse(bookmarkOf.get()));
}
return post;
}
}

View File

@ -10,6 +10,7 @@ public class Post {
private String[] syndicationUids;
private String photo;
private HttpUrl likeOf;
private HttpUrl bookmarkOf;
public Post(String content) {
if (content.equals("")) {
@ -102,4 +103,14 @@ public class Post {
public boolean hasContent() {
return content != null;
}
public void setBookmarkOf(HttpUrl bookmarkOf) {
this.bookmarkOf = bookmarkOf;
}
public boolean hasBookmarkOf() {
return bookmarkOf!=null;
}
public String getBookmarkOf() {
return bookmarkOf.toString();
}
}

View File

@ -45,7 +45,7 @@ class PostTask extends AsyncTask<String, Void, Void> {
}
if (post.hasInReplyTo()) {
builder.add("in-reply-to", post.getInReplyTo());
builder.add("in-reply-to[]", post.getInReplyTo());
}
if (post.hasName()) {
@ -53,13 +53,16 @@ class PostTask extends AsyncTask<String, Void, Void> {
}
if (post.hasPhoto()) {
builder.add("photo", post.getPhoto());
builder.add("photo[]", post.getPhoto());
}
if (post.hasLikeOf()) {
builder.add("like-of", post.getLikeOf());
builder.add("like-of[]", post.getLikeOf());
}
if (post.hasBookmarkOf()) {
builder.add("bookmark-of[]", post.getBookmarkOf());
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="eu.stuifzand.micropub.BookmarkActivity">
<data>
<variable
name="viewModel"
type="eu.stuifzand.micropub.PostViewModel" />
<variable
name="client"
type="eu.stuifzand.micropub.client.Client" />
</data>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_bookmark" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
</layout>

View File

@ -0,0 +1,158 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="eu.stuifzand.micropub.PostViewModel" />
<variable
name="client"
type="eu.stuifzand.micropub.client.Client" />
</data>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="eu.stuifzand.micropub.BookmarkActivity"
tools:showIn="@layout/activity_bookmark">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="@+id/editBookmarkOfTextLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/contentLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/editBookmarkOfText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Bookmark of"
android:singleLine="true"
android:text="@={viewModel.bookmarkOf}" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/content"
app:counterEnabled="true"
app:layout_constraintBottom_toTopOf="@+id/editNameLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/editBookmarkOfTextLayout">
<android.support.design.widget.TextInputEditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="100sp"
android:ems="10"
android:gravity="top"
android:imeOptions="actionNext"
android:inputType="textMultiLine|textCapSentences|textAutoComplete"
android:lines="5"
android:nextFocusForward="@id/editCategory"
android:scrollbars="vertical"
android:text="@={viewModel.content}"
tools:layout_editor_absoluteY="101dp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/editNameLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/name"
app:counterEnabled="false"
app:layout_constraintBottom_toTopOf="@+id/listLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/contentLayout">
<android.support.design.widget.TextInputEditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="56sp"
android:ems="10"
android:imeOptions="actionNext"
android:inputType="textAutoComplete|textUri"
android:lines="1"
android:maxLines="1"
android:nextFocusForward="@id/content"
android:singleLine="true"
android:text="@={viewModel.name}"
app:layout_constraintTop_toBottomOf="@+id/editInReplyToLayout" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/editCategoryLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/categories"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/listLayout">
<android.support.design.widget.TextInputEditText
android:id="@+id/editCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:imeOptions="actionNext"
android:inputType="text"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:text="@={viewModel.category}" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="@+id/listLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="top|fill"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/editCategoryLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editNameLayout">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="@string/syndication"
android:textSize="16sp" />
<ListView
android:id="@+id/listSyndication"
android:layout_width="match_parent"
android:layout_height="69dp"
android:isScrollContainer="false"
app:list="@{client.syndicates}" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
</layout>

View File

@ -17,4 +17,5 @@
<string name="action_send">Send</string>
<string name="like">Like</string>
<string name="bookmark">Bookmark</string>
<string name="title_activity_bookmark">BookmarkActivity</string>
</resources>