diff options
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/ui/PushNotificationActivity.java')
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/PushNotificationActivity.java | 146 |
1 files changed, 98 insertions, 48 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/PushNotificationActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/PushNotificationActivity.java index b0b0d68ae..cdc20ed50 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/PushNotificationActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/PushNotificationActivity.java @@ -5,26 +5,28 @@ import android.net.Uri; import android.text.TextUtils; import android.view.View; +import androidx.annotation.ColorInt; import androidx.annotation.NonNull; import androidx.annotation.UiThread; import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.ViewModelProvider; -import com.nextcloud.android.sso.helper.SingleAccountHelper; - +import it.niedermann.android.util.ColorUtil; import it.niedermann.nextcloud.deck.DeckLog; import it.niedermann.nextcloud.deck.R; +import it.niedermann.nextcloud.deck.api.IResponseCallback; import it.niedermann.nextcloud.deck.databinding.ActivityPushNotificationBinding; import it.niedermann.nextcloud.deck.model.Account; -import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; import it.niedermann.nextcloud.deck.ui.card.EditActivity; import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler; +import it.niedermann.nextcloud.deck.util.ProjectUtil; -import static android.graphics.Color.parseColor; import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce; public class PushNotificationActivity extends AppCompatActivity { private ActivityPushNotificationBinding binding; + private PushNotificationViewModel viewModel; // Provided by Files app NotificationJob private static final String KEY_SUBJECT = "subject"; @@ -44,6 +46,8 @@ public class PushNotificationActivity extends AppCompatActivity { } binding = ActivityPushNotificationBinding.inflate(getLayoutInflater()); + viewModel = new ViewModelProvider(this).get(PushNotificationViewModel.class); + setContentView(binding.getRoot()); setSupportActionBar(binding.toolbar); @@ -56,62 +60,105 @@ public class PushNotificationActivity extends AppCompatActivity { } final String link = getIntent().getStringExtra(KEY_LINK); + long[] ids = ProjectUtil.extractBoardIdAndCardIdFromUrl(link); binding.cancel.setOnClickListener((v) -> finish()); - final SyncManager accountReadingSyncManager = new SyncManager(this); final String cardRemoteIdString = getIntent().getStringExtra(KEY_CARD_REMOTE_ID); final String accountString = getIntent().getStringExtra(KEY_ACCOUNT); DeckLog.verbose("cardRemoteIdString = " + cardRemoteIdString); - if (cardRemoteIdString != null) { - try { - final int cardRemoteId = Integer.parseInt(cardRemoteIdString); - observeOnce(accountReadingSyncManager.readAccount(accountString), this, (account -> { - if (account != null) { - SingleAccountHelper.setCurrentAccount(this, account.getName()); - final SyncManager syncManager = new SyncManager(this); - DeckLog.verbose("account: " + account); - observeOnce(syncManager.getLocalBoardIdByCardRemoteIdAndAccount(cardRemoteId, account), PushNotificationActivity.this, (boardLocalId -> { - DeckLog.verbose("BoardLocalId " + boardLocalId); - if (boardLocalId != null) { - observeOnce(syncManager.synchronizeCardByRemoteId(cardRemoteId, account), PushNotificationActivity.this, (fullCard -> { - DeckLog.verbose("FullCard: " + fullCard); - if (fullCard != null) { - runOnUiThread(() -> { - binding.submit.setOnClickListener((v) -> launchEditActivity(account, boardLocalId, fullCard.getLocalId())); - binding.submit.setText(R.string.simple_open); - applyBrandToSubmitButton(account); - binding.submit.setEnabled(true); - binding.progress.setVisibility(View.INVISIBLE); - }); - } else { - DeckLog.warn("Something went wrong while synchronizing the card " + cardRemoteId + " (cardRemoteId). Given fullCard is null."); - applyBrandToSubmitButton(account); - fallbackToBrowser(link); - } - })); - } else { - DeckLog.warn("Given localBoardId for cardRemoteId " + cardRemoteId + " is null."); - applyBrandToSubmitButton(account); - fallbackToBrowser(link); - } - })); - } else { - DeckLog.warn("Given account for " + accountString + " is null."); - fallbackToBrowser(link); - } - })); - } catch (NumberFormatException e) { - DeckLog.logError(e); + if (ids.length == 2) { + if (cardRemoteIdString != null) { + try { + final int cardRemoteId = Integer.parseInt(cardRemoteIdString); + observeOnce(viewModel.readAccount(accountString), this, (account -> { + if (account != null) { + viewModel.setAccount(account.getName()); + DeckLog.verbose("account: " + account); + observeOnce(viewModel.getBoardByRemoteId(account.getId(), ids[0]), PushNotificationActivity.this, (board -> { + DeckLog.verbose("BoardLocalId " + board); + if (board != null) { + observeOnce(viewModel.getCardByRemoteID(account.getId(), cardRemoteId), PushNotificationActivity.this, (card -> { + DeckLog.verbose("Card: " + card); + if (card != null) { + viewModel.synchronizeCard(new IResponseCallback<Boolean>(account) { + @Override + public void onResponse(Boolean response) { + openCardOnSubmit(account, board.getLocalId(), card.getLocalId()); + } + + @Override + public void onError(Throwable throwable) { + super.onError(throwable); + openCardOnSubmit(account, board.getLocalId(), card.getLocalId()); + } + }, card); + } else { + DeckLog.info("Card is not yet available locally. Synchronize board with localId " + board); + + viewModel.synchronizeBoard(new IResponseCallback<Boolean>(account) { + @Override + public void onResponse(Boolean response) { + runOnUiThread(() -> { + observeOnce(viewModel.getCardByRemoteID(account.getId(), cardRemoteId), PushNotificationActivity.this, (card -> { + DeckLog.verbose("Card: " + card); + if (card != null) { + openCardOnSubmit(account, board.getLocalId(), card.getLocalId()); + } else { + DeckLog.warn("Something went wrong while synchronizing the card " + cardRemoteId + " (cardRemoteId). Given fullCard is null."); + applyBrandToSubmitButton(account); + fallbackToBrowser(link); + } + })); + }); + } + + @Override + public void onError(Throwable throwable) { + super.onError(throwable); + DeckLog.warn("Something went wrong while synchronizing the board with localId " + board + "."); + applyBrandToSubmitButton(account); + fallbackToBrowser(link); + } + }, board.getLocalId()); + } + })); + } else { + DeckLog.warn("Given localBoardId for cardRemoteId " + cardRemoteId + " is null."); + applyBrandToSubmitButton(account); + fallbackToBrowser(link); + } + })); + } else { + DeckLog.warn("Given account for " + accountString + " is null."); + fallbackToBrowser(link); + } + })); + } catch (NumberFormatException e) { + DeckLog.logError(e); + fallbackToBrowser(link); + } + } else { + DeckLog.warn(KEY_CARD_REMOTE_ID + " is null."); fallbackToBrowser(link); } } else { - DeckLog.warn(KEY_CARD_REMOTE_ID + " is null."); + DeckLog.warn("Link does not contain two IDs (expected one board id and one card id): " + link); fallbackToBrowser(link); } } + private void openCardOnSubmit(@NonNull Account account, long boardLocalId, long cardlocalId) { + runOnUiThread(() -> { + binding.submit.setOnClickListener((v) -> launchEditActivity(account, boardLocalId, cardlocalId)); + binding.submit.setText(R.string.simple_open); + applyBrandToSubmitButton(account); + binding.submit.setEnabled(true); + binding.progress.setVisibility(View.INVISIBLE); + }); + } + /** * If anything goes wrong and we cannot open the card directly, we fall back to open the given link in the webbrowser */ @@ -146,10 +193,13 @@ public class PushNotificationActivity extends AppCompatActivity { return true; } + // TODO implement Branded interface + // TODO apply branding based on board color public void applyBrandToSubmitButton(@NonNull Account account) { + @ColorInt final int mainColor = account.getColor(); try { - binding.submit.setBackgroundColor(parseColor(account.getColor())); - binding.submit.setTextColor(parseColor(account.getTextColor())); + binding.submit.setBackgroundColor(mainColor); + binding.submit.setTextColor(ColorUtil.INSTANCE.getForegroundColorForBackgroundColor(mainColor)); } catch (Throwable t) { DeckLog.logError(t); } |