Use data binding for main content view
This commit is contained in:
parent
3737d28ba9
commit
40091c4e20
|
@ -24,7 +24,7 @@
|
||||||
</value>
|
</value>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
|
|
@ -16,6 +16,13 @@ android {
|
||||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
compileOptions {
|
||||||
|
targetCompatibility 1.8
|
||||||
|
sourceCompatibility 1.8
|
||||||
|
}
|
||||||
|
dataBinding {
|
||||||
|
enabled = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
@ -24,8 +31,17 @@ dependencies {
|
||||||
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
|
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
|
||||||
implementation 'com.android.support:design:26.1.0'
|
implementation 'com.android.support:design:26.1.0'
|
||||||
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
|
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
|
||||||
|
|
||||||
|
// ViewModel and LiveData
|
||||||
|
implementation "android.arch.lifecycle:extensions:1.1.0"
|
||||||
|
annotationProcessor "android.arch.lifecycle:compiler:1.1.0"
|
||||||
|
|
||||||
|
// Java8 support for Lifecycles
|
||||||
|
implementation "android.arch.lifecycle:common-java8:1.1.0"
|
||||||
|
|
||||||
compile 'org.jsoup:jsoup:1.10.1'
|
compile 'org.jsoup:jsoup:1.10.1'
|
||||||
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
|
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
|
||||||
|
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
androidTestImplementation 'com.android.support.test:runner:1.0.1'
|
androidTestImplementation 'com.android.support.test:runner:1.0.1'
|
||||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
|
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
|
||||||
|
|
|
@ -6,44 +6,46 @@ import android.accounts.AccountManagerCallback;
|
||||||
import android.accounts.AccountManagerFuture;
|
import android.accounts.AccountManagerFuture;
|
||||||
import android.accounts.AuthenticatorException;
|
import android.accounts.AuthenticatorException;
|
||||||
import android.accounts.OperationCanceledException;
|
import android.accounts.OperationCanceledException;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.arch.lifecycle.ViewModelProviders;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.databinding.DataBindingUtil;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.design.widget.FloatingActionButton;
|
|
||||||
import android.support.design.widget.Snackbar;
|
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.widget.EditText;
|
import android.view.View;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import okhttp3.Call;
|
import eu.stuifzand.micropub.databinding.ActivityMainBinding;
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
// setContentView(R.layout.activity_main);
|
||||||
|
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
|
||||||
|
PostViewModel model = ViewModelProviders.of(MainActivity.this).get(PostViewModel.class);
|
||||||
|
binding.setViewModel(model);
|
||||||
|
|
||||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
|
|
||||||
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
|
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
|
||||||
fab.setOnClickListener(new View.OnClickListener() {
|
// fab.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
// @Override
|
||||||
public void onClick(View view) {
|
// public void onClick(View view) {
|
||||||
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
|
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
|
||||||
.setAction("Action", null).show();
|
// .setAction("Action", null).show();
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -76,8 +78,6 @@ public class MainActivity extends AppCompatActivity {
|
||||||
AccountManager am = AccountManager.get(this);
|
AccountManager am = AccountManager.get(this);
|
||||||
Bundle options = new Bundle();
|
Bundle options = new Bundle();
|
||||||
am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(true), null);
|
am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(true), null);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
|
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
|
||||||
|
@ -103,19 +103,23 @@ public class MainActivity extends AppCompatActivity {
|
||||||
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
|
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
|
||||||
|
|
||||||
if (sendMessage) {
|
if (sendMessage) {
|
||||||
EditText mEdit = (EditText) findViewById(R.id.content);
|
PostViewModel model = ViewModelProviders.of(MainActivity.this).get(PostViewModel.class);
|
||||||
AccountManager am = AccountManager.get(MainActivity.this);
|
AccountManager am = AccountManager.get(MainActivity.this);
|
||||||
Account[] accounts = am.getAccountsByType(bundle.getString("accountType"));
|
Account[] accounts = am.getAccountsByType(bundle.getString("accountType"));
|
||||||
String accountName = bundle.getString("authAccount");
|
String accountName = bundle.getString("authAccount");
|
||||||
String micropub = null;
|
|
||||||
|
String micropubBackend = null;
|
||||||
for (Account account : accounts) {
|
for (Account account : accounts) {
|
||||||
if (account.name.equals(accountName)) {
|
if (account.name.equals(accountName)) {
|
||||||
micropub = am.getUserData(account, "micropub");
|
micropubBackend = am.getUserData(account, "micropub");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String micropubBackend = micropub;
|
|
||||||
|
if (micropubBackend != null) {
|
||||||
Log.i("micropub", "Sending message to " + micropubBackend);
|
Log.i("micropub", "Sending message to " + micropubBackend);
|
||||||
new PostMessageTask(MainActivity.this, token, mEdit).execute(micropubBackend, mEdit.getText().toString());
|
new PostMessageTask(MainActivity.this, token, model, micropubBackend)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d("micropub", "GetTokenForAccount Bundle is " + token);
|
Log.d("micropub", "GetTokenForAccount Bundle is " + token);
|
||||||
|
|
23
app/src/main/java/eu/stuifzand/micropub/Post.java
Normal file
23
app/src/main/java/eu/stuifzand/micropub/Post.java
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package eu.stuifzand.micropub;
|
||||||
|
|
||||||
|
import okhttp3.FormBody;
|
||||||
|
|
||||||
|
public class Post {
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
public Post(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void applyToFormBodyBuilder(FormBody.Builder builder) {
|
||||||
|
builder.add("content", content);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,8 +3,6 @@ package eu.stuifzand.micropub;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
|
||||||
import android.widget.EditText;
|
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -19,25 +17,28 @@ import okhttp3.Response;
|
||||||
|
|
||||||
public class PostMessageTask extends AsyncTask<String, Void, String> {
|
public class PostMessageTask extends AsyncTask<String, Void, String> {
|
||||||
private final String accessToken;
|
private final String accessToken;
|
||||||
private EditText mEdit;
|
private PostViewModel postModel;
|
||||||
|
private String micropubBackend;
|
||||||
private WeakReference<Context> context;
|
private WeakReference<Context> context;
|
||||||
|
|
||||||
public PostMessageTask(Context context, String accessToken, EditText mEdit) {
|
public PostMessageTask(Context context, String accessToken, PostViewModel postModel, String micropubBackend) {
|
||||||
this.context = new WeakReference<Context>(context);
|
this.context = new WeakReference<Context>(context);
|
||||||
this.accessToken = accessToken;
|
this.accessToken = accessToken;
|
||||||
this.mEdit = mEdit;
|
assert postModel != null;
|
||||||
|
this.postModel = postModel;
|
||||||
|
this.micropubBackend = micropubBackend;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String doInBackground(String... strings) {
|
protected String doInBackground(String... strings) {
|
||||||
RequestBody formBody = new FormBody.Builder()
|
FormBody.Builder builder = new FormBody.Builder();
|
||||||
.add("content", strings[1])
|
builder.add("content", postModel.content.get());
|
||||||
.build();
|
RequestBody formBody = builder.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.addHeader("Authorization", "Bearer " + accessToken)
|
.addHeader("Authorization", "Bearer " + accessToken)
|
||||||
.method("POST", formBody)
|
.method("POST", formBody)
|
||||||
.url(strings[0])
|
.url(micropubBackend)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
@ -59,7 +60,7 @@ public class PostMessageTask extends AsyncTask<String, Void, String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onPostExecute(String message) {
|
protected void onPostExecute(String message) {
|
||||||
mEdit.setText("");
|
|
||||||
Toast.makeText(context.get(), message, Toast.LENGTH_SHORT).show();
|
Toast.makeText(context.get(), message, Toast.LENGTH_SHORT).show();
|
||||||
|
postModel.content.set("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
13
app/src/main/java/eu/stuifzand/micropub/PostViewModel.java
Normal file
13
app/src/main/java/eu/stuifzand/micropub/PostViewModel.java
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package eu.stuifzand.micropub;
|
||||||
|
|
||||||
|
import android.arch.lifecycle.ViewModel;
|
||||||
|
import android.databinding.ObservableField;
|
||||||
|
|
||||||
|
|
||||||
|
public class PostViewModel extends ViewModel {
|
||||||
|
public final ObservableField<String> content = new ObservableField<>();
|
||||||
|
|
||||||
|
public PostViewModel() {
|
||||||
|
this.content.set("test");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,15 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<layout 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"
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<data>
|
||||||
|
<variable
|
||||||
|
name="viewModel"
|
||||||
|
type="eu.stuifzand.micropub.PostViewModel" />
|
||||||
|
</data>
|
||||||
|
|
||||||
|
<android.support.design.widget.CoordinatorLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context="eu.stuifzand.micropub.MainActivity">
|
tools:context="eu.stuifzand.micropub.MainActivity">
|
||||||
|
@ -20,7 +28,7 @@
|
||||||
|
|
||||||
</android.support.design.widget.AppBarLayout>
|
</android.support.design.widget.AppBarLayout>
|
||||||
|
|
||||||
<include layout="@layout/content_main" />
|
<include layout="@layout/content_main" app:viewModel="@{viewModel}"/>
|
||||||
|
|
||||||
<android.support.design.widget.FloatingActionButton
|
<android.support.design.widget.FloatingActionButton
|
||||||
android:id="@+id/fab"
|
android:id="@+id/fab"
|
||||||
|
@ -31,3 +39,4 @@
|
||||||
app:srcCompat="@android:drawable/ic_dialog_email" />
|
app:srcCompat="@android:drawable/ic_dialog_email" />
|
||||||
|
|
||||||
</android.support.design.widget.CoordinatorLayout>
|
</android.support.design.widget.CoordinatorLayout>
|
||||||
|
</layout>
|
|
@ -1,7 +1,15 @@
|
||||||
<?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"
|
<layout 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"
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<data>
|
||||||
|
<variable
|
||||||
|
name="viewModel"
|
||||||
|
type="eu.stuifzand.micropub.PostViewModel" />
|
||||||
|
</data>
|
||||||
|
|
||||||
|
<android.support.constraint.ConstraintLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:background="@android:color/holo_blue_bright"
|
android:background="@android:color/holo_blue_bright"
|
||||||
|
@ -11,30 +19,28 @@
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/content"
|
android:id="@+id/content"
|
||||||
android:layout_width="0dp"
|
android:layout_width="328dp"
|
||||||
android:layout_height="230dp"
|
android:layout_height="230dp"
|
||||||
android:layout_marginBottom="272dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:layout_marginEnd="16dp"
|
android:layout_marginStart="8dp"
|
||||||
android:layout_marginStart="16dp"
|
|
||||||
android:layout_marginTop="16dp"
|
android:layout_marginTop="16dp"
|
||||||
android:autoText="false"
|
|
||||||
android:background="@android:color/white"
|
android:background="@android:color/white"
|
||||||
android:ems="10"
|
android:ems="10"
|
||||||
android:gravity="top"
|
android:gravity="top"
|
||||||
android:inputType="textMultiLine"
|
android:inputType="textMultiLine"
|
||||||
android:padding="3px"
|
android:padding="3dp"
|
||||||
android:singleLine="false"
|
android:singleLine="false"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
android:text="@={viewModel.content}"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintHorizontal_bias="0.0"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toBottomOf="@+id/labelCounter" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btnPost"
|
android:id="@+id/btnPost"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
|
android:layout_marginRight="8dp"
|
||||||
android:layout_marginTop="8dp"
|
android:layout_marginTop="8dp"
|
||||||
android:onClick="sendPost"
|
android:onClick="sendPost"
|
||||||
android:text="Post"
|
android:text="Post"
|
||||||
|
@ -51,4 +57,31 @@
|
||||||
android:text="Sign in"
|
android:text="Sign in"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent" />
|
app:layout_constraintStart_toStartOf="parent" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/labelCounter"
|
||||||
|
android:layout_width="74dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:text="@{String.valueOf(viewModel.content.length)}"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView2"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_marginBottom="8dp"
|
||||||
|
android:layout_marginEnd="8dp"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:text="@{viewModel.content}"
|
||||||
|
app:layout_constraintBottom_toTopOf="@+id/button"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/btnPost" />
|
||||||
|
|
||||||
</android.support.constraint.ConstraintLayout>
|
</android.support.constraint.ConstraintLayout>
|
||||||
|
|
||||||
|
</layout>
|
|
@ -25,3 +25,7 @@ allprojects {
|
||||||
task clean(type: Delete) {
|
task clean(type: Delete) {
|
||||||
delete rootProject.buildDir
|
delete rootProject.buildDir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ext {
|
||||||
|
archLifecycleVersion = "1.0.0"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user