Twidere-App-Android-Twitter.../twidere.component.common/src/main/java/org/mariotaku/twidere/model/tab/argument/TabArguments.java

123 lines
4.0 KiB
Java
Raw Normal View History

/*
* Twidere - Twitter client for Android
*
2017-04-20 16:18:45 +02:00
* Copyright 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
2017-04-20 16:18:45 +02:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2017-04-20 16:18:45 +02:00
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2016-03-08 07:40:23 +01:00
package org.mariotaku.twidere.model.tab.argument;
import android.os.Bundle;
2020-01-26 08:35:15 +01:00
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2016-03-08 07:40:23 +01:00
2017-04-08 16:06:04 +02:00
import com.bluelinelabs.logansquare.LoganSquare;
2016-03-08 07:40:23 +01:00
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
2016-03-08 08:44:46 +01:00
import org.mariotaku.twidere.TwidereConstants;
2016-12-01 08:55:38 +01:00
import org.mariotaku.twidere.annotation.CustomTabType;
2016-03-08 07:40:23 +01:00
import org.mariotaku.twidere.model.UserKey;
2016-12-01 08:55:38 +01:00
import java.io.IOException;
2016-03-08 07:40:23 +01:00
import java.util.Arrays;
/**
* Created by mariotaku on 16/3/6.
*/
@JsonObject
2016-03-08 08:44:46 +01:00
public class TabArguments implements TwidereConstants {
2016-03-08 07:40:23 +01:00
@JsonField(name = "account_id")
2016-03-31 02:09:41 +02:00
String accountId;
2016-03-08 07:40:23 +01:00
@JsonField(name = "account_keys")
2016-11-30 08:18:43 +01:00
@Nullable
2016-03-08 07:40:23 +01:00
UserKey[] accountKeys;
2016-11-30 08:18:43 +01:00
@Nullable
2016-03-08 07:40:23 +01:00
public UserKey[] getAccountKeys() {
return accountKeys;
}
2016-11-30 08:18:43 +01:00
public void setAccountKeys(@Nullable UserKey[] accountKeys) {
2016-03-08 07:40:23 +01:00
this.accountKeys = accountKeys;
}
2016-03-31 02:09:41 +02:00
public String getAccountId() {
return accountId;
}
2016-03-08 07:40:23 +01:00
@CallSuper
public void copyToBundle(@NonNull Bundle bundle) {
2016-11-30 08:18:43 +01:00
final UserKey[] accountKeys = this.accountKeys;
2016-12-01 08:55:38 +01:00
if (accountKeys != null && accountKeys.length > 0) {
2016-03-25 15:41:04 +01:00
for (UserKey key : accountKeys) {
if (key == null) return;
}
2016-03-10 17:02:16 +01:00
bundle.putParcelableArray(EXTRA_ACCOUNT_KEYS, accountKeys);
} else if (accountId != null) {
2016-12-01 08:55:38 +01:00
long id = Long.MIN_VALUE;
try {
id = Long.parseLong(accountId);
} catch (NumberFormatException e) {
// Ignore
}
2016-03-31 02:09:41 +02:00
if (id != Long.MIN_VALUE && id <= 0) {
// account_id = -1, means no account selected
bundle.putParcelableArray(EXTRA_ACCOUNT_KEYS, null);
2016-03-31 02:09:41 +02:00
return;
}
bundle.putParcelableArray(EXTRA_ACCOUNT_KEYS, new UserKey[]{UserKey.valueOf(accountId)});
2016-03-08 07:40:23 +01:00
}
}
@Override
public String toString() {
return "TabArguments{" +
"accountId=" + accountId +
", accountKeys=" + Arrays.toString(accountKeys) +
'}';
}
2016-12-01 08:55:38 +01:00
2017-04-13 09:53:22 +02:00
/**
* Remember to make this method correspond to {@code CustomTabUtils#newTabArguments(String)}
*/
2016-12-01 08:55:38 +01:00
@Nullable
2016-12-01 15:02:10 +01:00
public static TabArguments parse(@NonNull @CustomTabType String type, @Nullable String json) throws IOException {
if (json == null) return null;
2016-12-01 08:55:38 +01:00
switch (type) {
case CustomTabType.HOME_TIMELINE:
case CustomTabType.NOTIFICATIONS_TIMELINE:
2017-02-05 08:24:20 +01:00
case CustomTabType.DIRECT_MESSAGES:
2017-04-13 09:53:22 +02:00
case CustomTabType.TRENDS_SUGGESTIONS:
case CustomTabType.PUBLIC_TIMELINE:
case CustomTabType.NETWORK_PUBLIC_TIMELINE: {
2017-04-08 16:06:04 +02:00
return LoganSquare.parse(json, TabArguments.class);
2016-12-01 08:55:38 +01:00
}
case CustomTabType.USER_TIMELINE:
case CustomTabType.FAVORITES: {
2017-04-08 16:06:04 +02:00
return LoganSquare.parse(json, UserArguments.class);
2016-12-01 08:55:38 +01:00
}
case CustomTabType.LIST_TIMELINE: {
2017-04-08 16:06:04 +02:00
return LoganSquare.parse(json, UserListArguments.class);
2016-12-01 08:55:38 +01:00
}
case CustomTabType.SEARCH_STATUSES: {
2017-04-08 16:06:04 +02:00
return LoganSquare.parse(json, TextQueryArguments.class);
2016-12-01 08:55:38 +01:00
}
}
return null;
}
2016-03-08 07:40:23 +01:00
}