From 40091c4e204bb780a7495e71b7e47d81a7b4a0f1 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sat, 24 Feb 2018 20:17:55 +0100 Subject: [PATCH] Use data binding for main content view --- .idea/misc.xml | 2 +- app/build.gradle | 16 +++ .../eu/stuifzand/micropub/MainActivity.java | 54 ++++---- .../main/java/eu/stuifzand/micropub/Post.java | 23 +++ .../stuifzand/micropub/PostMessageTask.java | 21 +-- .../eu/stuifzand/micropub/PostViewModel.java | 13 ++ app/src/main/res/layout/activity_main.xml | 55 +++++--- app/src/main/res/layout/content_main.xml | 131 +++++++++++------- build.gradle | 4 + 9 files changed, 211 insertions(+), 108 deletions(-) create mode 100644 app/src/main/java/eu/stuifzand/micropub/Post.java create mode 100644 app/src/main/java/eu/stuifzand/micropub/PostViewModel.java diff --git a/.idea/misc.xml b/.idea/misc.xml index ba7052b..635999d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -24,7 +24,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index ce04e54..01a99b9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,6 +16,13 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + compileOptions { + targetCompatibility 1.8 + sourceCompatibility 1.8 + } + dataBinding { + enabled = true +} } dependencies { @@ -24,8 +31,17 @@ dependencies { implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:design:26.1.0' 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 group: 'com.google.code.gson', name: 'gson', version: '2.8.2' + testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' diff --git a/app/src/main/java/eu/stuifzand/micropub/MainActivity.java b/app/src/main/java/eu/stuifzand/micropub/MainActivity.java index 0b7b5b1..45fa4bf 100644 --- a/app/src/main/java/eu/stuifzand/micropub/MainActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/MainActivity.java @@ -6,44 +6,46 @@ import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; +import android.app.Activity; +import android.arch.lifecycle.ViewModelProviders; import android.content.Intent; +import android.databinding.DataBindingUtil; 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.widget.Toolbar; import android.text.TextUtils; import android.util.Log; -import android.view.View; import android.view.Menu; import android.view.MenuItem; -import android.widget.EditText; +import android.view.View; import android.widget.Toast; import java.io.IOException; -import okhttp3.Call; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; +import eu.stuifzand.micropub.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle 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); setSupportActionBar(toolbar); - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - } - }); +// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); +// fab.setOnClickListener(new View.OnClickListener() { +// @Override +// public void onClick(View view) { +// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) +// .setAction("Action", null).show(); +// } +// }); + } @Override @@ -76,8 +78,6 @@ public class MainActivity extends AppCompatActivity { AccountManager am = AccountManager.get(this); Bundle options = new Bundle(); am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(true), null); - - } private class OnTokenAcquired implements AccountManagerCallback { @@ -103,19 +103,23 @@ public class MainActivity extends AppCompatActivity { String token = bundle.getString(AccountManager.KEY_AUTHTOKEN); if (sendMessage) { - EditText mEdit = (EditText) findViewById(R.id.content); + PostViewModel model = ViewModelProviders.of(MainActivity.this).get(PostViewModel.class); AccountManager am = AccountManager.get(MainActivity.this); Account[] accounts = am.getAccountsByType(bundle.getString("accountType")); String accountName = bundle.getString("authAccount"); - String micropub = null; + + String micropubBackend = null; for (Account account : accounts) { if (account.name.equals(accountName)) { - micropub = am.getUserData(account, "micropub"); + micropubBackend = am.getUserData(account, "micropub"); } } - String micropubBackend = micropub; - Log.i("micropub", "Sending message to " + micropubBackend); - new PostMessageTask(MainActivity.this, token, mEdit).execute(micropubBackend, mEdit.getText().toString()); + + if (micropubBackend != null) { + Log.i("micropub", "Sending message to " + micropubBackend); + new PostMessageTask(MainActivity.this, token, model, micropubBackend) + .execute(); + } } Log.d("micropub", "GetTokenForAccount Bundle is " + token); diff --git a/app/src/main/java/eu/stuifzand/micropub/Post.java b/app/src/main/java/eu/stuifzand/micropub/Post.java new file mode 100644 index 0000000..94345fd --- /dev/null +++ b/app/src/main/java/eu/stuifzand/micropub/Post.java @@ -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); + } +} diff --git a/app/src/main/java/eu/stuifzand/micropub/PostMessageTask.java b/app/src/main/java/eu/stuifzand/micropub/PostMessageTask.java index 64fdb9f..f5a3444 100644 --- a/app/src/main/java/eu/stuifzand/micropub/PostMessageTask.java +++ b/app/src/main/java/eu/stuifzand/micropub/PostMessageTask.java @@ -3,8 +3,6 @@ package eu.stuifzand.micropub; import android.content.Context; import android.os.AsyncTask; import android.util.Log; -import android.view.View; -import android.widget.EditText; import android.widget.Toast; import java.io.IOException; @@ -19,25 +17,28 @@ import okhttp3.Response; public class PostMessageTask extends AsyncTask { private final String accessToken; - private EditText mEdit; + private PostViewModel postModel; + private String micropubBackend; private WeakReference context; - public PostMessageTask(Context context, String accessToken, EditText mEdit) { + public PostMessageTask(Context context, String accessToken, PostViewModel postModel, String micropubBackend) { this.context = new WeakReference(context); this.accessToken = accessToken; - this.mEdit = mEdit; + assert postModel != null; + this.postModel = postModel; + this.micropubBackend = micropubBackend; } @Override protected String doInBackground(String... strings) { - RequestBody formBody = new FormBody.Builder() - .add("content", strings[1]) - .build(); + FormBody.Builder builder = new FormBody.Builder(); + builder.add("content", postModel.content.get()); + RequestBody formBody = builder.build(); Request request = new Request.Builder() .addHeader("Authorization", "Bearer " + accessToken) .method("POST", formBody) - .url(strings[0]) + .url(micropubBackend) .build(); OkHttpClient client = new OkHttpClient(); @@ -59,7 +60,7 @@ public class PostMessageTask extends AsyncTask { } protected void onPostExecute(String message) { - mEdit.setText(""); Toast.makeText(context.get(), message, Toast.LENGTH_SHORT).show(); + postModel.content.set(""); } } diff --git a/app/src/main/java/eu/stuifzand/micropub/PostViewModel.java b/app/src/main/java/eu/stuifzand/micropub/PostViewModel.java new file mode 100644 index 0000000..8a92f8b --- /dev/null +++ b/app/src/main/java/eu/stuifzand/micropub/PostViewModel.java @@ -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 content = new ObservableField<>(); + + public PostViewModel() { + this.content.set("test"); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e45b541..4a9638c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,33 +1,42 @@ - + xmlns:tools="http://schemas.android.com/tools"> - + + + + + android:layout_height="match_parent" + tools:context="eu.stuifzand.micropub.MainActivity"> - + android:layout_height="wrap_content" + android:theme="@style/AppTheme.AppBarOverlay"> - + - + - + - + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 5364608..1509d49 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -1,54 +1,87 @@ - + xmlns:tools="http://schemas.android.com/tools"> - + + + -