diff options
author | stefan-niedermann <info@niedermann.it> | 2019-03-11 19:26:28 +0300 |
---|---|---|
committer | stefan-niedermann <info@niedermann.it> | 2019-03-11 19:26:28 +0300 |
commit | 3ff6732d0a46067d1dd60c8a062659c09c0bfae0 (patch) | |
tree | ae1195a2aef55c268c7e785f12191d847dc6b466 | |
parent | ec25615ea78324d313a17c6136cb39dd2b0b1086 (diff) |
Use databinding for displaying title of cards in edit mode
-rw-r--r-- | .idea/modules.xml | 1 | ||||
-rw-r--r-- | app/build.gradle | 3 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/model/viewmodel/FullCardViewModel.java | 12 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java | 63 | ||||
-rw-r--r-- | app/src/main/res/layout/activity_edit.xml | 82 |
5 files changed, 100 insertions, 61 deletions
diff --git a/.idea/modules.xml b/.idea/modules.xml index dd53b4260..435960061 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -4,7 +4,6 @@ <modules> <module fileurl="file://$PROJECT_DIR$/Android-SingleSignOn/Android-SingleSignOn.iml" filepath="$PROJECT_DIR$/Android-SingleSignOn/Android-SingleSignOn.iml" /> <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> - <module fileurl="file://$PROJECT_DIR$/nc_deck.iml" filepath="$PROJECT_DIR$/nc_deck.iml" /> <module fileurl="file://$PROJECT_DIR$/nextcloud-deck.iml" filepath="$PROJECT_DIR$/nextcloud-deck.iml" /> </modules> </component> diff --git a/app/build.gradle b/app/build.gradle index 3b54f9f9c..ce2004b45 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,6 +16,9 @@ android { } } } + dataBinding { + enabled = true + } buildTypes { release { minifyEnabled false diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/viewmodel/FullCardViewModel.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/viewmodel/FullCardViewModel.java new file mode 100644 index 000000000..204d38b8d --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/viewmodel/FullCardViewModel.java @@ -0,0 +1,12 @@ +package it.niedermann.nextcloud.deck.model.viewmodel; + +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.ViewModel; + +import it.niedermann.nextcloud.deck.model.full.FullCard; + +public class FullCardViewModel extends ViewModel { + + public LiveData<FullCard> fullCard; + +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java index 7079537c0..97154674d 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java @@ -1,5 +1,7 @@ package it.niedermann.nextcloud.deck.ui; +import android.arch.lifecycle.ViewModelProviders; +import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; @@ -12,7 +14,9 @@ import butterknife.ButterKnife; import butterknife.Unbinder; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.SupportUtil; +import it.niedermann.nextcloud.deck.databinding.ActivityEditBinding; import it.niedermann.nextcloud.deck.model.full.FullCard; +import it.niedermann.nextcloud.deck.model.viewmodel.FullCardViewModel; import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; import it.niedermann.nextcloud.deck.ui.card.CardTabAdapter; @@ -21,7 +25,6 @@ import static it.niedermann.nextcloud.deck.ui.card.CardAdapter.BUNDLE_KEY_LOCAL_ public class EditActivity extends AppCompatActivity { - FullCard card; SyncManager syncManager; @BindView(R.id.title) @@ -36,6 +39,7 @@ public class EditActivity extends AppCompatActivity { @BindView(R.id.pager) ViewPager pager; + FullCardViewModel fullCardViewModel; private Unbinder unbinder; private long accountId; @@ -44,36 +48,46 @@ public class EditActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_edit); + + + fullCardViewModel = ViewModelProviders.of(this) + .get(FullCardViewModel.class); + + ActivityEditBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_edit); + + // Assign the component to a property in the binding class. + binding.setLifecycleOwner(this); + binding.setEditmodel(fullCardViewModel); + + //setContentView(R.layout.activity_edit); unbinder = ButterKnife.bind(this); + Bundle extras = getIntent().getExtras(); if (extras != null) { accountId = extras.getLong(BUNDLE_KEY_ACCOUNT_ID); localId = extras.getLong(BUNDLE_KEY_LOCAL_ID); syncManager = new SyncManager(getApplicationContext(), this); - syncManager.getCardByLocalId(accountId, localId) - .observe(EditActivity.this, (FullCard card) -> { - this.card = card; - if (this.card != null) { - title.setText(this.card.getCard().getTitle()); - if (this.card.getCard().getCreatedAt() != null - && this.card.getCard().getLastModified() != null) { - timestamps.setText( - getString( - R.string.modified_created_time, - SupportUtil.getRelativeDateTimeString( - this, - this.card.getCard().getLastModified().getTime()), - SupportUtil.getRelativeDateTimeString( - this, - this.card.getCard().getCreatedAt().getTime()) - ) - ); - } - } - }); + fullCardViewModel.fullCard = syncManager.getCardByLocalId(accountId, localId); + fullCardViewModel.fullCard.observe(EditActivity.this, (FullCard card) -> { + if (card != null) { + if (card.getCard().getCreatedAt() != null + && card.getCard().getLastModified() != null) { + timestamps.setText( + getString( + R.string.modified_created_time, + SupportUtil.getRelativeDateTimeString( + this, + card.getCard().getLastModified().getTime()), + SupportUtil.getRelativeDateTimeString( + this, + card.getCard().getCreatedAt().getTime()) + ) + ); + } + } + }); } else { throw new IllegalArgumentException("No localId argument"); } @@ -94,7 +108,8 @@ public class EditActivity extends AppCompatActivity { @Override protected void onPause() { - syncManager.updateCard(this.card.card); + // TODO ???? + syncManager.updateCard(fullCardViewModel.fullCard.getValue().card); super.onPause(); } diff --git a/app/src/main/res/layout/activity_edit.xml b/app/src/main/res/layout/activity_edit.xml index 0a1afb66a..d2d323570 100644 --- a/app/src/main/res/layout/activity_edit.xml +++ b/app/src/main/res/layout/activity_edit.xml @@ -1,40 +1,50 @@ <?xml version="1.0" encoding="utf-8"?> -<LinearLayout 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" - android:layout_width="match_parent" - android:layout_height="match_parent" - android:orientation="vertical"> - - <EditText - android:id="@+id/title" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:hint="@string/label_title" - android:importantForAutofill="no" - android:layout_marginLeft="16dp" - android:layout_marginRight="16dp" - android:inputType="textPersonName" /> - - <TextView - android:id="@+id/timestamps" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_marginBottom="16dp" - android:layout_marginLeft="19dp" - android:layout_marginRight="19dp" - tools:text="Modified: 12 days ago Created: 12 days ago"/> - - <android.support.design.widget.TabLayout - android:id="@+id/tab_layout" - android:layout_width="match_parent" - android:layout_height="wrap_content" - app:tabGravity="center" - app:tabMode="fixed" /> +<layout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto"> - <android.support.v4.view.ViewPager - android:id="@+id/pager" + <data> + <variable + name="editmodel" + type="it.niedermann.nextcloud.deck.model.viewmodel.FullCardViewModel" /> + </data> + + <LinearLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="fill_parent"/> + android:layout_height="match_parent" + android:orientation="vertical"> + + + <EditText + android:id="@+id/title" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginLeft="16dp" + android:layout_marginRight="16dp" + android:text="@{editmodel.fullCard.card.title}" + android:hint="@string/label_title" + android:importantForAutofill="no" + android:inputType="textPersonName" /> + + <TextView + android:id="@+id/timestamps" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginLeft="19dp" + android:layout_marginRight="19dp" + android:layout_marginBottom="16dp" + tools:text="Modified: 12 days ago Created: 12 days ago" /> + + <android.support.design.widget.TabLayout + android:id="@+id/tab_layout" + android:layout_width="match_parent" + android:layout_height="wrap_content" + app:tabGravity="center" + app:tabMode="fixed" /> + + <android.support.v4.view.ViewPager + android:id="@+id/pager" + android:layout_width="match_parent" + android:layout_height="fill_parent" /> -</LinearLayout>
\ No newline at end of file + </LinearLayout> +</layout>
\ No newline at end of file |