Skip to content

Commit 9c9bb99

Browse files
committed
performance: passing repository owner & name saves one HTTP request; can't be avoided with deep-links.
1 parent 6413673 commit 9c9bb99

File tree

9 files changed

+269
-107
lines changed

9 files changed

+269
-107
lines changed

mobile/src/main/java/io/syslogic/github/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public final class Constants {
1212
@NonNull public static final String ARGUMENT_ITEM_ID = "itemId";
1313
@NonNull public static final String ARGUMENT_ITEM_NAME = "name";
1414
@NonNull public static final String ARGUMENT_REPO_ID = "repoId";
15+
@NonNull public static final String ARGUMENT_REPO_OWNER = "repoOwner";
16+
@NonNull public static final String ARGUMENT_REPO_NAME = "repoName";
1517
@NonNull public static final String ARGUMENT_RUN_ID = "runId";
1618
@NonNull public static final String ARGUMENT_REPOSITORY_TOPIC = "topic";
1719

mobile/src/main/java/io/syslogic/github/fragment/RepositoryFragment.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class RepositoryFragment extends BaseFragment implements TokenCallback {
7474
/** Data-Binding */
7575
FragmentRepositoryBinding mDataBinding;
7676
ProgressDialogFragment currentDialog;
77-
Long itemId = -1L;
77+
Long repositoryId = -1L;
7878

7979
/** Constructor */
8080
public RepositoryFragment() {}
@@ -94,7 +94,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
9494
this.registerBroadcastReceiver();
9595
Bundle args = this.getArguments();
9696
if (args != null) {
97-
this.setItemId(args.getLong(Constants.ARGUMENT_REPO_ID));
97+
this.setRepositoryId(args.getLong(Constants.ARGUMENT_REPO_ID));
9898
}
9999
}
100100

@@ -119,8 +119,8 @@ public void onPageCommitVisible (WebView view, String url) {
119119
}
120120
}
121121
});
122+
this.getRepository(this.repositoryId);
122123

123-
this.setRepository();
124124
this.mDataBinding.toolbarDownload.spinnerBranch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
125125
int count = 0;
126126
@Override
@@ -230,8 +230,8 @@ void downloadBranchAsZip(@Nullable String branch) {
230230
downloadZipball(this.getDataBinding().getRepository(), branch);
231231
}
232232

233-
private void setItemId(@NonNull Long value) {
234-
this.itemId = value;
233+
private void setRepositoryId(@NonNull Long value) {
234+
this.repositoryId = value;
235235
}
236236

237237
@NonNull
@@ -266,7 +266,7 @@ public void onNetworkAvailable() {
266266
}
267267

268268
if (this.mDataBinding != null && !this.contentLoaded) {
269-
setRepository();
269+
getRepository(this.repositoryId);
270270
}
271271
}
272272
}
@@ -279,11 +279,10 @@ public void onNetworkLost() {
279279
}
280280
}
281281

