1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 19:36:50 +01:00

Contacts: Migrate to ActivityResultContracts

This commit is contained in:
José Rebelo 2023-08-21 21:48:03 +01:00
parent 353c6d9679
commit 74dc0f70a6
2 changed files with 21 additions and 14 deletions

View File

@ -16,10 +16,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities; package nodomain.freeyourgadget.gadgetbridge.activities;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.MenuItem; import android.view.MenuItem;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -51,11 +56,17 @@ import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
public class ConfigureContacts extends AbstractGBActivity { public class ConfigureContacts extends AbstractGBActivity {
private static final Logger LOG = LoggerFactory.getLogger(ConfigureContacts.class); private static final Logger LOG = LoggerFactory.getLogger(ConfigureContacts.class);
private static final int REQ_CONFIGURE_CONTACT = 1;
private GBContactListAdapter mGBContactListAdapter; private GBContactListAdapter mGBContactListAdapter;
private GBDevice gbDevice; private GBDevice gbDevice;
private ActivityResultLauncher<Intent> configureContactLauncher;
private final ActivityResultCallback<ActivityResult> configureContactCallback = result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
updateContactsFromDB();
sendContactsToDevice();
}
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -64,6 +75,11 @@ public class ConfigureContacts extends AbstractGBActivity {
gbDevice = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE); gbDevice = getIntent().getParcelableExtra(GBDevice.EXTRA_DEVICE);
configureContactLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
configureContactCallback
);
mGBContactListAdapter = new GBContactListAdapter(this); mGBContactListAdapter = new GBContactListAdapter(this);
final RecyclerView contactsRecyclerView = findViewById(R.id.contact_list); final RecyclerView contactsRecyclerView = findViewById(R.id.contact_list);
@ -105,16 +121,6 @@ public class ConfigureContacts extends AbstractGBActivity {
}); });
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CONFIGURE_CONTACT && resultCode == 1) {
updateContactsFromDB();
sendContactsToDevice();
}
}
private Contact createDefaultContact(@NonNull Device device, @NonNull User user) { private Contact createDefaultContact(@NonNull Device device, @NonNull User user) {
final Contact contact = new Contact(); final Contact contact = new Contact();
contact.setName(""); contact.setName("");
@ -151,7 +157,7 @@ public class ConfigureContacts extends AbstractGBActivity {
final Intent startIntent = new Intent(getApplicationContext(), ContactDetails.class); final Intent startIntent = new Intent(getApplicationContext(), ContactDetails.class);
startIntent.putExtra(GBDevice.EXTRA_DEVICE, gbDevice); startIntent.putExtra(GBDevice.EXTRA_DEVICE, gbDevice);
startIntent.putExtra(Contact.EXTRA_CONTACT, contact); startIntent.putExtra(Contact.EXTRA_CONTACT, contact);
startActivityForResult(startIntent, REQ_CONFIGURE_CONTACT); configureContactLauncher.launch(startIntent);
} }
public void deleteContact(final Contact contact) { public void deleteContact(final Contact contact) {

View File

@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities; package nodomain.freeyourgadget.gadgetbridge.activities;
import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -110,7 +111,7 @@ public class ContactDetails extends AbstractGBActivity {
} }
updateContact(); updateContact();
ContactDetails.this.setResult(1); ContactDetails.this.setResult(Activity.RESULT_OK);
finish(); finish();
}); });