Skip to content
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

Fix #179, don't allow to push messages if no apps available #182

Merged
merged 7 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class ApplicationHolder {
private List<Application> state;
private Runnable onUpdate;
private Runnable onUpdateFailed;
private Activity activity;
private ApiClient client;

Expand All @@ -35,11 +36,12 @@ public void request() {

private void onReceiveApps(List<Application> apps) {
state = apps;
onUpdate.run();
if (onUpdate != null) onUpdate.run();
}

private void onFailedApps(ApiException e) {
Utils.showSnackBar(activity, "Could not request applications, see logs.");
if (onUpdateFailed != null) onUpdateFailed.run();
}

public List<Application> get() {
Expand All @@ -49,4 +51,8 @@ public List<Application> get() {
public void onUpdate(Runnable runnable) {
this.onUpdate = runnable;
}

public void onUpdateFailed(Runnable runnable) {
this.onUpdateFailed = runnable;
}
}
24 changes: 23 additions & 1 deletion app/src/main/java/com/github/gotify/sharing/ShareActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
Expand Down Expand Up @@ -46,6 +48,12 @@ public class ShareActivity extends AppCompatActivity {
@BindView(R.id.appSpinner)
Spinner appSpinner;

@BindView(R.id.push_button)
Button pushMessageButton;

@BindView(R.id.missingAppsContainer)
LinearLayout missingAppsInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -73,7 +81,16 @@ protected void onCreate(Bundle savedInstanceState) {
ApiClient client =
ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token());
appsHolder = new ApplicationHolder(this, client);
appsHolder.onUpdate(() -> populateSpinner(appsHolder.get()));
appsHolder.onUpdate(
() -> {
List<Application> apps = appsHolder.get();
populateSpinner(apps);

boolean appsAvailable = !apps.isEmpty();
pushMessageButton.setEnabled(appsAvailable);
missingAppsInfo.setVisibility(appsAvailable ? View.GONE : View.VISIBLE);
});
appsHolder.onUpdateFailed(() -> pushMessageButton.setEnabled(false));
appsHolder.request();
}

Expand All @@ -98,6 +115,11 @@ public void pushMessage(View view) {
} else if (priority.isEmpty()) {
Toast.makeText(this, "Priority should be number.", Toast.LENGTH_LONG).show();
return;
} else if (appIndex == Spinner.INVALID_POSITION) {
// For safety, e.g. loading the apps needs too much time (maybe a timeout) and
// the user tries to push without an app selected.
Toast.makeText(this, "An app must be selected.", Toast.LENGTH_LONG).show();
return;
}

Message message = new Message();
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/icons"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-6h2v6zM13,9h-2L11,7h2v2z"/>
</vector>
190 changes: 116 additions & 74 deletions app/src/main/res/layout/activity_share.xml
Original file line number Diff line number Diff line change
@@ -1,92 +1,134 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="match_parent">

<include
layout="@layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
android:orientation="vertical">

<include
layout="@layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/title"
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/push_title_hint"
android:inputType="text"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">
android:layout_margin="20dp">

<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/push_title_hint"
android:inputType="text"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/content"
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/push_content_hint"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="10" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edtTxtPriority"
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/push_content_hint"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="10" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:hint="@string/push_priority_hint"
android:imeOptions="actionDone"
android:inputType="numberSigned"
android:maxLength="3"
android:text="0" />
</com.google.android.material.textfield.TextInputLayout>
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edtTxtPriority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:hint="@string/push_priority_hint"
android:imeOptions="actionDone"
android:inputType="numberSigned"
android:maxLength="3"
android:text="0" />
</com.google.android.material.textfield.TextInputLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">

<TextView
android:id="@+id/txtAppListDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/appListDescription" />

<Spinner
android:id="@+id/appSpinner"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:spinnerMode="dropdown"
android:textSize="18sp" />
</LinearLayout>

<TextView
android:id="@+id/txtAppListDesc"
android:layout_width="wrap_content"
<LinearLayout
android:id="@+id/missingAppsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/appListDescription" />

<Spinner
android:id="@+id/appSpinner"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:spinnerMode="dropdown"
android:textSize="18sp" />
</LinearLayout>
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:visibility="gone">

<Button
android:id="@+id/push_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginBottom="20dp"
android:layout_marginHorizontal="20dp"
android:text="@string/push_button" />
</LinearLayout>
<ImageView
android:id="@+id/missingAppsIcon"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_weight="0"
app:srcCompat="@drawable/ic_info" />

<Space
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_weight="0" />

<TextView
android:id="@+id/missingAppsText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:text="@string/push_missing_app_info"
android:textAlignment="center"
android:visibility="visible" />
</LinearLayout>

<Button
android:id="@+id/push_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp"
android:text="@string/push_button" />

</LinearLayout>
</ScrollView>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@
<string name="push_title_hint">Title</string>
<string name="push_content_hint">Content</string>
<string name="push_priority_hint">Priority</string>
<string name="push_missing_app_info">There are no applications available on the server to push a message to.</string>
<string name="message_copied_to_clipboard">Content copied to clipboard</string>
</resources>