Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AttachmentsActivity.java « attachments « ui « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 98cfd4440045ffed3bdd5629c324a0374c43e86a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package it.niedermann.nextcloud.deck.ui.attachments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.SharedElementCallback;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.databinding.ActivityAttachmentsBinding;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.Attachment;
import it.niedermann.nextcloud.deck.persistence.sync.SyncManager;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler;
import it.niedermann.nextcloud.deck.util.MimeTypeUtil;

public class AttachmentsActivity extends AppCompatActivity {

    private static final String BUNDLE_KEY_ACCOUNT = "account";
    private static final String BUNDLE_KEY_CARD_ID = "cardId";
    private static final String BUNDLE_KEY_CURRENT_ATTACHMENT_LOCAL_ID = "currentAttachmenLocaltId";

    private ActivityAttachmentsBinding binding;
    private ViewPager2.OnPageChangeCallback onPageChangeCallback;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler(this));

        binding = ActivityAttachmentsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        supportPostponeEnterTransition();

        setSupportActionBar(binding.toolbar);

        final Bundle args = getIntent().getExtras();
        if (args == null || !args.containsKey(BUNDLE_KEY_ACCOUNT) || !args.containsKey(BUNDLE_KEY_CARD_ID)) {
            throw new IllegalArgumentException("Provide at least " + BUNDLE_KEY_ACCOUNT + " and " + BUNDLE_KEY_CARD_ID);
        }

        final Account account = (Account) args.getSerializable(BUNDLE_KEY_ACCOUNT);

        if (account == null) {
            throw new IllegalArgumentException(BUNDLE_KEY_ACCOUNT + " must not be null.");
        }

        long cardId = args.getLong(BUNDLE_KEY_CARD_ID);

        final SyncManager syncManager = new SyncManager(this);
        syncManager.getCardByLocalId(account.getId(), cardId).observe(this, fullCard -> {
            final List<Attachment> attachments = new ArrayList<>();
            for (Attachment a : fullCard.getAttachments()) {
                if (MimeTypeUtil.isImage(a.getMimetype())) {
                    attachments.add(a);
                }
            }
            if (fullCard.getAttachments().size() == 0) {
                DeckLog.logError(new IllegalStateException(AttachmentsActivity.class.getSimpleName() + " called, but card " + fullCard.getLocalId() + "has no attachments"));
                supportFinishAfterTransition();
                return;
            }
            binding.toolbar.setSubtitle(fullCard.getCard().getTitle());
            onPageChangeCallback = new ViewPager2.OnPageChangeCallback() {
                @Override
                public void onPageSelected(int position) {
                    super.onPageSelected(position);
                    binding.toolbar.setTitle(attachments.get(position).getBasename());
                }
            };
            RecyclerView.Adapter adapter = new AttachmentAdapter(account, fullCard.getId(), attachments);
            binding.viewPager.setAdapter(adapter);
            binding.viewPager.registerOnPageChangeCallback(onPageChangeCallback);

            long currentAttachment = args.getLong(BUNDLE_KEY_CURRENT_ATTACHMENT_LOCAL_ID);
            if (currentAttachment != 0L) {
                for (int i = 0; i < attachments.size(); i++) {
                    if (attachments.get(i).getLocalId() == currentAttachment) {
                        binding.viewPager.setCurrentItem(i, false);
                        break;
                    }
                }
            }

            // https://android-developers.googleblog.com/2018/02/continuous-shared-element-transitions.html?m=1
            // https://github.com/android/animation-samples/blob/master/GridToPager/app/src/main/java/com/google/samples/gridtopager/fragment/ImagePagerFragment.java
            setEnterSharedElementCallback(new SharedElementCallback() {
                @Override
                public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                    // TODO Fix shared elements mapping

                    // This does only show an animation if origin and current ViewPager item match
                    long currentAttachmentLocalId = attachments.get(binding.viewPager.getCurrentItem()).getLocalId();
                    String transitionKey = getString(R.string.transition_attachment_preview, String.valueOf(currentAttachmentLocalId));
                    if (transitionKey.equals(names.get(0))) {
                        sharedElements.put(transitionKey, binding.viewPager.getRootView().findViewById(R.id.preview)
                        );
                    }

                    // This will move the picture back to the origin, regardless where the ViewPager has been scrolled in the meantime
                    // sharedElements.put(names.get(0), binding.viewPager.getRootView().findViewById(R.id.preview));
                }
            });
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        supportFinishAfterTransition();
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        supportFinishAfterTransition();
        return true;
    }

    @Override
    protected void onDestroy() {
        binding.viewPager.unregisterOnPageChangeCallback(onPageChangeCallback);
        super.onDestroy();
    }

    @NonNull
    public static Intent createIntent(@NonNull Context context, @NonNull Account account, Long cardLocalId, Long attachmentLocalId) {
        return new Intent(context, AttachmentsActivity.class)
                .putExtra(BUNDLE_KEY_ACCOUNT, account)
                .putExtra(BUNDLE_KEY_CARD_ID, cardLocalId)
                .putExtra(BUNDLE_KEY_CURRENT_ATTACHMENT_LOCAL_ID, attachmentLocalId)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
}