-
Notifications
You must be signed in to change notification settings - Fork 92
Update peer list when peers tab opened #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fb37c58
Add abstract implementation of StateListener
doromaraujo 4e54dc1
Add adapter to implement only StateListener
doromaraujo 0e8121e
Add ViewModel usage to PeersFragment
doromaraujo 9857c4e
Remove unused code and references
doromaraujo bd81f5b
Add null check for model before unregistering service state listener
doromaraujo e6e0091
Make UI state immutable
doromaraujo e773dd9
Change exception message in Status.fromString
doromaraujo fa2c24d
Add Locale.ROOT when doing status.ToLowerCase()
doromaraujo fd5fc23
Skip null peer info or connStatus
doromaraujo 962fd00
Move cleaning up stateListenerRegistry and serviceAccessor to onDetac…
doromaraujo d9fa6fe
Reverse cleanup order
doromaraujo b9dcf7f
Add Locale.ROOT usage to exception message
doromaraujo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package io.netbird.client; | ||
|
|
||
| public interface PeersStateListener { | ||
| void onPeersChanged(long totalPeers); | ||
| } |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/io/netbird/client/PeersStateListenerAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package io.netbird.client; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| public class PeersStateListenerAdapter extends StateListenerAdapter { | ||
| private PeersStateListener listener; | ||
|
|
||
| public PeersStateListenerAdapter(@NotNull PeersStateListener listener) { | ||
| this.listener = listener; | ||
| } | ||
|
|
||
| public void clearListener() { | ||
| this.listener = null; | ||
| } | ||
|
|
||
| @Override | ||
| public void onPeersListChanged(long totalPeers) { | ||
| if (listener == null) return; | ||
|
|
||
| listener.onPeersChanged(totalPeers); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/io/netbird/client/StateListenerAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package io.netbird.client; | ||
|
|
||
| public abstract class StateListenerAdapter implements StateListener { | ||
|
|
||
| @Override | ||
| public void onEngineStarted() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onEngineStopped() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onAddressChanged(String fqdn, String ip) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onConnected() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onConnecting() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onDisconnected() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onDisconnecting() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onPeersListChanged(long totalPeers) { | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/main/java/io/netbird/client/ui/home/PeersFragmentUiState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package io.netbird.client.ui.home; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class PeersFragmentUiState { | ||
| private final List<Peer> peers; | ||
|
|
||
| public PeersFragmentUiState(List<Peer> peers) { | ||
| this.peers = new ArrayList<>(peers); | ||
| } | ||
|
|
||
| public List<Peer> getPeers() { | ||
| return Collections.unmodifiableList(peers); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
88 changes: 88 additions & 0 deletions
88
app/src/main/java/io/netbird/client/ui/home/PeersFragmentViewModel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package io.netbird.client.ui.home; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.lifecycle.LiveData; | ||
| import androidx.lifecycle.MutableLiveData; | ||
| import androidx.lifecycle.ViewModel; | ||
| import androidx.lifecycle.ViewModelProvider; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import io.netbird.client.PeersStateListener; | ||
| import io.netbird.client.PeersStateListenerAdapter; | ||
| import io.netbird.client.ServiceAccessor; | ||
| import io.netbird.client.StateListener; | ||
| import io.netbird.gomobile.android.PeerInfo; | ||
| import io.netbird.gomobile.android.PeerInfoArray; | ||
|
|
||
| public class PeersFragmentViewModel extends ViewModel implements PeersStateListener { | ||
| private final PeersStateListenerAdapter peersAdapter; | ||
| private final ServiceAccessor serviceAccessor; | ||
|
|
||
| private final MutableLiveData<PeersFragmentUiState> uiState = | ||
| new MutableLiveData<>(new PeersFragmentUiState(new ArrayList<>())); | ||
|
|
||
| public PeersFragmentViewModel(ServiceAccessor serviceAccessor) { | ||
| peersAdapter = new PeersStateListenerAdapter(this); | ||
| this.serviceAccessor = serviceAccessor; | ||
| } | ||
|
|
||
| public static ViewModelProvider.Factory getFactory(ServiceAccessor serviceAccessor) { | ||
| return new ViewModelProvider.Factory() { | ||
| @NonNull | ||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { | ||
| if (modelClass.isAssignableFrom(PeersFragmentViewModel.class)) { | ||
| return (T) new PeersFragmentViewModel(serviceAccessor); | ||
| } | ||
| throw new IllegalArgumentException("Unknown ViewModel class"); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private List<Peer> getPeers(PeerInfoArray peersInfo) { | ||
| List<Peer> peers = new ArrayList<>(); | ||
| PeerInfo peerInfo; | ||
| String connStatus; | ||
| Status status; | ||
|
|
||
| for (int i = 0; i < peersInfo.size(); i++) { | ||
| peerInfo = peersInfo.get(i); | ||
|
|
||
| if (peerInfo == null) { | ||
| continue; | ||
| } | ||
|
|
||
| connStatus = peerInfo.getConnStatus(); | ||
| if (connStatus == null) { | ||
| continue; | ||
| } | ||
|
|
||
| status = Status.fromString(connStatus); | ||
| peers.add(new Peer(status, peerInfo.getIP(), peerInfo.getFQDN())); | ||
| } | ||
| return peers; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| public LiveData<PeersFragmentUiState> getUiState() { | ||
| return uiState; | ||
| } | ||
|
|
||
| public StateListener getStateListener() { | ||
| return this.peersAdapter; | ||
| } | ||
|
|
||
| @Override | ||
| protected void onCleared() { | ||
| peersAdapter.clearListener(); | ||
| super.onCleared(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onPeersChanged(long totalPeers) { | ||
| var peers = getPeers(serviceAccessor.getPeersList()); | ||
| this.uiState.postValue(new PeersFragmentUiState(peers)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.