mirror of
https://codeberg.org/tom79/Fedilab.git
synced 2025-07-14 07:30:29 +03:00
Add/Remove featured tags from profile editor
This commit is contained in:
parent
2e33cd58f5
commit
1c2c6f404a
2 changed files with 62 additions and 2 deletions
|
@ -38,6 +38,7 @@ import com.bumptech.glide.Glide;
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||||
import com.google.android.material.textfield.TextInputEditText;
|
import com.google.android.material.textfield.TextInputEditText;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
@ -396,6 +397,16 @@ public class EditProfileActivity extends BaseBarActivity {
|
||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
List<String> getFeaturedHashtags() {
|
||||||
|
List<String> featuredHashtags = new ArrayList<>();
|
||||||
|
for (int i = 0; i < binding.featuredHashtagsContainer.getChildCount(); i++) {
|
||||||
|
String name = Objects.requireNonNull(((TextInputEditText) binding.featuredHashtagsContainer.getChildAt(i).findViewById(R.id.name)).getText()).toString().trim();
|
||||||
|
featuredHashtags.add(name);
|
||||||
|
}
|
||||||
|
return featuredHashtags;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
if (item.getItemId() == android.R.id.home) {
|
if (item.getItemId() == android.R.id.home) {
|
||||||
|
@ -411,7 +422,8 @@ public class EditProfileActivity extends BaseBarActivity {
|
||||||
getPrivacy(),
|
getPrivacy(),
|
||||||
binding.sensitive.isChecked(),
|
binding.sensitive.isChecked(),
|
||||||
null,
|
null,
|
||||||
getFields()
|
getFields(),
|
||||||
|
getFeaturedHashtags()
|
||||||
)
|
)
|
||||||
.observe(EditProfileActivity.this, account -> {
|
.observe(EditProfileActivity.this, account -> {
|
||||||
if (account != null) {
|
if (account != null) {
|
||||||
|
|
|
@ -20,12 +20,14 @@ import android.net.Uri;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.lifecycle.AndroidViewModel;
|
import androidx.lifecycle.AndroidViewModel;
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
import java.net.IDN;
|
import java.net.IDN;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -251,6 +253,7 @@ public class AccountsVM extends AndroidViewModel {
|
||||||
* @param sensitive Whether to mark authored statuses as sensitive by default.
|
* @param sensitive Whether to mark authored statuses as sensitive by default.
|
||||||
* @param language Default language to use for authored statuses. (ISO 6391)
|
* @param language Default language to use for authored statuses. (ISO 6391)
|
||||||
* @param fields Profile metadata name (By default, max 4 fields and 255 characters per property/value)
|
* @param fields Profile metadata name (By default, max 4 fields and 255 characters per property/value)
|
||||||
|
* @param featuredHashtags Featured hashtags that will be displayed on the profile
|
||||||
* @return {@link LiveData} containing an {@link Account}
|
* @return {@link LiveData} containing an {@link Account}
|
||||||
*/
|
*/
|
||||||
public LiveData<Account> updateCredentials(@NonNull String instance, String token,
|
public LiveData<Account> updateCredentials(@NonNull String instance, String token,
|
||||||
|
@ -262,10 +265,15 @@ public class AccountsVM extends AndroidViewModel {
|
||||||
String privacy,
|
String privacy,
|
||||||
Boolean sensitive,
|
Boolean sensitive,
|
||||||
String language,
|
String language,
|
||||||
LinkedHashMap<Integer, Field.FieldParams> fields
|
LinkedHashMap<Integer, Field.FieldParams> fields,
|
||||||
|
List<String> featuredHashtags
|
||||||
) {
|
) {
|
||||||
MastodonAccountsService mastodonAccountsService = init(instance);
|
MastodonAccountsService mastodonAccountsService = init(instance);
|
||||||
accountMutableLiveData = new MutableLiveData<>();
|
accountMutableLiveData = new MutableLiveData<>();
|
||||||
|
if(featuredHashtags == null) {
|
||||||
|
featuredHashtags = new ArrayList<>();
|
||||||
|
}
|
||||||
|
List<String> finalFeaturedHashtags = featuredHashtags;
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
Account account = null;
|
Account account = null;
|
||||||
Account.AccountParams accountParams = new Account.AccountParams();
|
Account.AccountParams accountParams = new Account.AccountParams();
|
||||||
|
@ -291,6 +299,46 @@ public class AccountsVM extends AndroidViewModel {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Call<List<FeaturedTag>> featuredTagsCall = mastodonAccountsService.getFeaturedTags(token);
|
||||||
|
try {
|
||||||
|
Response<List<FeaturedTag>> featuredTagsResponse = featuredTagsCall.execute();
|
||||||
|
if (featuredTagsResponse.isSuccessful()) {
|
||||||
|
List<FeaturedTag> currentFeaturedTags = featuredTagsResponse.body();
|
||||||
|
if(currentFeaturedTags == null) {
|
||||||
|
currentFeaturedTags = new ArrayList<>();
|
||||||
|
}
|
||||||
|
List<String> currentTags = new ArrayList<>();
|
||||||
|
for(FeaturedTag featuredTag: currentFeaturedTags) {
|
||||||
|
currentTags.add(featuredTag.name);
|
||||||
|
}
|
||||||
|
List<String> toRemove = new ArrayList<>();
|
||||||
|
List<String> toAdd = new ArrayList<>();
|
||||||
|
for(String value: currentTags) {
|
||||||
|
if(!finalFeaturedHashtags.contains(value)){
|
||||||
|
toRemove.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(String value: finalFeaturedHashtags) {
|
||||||
|
if(!currentTags.contains(value)){
|
||||||
|
toAdd.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(String remove: toRemove) {
|
||||||
|
for(FeaturedTag featuredTag: currentFeaturedTags) {
|
||||||
|
if(featuredTag.name.trim().equalsIgnoreCase(remove.trim())) {
|
||||||
|
mastodonAccountsService.removeFeaturedTag(token, featuredTag.id).execute();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(String add: toAdd) {
|
||||||
|
mastodonAccountsService.addFeaturedTag(token, add).execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
Handler mainHandler = new Handler(Looper.getMainLooper());
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
Account finalAccount = account;
|
Account finalAccount = account;
|
||||||
Runnable myRunnable = () -> accountMutableLiveData.setValue(finalAccount);
|
Runnable myRunnable = () -> accountMutableLiveData.setValue(finalAccount);
|
||||||
|
|
Loading…
Reference in a new issue