282-
private void setRepository() {
282+
private void getRepository(Long repositoryId) {
283+
if (repositoryId != 0) {
283284

284-
if (this.itemId != 0) {
285-
286-
Call<Repository> api = GithubClient.getRepository(this.itemId);
285+
Call<Repository> api = GithubClient.getRepository(repositoryId);
287286
if (mDebug) {Log.w(LOG_TAG, api.request().url() + "");}
288287

289288
api.enqueue(new Callback<>() {
@@ -318,9 +317,7 @@ public void onResponse(@NonNull Call<Repository> call, @NonNull Response<Reposit
318317

319318
@Override
320319
public void onFailure(@NonNull Call<Repository> call, @NonNull Throwable t) {
321-
if (mDebug) {
322-
Log.e(LOG_TAG, "" + t.getMessage());
323-
}
320+
if (mDebug) {Log.e(LOG_TAG, "" + t.getMessage());}
324321
}
325322
});
326323
}

mobile/src/main/java/io/syslogic/github/fragment/WorkflowJobsFragment.java

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ public class WorkflowJobsFragment extends BaseFragment {
4545
/** Data-Binding */
4646
FragmentWorkflowJobsBinding mDataBinding;
4747

48-
/** The itemId is the repositoryId. */
49-
private Long itemId = -1L;
48+
/** The repository's ID. */
49+
Long repositoryId = -1L;
50+
51+
/** The repository's owner. */
52+
private String repositoryOwner;
53+
54+
/** The repository's name. */
55+
private String repositoryName;
56+
57+
/** The workflow run's ID. */
5058
private Long runId = -1L;
5159

5260
/** Constructor */
@@ -66,8 +74,16 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
6674
super.onCreate(savedInstanceState);
6775
Bundle args = this.getArguments();
6876
if (args != null) {
69-
this.setItemId(args.getLong(Constants.ARGUMENT_REPO_ID));
70-
this.setRunId(args.getLong(Constants.ARGUMENT_RUN_ID));
77+
if (args.keySet().contains("android-support-nav:controller:deepLinkIntent")) {
78+
//noinspection DataFlowIssue
79+
this.setRepositoryId(Long.valueOf(args.getString(Constants.ARGUMENT_REPO_ID)));
80+
this.setRunId(args.getLong(Constants.ARGUMENT_RUN_ID));
81+
} else {
82+
this.setRepositoryId(args.getLong(Constants.ARGUMENT_REPO_ID));
83+
this.setRepositoryOwner(args.getString(Constants.ARGUMENT_REPO_OWNER, null));
84+
this.setRepositoryName(args.getString(Constants.ARGUMENT_REPO_NAME, null));
85+
this.setRunId(args.getLong(Constants.ARGUMENT_RUN_ID));
86+
}
7187
}
7288
}
7389

@@ -84,17 +100,24 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
84100
activity.setSupportActionBar(this.getDataBinding().toolbarWorkflowJobs.toolbarWorkflowJobs);
85101
this.mDataBinding.toolbarWorkflowJobs.home.setOnClickListener(view -> activity.onBackPressed());
86102

103+
// Recyclerview.Adapter
104+
WorkflowStepsAdapter adapter = new WorkflowStepsAdapter(requireContext());
105+
this.getDataBinding().recyclerviewWorkflowSteps.setAdapter(adapter);
106+
87107
if (! isNetworkAvailable(this.requireContext())) {
88108
this.onNetworkLost();
89-
} else if (this.itemId != -1L) {
90-
WorkflowStepsAdapter adapter = new WorkflowStepsAdapter(requireContext());
91-
this.getDataBinding().recyclerviewWorkflowSteps.setAdapter(adapter);
92-
this.setRepositoryId(this.itemId);
109+
} else if (this.repositoryOwner != null && this.repositoryName != null) {
110+
/* No need to load the repository, when the owner, name and runId are known. */
111+
adapter.getWorkflowSteps(getAccessToken(), this.repositoryOwner, this.repositoryName, this.runId);
112+
} else if (this.repositoryId != -1L) {
113+
/* Load the repository, when only the ID is known (eg. when started from deep-link intent). */
114+
this.getRepository(this.repositoryId);
93115
}
116+
94117
return this.getDataBinding().getRoot();
95118
}
96119

97-
private void setRepositoryId(long repositoryId) {
120+
private void getRepository(Long repositoryId) {
98121

99122
if (repositoryId != 0) {
100123

@@ -107,14 +130,15 @@ public void onResponse(@NonNull Call<Repository> call, @NonNull Response<Reposit
107130
switch (response.code()) {
108131
case 200 -> {
109132
if (response.body() != null) {
110-
111133
Repository item = response.body();
112134
mDataBinding.setRepository(item);
113135

136+
/* Filling in the blanks. */
137+
repositoryOwner = item.getOwner().getLogin();
138+
repositoryName = item.getName();
139+
114140
WorkflowStepsAdapter adapter = ((WorkflowStepsAdapter) mDataBinding.recyclerviewWorkflowSteps.getAdapter());
115-
if (adapter != null) {
116-
adapter.getWorkflowSteps(getAccessToken(), item.getOwner().getLogin(), item.getName(), getRunId());
117-
}
141+
if (adapter != null) {adapter.getWorkflowSteps(getAccessToken(), repositoryOwner, repositoryName, getRunId());}
118142
}
119143
}
120144
case 403 -> {
@@ -143,17 +167,37 @@ public void onFailure(@NonNull Call<Repository> call, @NonNull Throwable t) {
143167
}
144168

145169
@NonNull
146-
public Long getItemId() {
147-
return this.itemId;
170+
public Long getRepositoryId() {
171+
return this.repositoryId;
172+
}
173+
174+
@Nullable
175+
public String getRepositoryOwner() {
176+
return this.repositoryOwner;
177+
}
178+
179+
@Nullable
180+
public String getRepositoryName() {
181+
return this.repositoryName;
148182
}
183+
149184
@NonNull
150185
public Long getRunId() {
151186
return this.runId;
152187
}
153188

154-
private void setItemId(@NonNull Long value) {
155-
this.itemId = value;
189+
private void setRepositoryId(@NonNull Long value) {
190+
this.repositoryId = value;
156191
}
192+
193+
private void setRepositoryOwner(@NonNull String value) {
194+
this.repositoryOwner = value;
195+
}
196+
197+
private void setRepositoryName(@NonNull String value) {
198+
this.repositoryName = value;
199+
}
200+
157201
private void setRunId(@NonNull Long value) {
158202
this.runId = value;
159203
}

mobile/src/main/java/io/syslogic/github/fragment/WorkflowRunFragment.java

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.syslogic.github.databinding.FragmentWorkflowRunBinding;
2626
import io.syslogic.github.provider.WorkflowsMenuProvider;
2727

28+
import io.syslogic.github.recyclerview.WorkflowRunsAdapter;
2829
import retrofit2.Call;
2930
import retrofit2.Callback;
3031
import retrofit2.Response;
@@ -45,8 +46,16 @@ public class WorkflowRunFragment extends BaseFragment {
4546
/** Data-Binding */
4647
FragmentWorkflowRunBinding mDataBinding;
4748

48-
/** The itemId is the repositoryId. */
49-
private Long itemId = -1L;
49+
/** The repository's ID. */
50+
Long repositoryId = -1L;
51+
52+
/** The repository's owner. */
53+
private String repositoryOwner;
54+
55+
/** The repository's name. */
56+
private String repositoryName;
57+
58+
/** The workflow run's ID. */
5059
private Long runId = -1L;
5160

5261
/** Constructor */
@@ -66,8 +75,15 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
6675
super.onCreate(savedInstanceState);
6776
Bundle args = this.getArguments();
6877
if (args != null) {
69-
this.setItemId(args.getLong(Constants.ARGUMENT_REPO_ID));
70-
this.setRunId(args.getLong(Constants.ARGUMENT_RUN_ID));
78+
if (args.keySet().contains("android-support-nav:controller:deepLinkIntent")) {
79+
//noinspection DataFlowIssue
80+
this.setRepositoryId(Long.valueOf(args.getString(Constants.ARGUMENT_REPO_ID)));
81+
//noinspection DataFlowIssue
82+
this.setRunId(Long.valueOf(args.getString(Constants.ARGUMENT_RUN_ID)));
83+
} else {
84+
this.setRepositoryId(args.getLong(Constants.ARGUMENT_REPO_ID));
85+
this.setRunId(args.getLong(Constants.ARGUMENT_RUN_ID));
86+
}
7187
}
7288
}
7389

@@ -86,15 +102,18 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
86102

87103
if (! isNetworkAvailable(this.requireContext())) {
88104
this.onNetworkLost();
89-
} else if (itemId != -1L) {
90-
// WorkflowRunsAdapter adapter = new WorkflowRunsAdapter(requireContext());
91-
// this.getDataBinding().recyclerviewWorkflowRuns.setAdapter(adapter);
92-
this.setRepositoryId(itemId);
105+
} else if (this.repositoryOwner != null && this.repositoryName != null) {
106+
/* No need to load the repository, when the owner and name are known. */
107+
this.getWorkflowRun();
108+
} else if (this.repositoryId != -1L) {
109+
/* Load the repository, when only the ID is known (eg. when started from deep-link intent). */
110+
this.getRepository(this.repositoryId);
93111
}
112+
94113
return this.getDataBinding().getRoot();
95114
}
96115

97-
private void setRepositoryId(long repositoryId) {
116+
private void getRepository(Long repositoryId) {
98117
if (repositoryId != 0) {
99118

100119
Call<Repository> api = GithubClient.getRepository(repositoryId);
@@ -106,11 +125,13 @@ public void onResponse(@NonNull Call<Repository> call, @NonNull Response<Reposit
106125
switch (response.code()) {
107126
case 200 -> {
108127
if (response.body() != null) {
109-
110128
Repository item = response.body();
111129
mDataBinding.setRepository(item);
112130

113-
Call<WorkflowRun> api2 = GithubClient.getWorkflowRun(getAccessToken(), item.getOwner().getLogin(), item.getName(), getRunId());
131+
/* Filling in the blanks. */
132+
repositoryOwner = item.getOwner().getLogin();
133+
repositoryName = item.getName();
134+
getWorkflowRun();
114135
}
115136
}
116137
case 403 -> {
@@ -140,17 +161,58 @@ public void onFailure(@NonNull Call<Repository> call, @NonNull Throwable t) {
140161
}
141162
}
142163

164+
private void getWorkflowRun() {
165+
166+
Call<WorkflowRun> api = GithubClient.getWorkflowRun(getAccessToken(), repositoryOwner, repositoryName, getRunId());
167+
if (mDebug) {Log.w(LOG_TAG, api.request().url() + "");}
168+
169+
api.enqueue(new Callback<>() {
170+
@Override
171+
public void onResponse(@NonNull Call<WorkflowRun> call, @NonNull Response<WorkflowRun> response) {
172+
switch (response.code()) {
173+
case 200 -> {
174+
if (response.body() != null) {
175+
WorkflowRun item = response.body();
176+
mDataBinding.setRun(item);
177+
}
178+
}
179+
case 403 -> {
180+
if (response.errorBody() != null) {
181+
try {
182+
String errors = response.errorBody().string();
183+
JsonObject jsonObject = JsonParser.parseString(errors).getAsJsonObject();
184+
String message = jsonObject.get("message").toString();
185+
if (mDebug) {
186+
Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
187+
Log.e(LOG_TAG, message);
188+
}
189+
} catch (IOException e) {
190+
if (mDebug) {Log.e(LOG_TAG, "" + e.getMessage());}
191+
}
192+
}
193+
}
194+
}
195+
}
196+
@Override
197+
public void onFailure(@NonNull Call<WorkflowRun> call, @NonNull Throwable t) {
198+
if (mDebug) {
199+
Log.e(LOG_TAG, "" + t.getMessage());
200+
}
201+
}
202+
});
203+
}
204+
143205
@NonNull
144-
public Long getItemId() {
145-
return this.itemId;
206+
public Long getRepositoryId() {
207+
return this.repositoryId;
146208
}
147209
@NonNull
148210
public Long getRunId() {
149211
return this.runId;
150212
}
151213

152-
private void setItemId(@NonNull Long value) {
153-
this.itemId = value;
214+
private void setRepositoryId(@NonNull Long value) {
215+
this.repositoryId = value;
154216
}
155217
private void setRunId(@NonNull Long value) {
156218
this.runId = value;

0 commit comments

Comments
 (0)