Skip to content

Commit

Permalink
ShaareActivity: improve account switching behavior
Browse files Browse the repository at this point in the history
When switching account using the spinner, the interface will be updated with the new Shaarli info
  • Loading branch information
dimtion committed Aug 25, 2020
1 parent e0f7c68 commit c8d2d6f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Checkable;
import android.widget.EditText;
Expand Down Expand Up @@ -46,7 +47,6 @@ public class ShareActivity extends AppCompatActivity {
private boolean isLoadingDescription = false;
private boolean isPrefetching = false;


final int LOADER_TITLE = 0;
final int LOADER_DESCRIPTION = 1;
private boolean isNotNewLink = false;
Expand Down Expand Up @@ -77,7 +77,7 @@ protected void onCreate(Bundle savedInstanceState) {

readIntent(intent);

if(userPrefs.isOpenDialog()) {
if (userPrefs.isOpenDialog()) {
openDialog();
} else {
autoLoadTitleAndDescription(defaults);
Expand Down Expand Up @@ -264,17 +264,26 @@ private void prefetchLink(@NonNull Link defaults) {

private void initAccountSpinner() {
final Spinner accountSpinner = this.findViewById(R.id.chooseAccount);
ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.tags_list, accounts);
ArrayAdapter<ShaarliAccount> adapter = new ArrayAdapter<>(this, R.layout.tags_list, accounts);
accountSpinner.setAdapter(adapter);
if(accountSpinner.getCount() < 2) {
if (accountSpinner.getCount() < 2) {
accountSpinner.setVisibility(View.GONE);
}
AccountSpinnerListener listener = new AccountSpinnerListener();
accountSpinner.setOnItemSelectedListener(listener);
}

private void autoLoadTitleAndDescription(Link defaults) {
/**
* Load everything from the interface and share the link
*/
private void saveAndShare() {
sendLink(linkFromUserInput());
finish();
}

private void autoLoadTitleAndDescription(Link defaults) {
// Don't use network resources if not needed
if (!userPrefs.isAutoTitle() && !userPrefs.isAutoDescription()){
if (!userPrefs.isAutoTitle() && !userPrefs.isAutoDescription()) {
return;
}
// Launch intent to retrieve the title and the description
Expand Down Expand Up @@ -406,7 +415,6 @@ private void updateDescription(String description, boolean isError) {
descriptionEdit.setText(description);
}
updateLoadersVisibility();

}

private void updateTags(String tags, boolean isError) {
Expand Down Expand Up @@ -435,11 +443,8 @@ private void updateToot(boolean toot) {
tootCheck.setChecked(toot);
}

/**
* Load everything from the interface and share the link
*/
private void saveAndShare() {
Link link = new Link(
private Link linkFromUserInput() {
return new Link(
((EditText) findViewById(R.id.url)).getText().toString(),
((EditText) findViewById(R.id.title)).getText().toString(),
((EditText) findViewById(R.id.description)).getText().toString(),
Expand All @@ -451,8 +456,27 @@ private void saveAndShare() {
null,
null
);
sendLink(link);
finish();
}

class AccountSpinnerListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setLoading(LOADER_DESCRIPTION, true);
setLoading(LOADER_TITLE, true);
updateLoadersVisibility();
Link defaults = linkFromUserInput();
// Override text values to avoid messing with `seemsNewLink`
defaults.setTitle("");
defaults.setDescription("");
defaults.setTags("");

prefetchLink(defaults);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// pass
}
}

@Override
Expand Down Expand Up @@ -522,31 +546,38 @@ public void handleMessage(Message msg) {
case NetworkService.PREFETCH_LINK:
Log.i("NETWORK_MSG", "Link prefetched");

MenuItem isEditedIcon = menu.findItem(R.id.editing);
setLoading(LOADER_PREFETCH, false);
if (userPrefs.isOpenDialog()) {
Link prefetchedLink = (Link) msg.obj;

isNotNewLink = prefetchedLink.seemsNotNew();
Log.i("PREFETCH_LINK", "SeemsNotNew? " + isNotNewLink);
if (isNotNewLink) {
defaults = prefetchedLink;

// prefetching success: stop other loaders
setLoading(LOADER_TITLE, false);
setLoading(LOADER_DESCRIPTION, false);

// Update the interface
updateTitle(defaults.getTitle(), false);
updateDescription(defaults.getDescription(), false);
updateTags(defaults.getTags(), false);
if (defaults.getTitle().length() > 0) {
updateTitle(defaults.getTitle(), false);
}
if (defaults.getDescription().length() > 0) {
updateDescription(defaults.getDescription(), false);
}
if (defaults.getTagList().size() > 0) {
updateTags(defaults.getTags(), false);
}
updatePrivate(defaults.isPrivate());
updateTweet(defaults.isTweet());
updateToot(defaults.isToot());


// Show that we are editing an existing entry
MenuItem isNotNewIcon = menu.findItem(R.id.editing);
isNotNewIcon.setVisible(true);
isEditedIcon.setVisible(true);
} else {
isEditedIcon.setVisible(false);
}
// prefetch success: stop other loaders
setLoading(LOADER_TITLE, false);
setLoading(LOADER_DESCRIPTION, false);
updateLoadersVisibility();
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected Boolean doInBackground(String... foo) {
NetworkManager manager = NetworkUtils.getNetworkManager(account);
try {
if (!manager.isCompatibleShaarli() || !manager.login()) {
throw new Exception("Login Error");
throw new Exception(account + ": Login Error");
}
List<String> tags = manager.retrieveTags();
Log.d("TAGS", tags.toString());
Expand All @@ -103,7 +103,7 @@ protected Boolean doInBackground(String... foo) {
protected void onPostExecute(Boolean r) {
if(!r) {
String error = (mError != null) ? mError.getMessage() : "";
Toast.makeText(a_context, a_context.getString(R.string.error_retrieving_tags) + " -- " + error, Toast.LENGTH_LONG).show();
Toast.makeText(a_context, a_context.getString(R.string.error_retrieving_tags) + " - " + error, Toast.LENGTH_LONG).show();
} else {
updateTagsView();
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/dimtion/shaarlier/utils/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,4 @@ public boolean seemsNotNew() {
|| id != null
);
}

}

0 comments on commit c8d2d6f

Please sign in to comment.