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

Add integration for Tap/Tab #568

Merged
merged 21 commits into from
May 25, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add Tab requests/transactions
niknetniko committed May 25, 2022

Verified

This commit was signed with the committer’s verified signature.
booc0mtaco Holloway
commit d03d26ea8c932355cb1c7c1de2eaceb292cef4cf
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="false"
android:usesCleartextTraffic="true"
android:theme="@style/Hydra.Material"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">

@@ -156,9 +157,14 @@

<activity
android:name=".wpi.WpiActivity"
android:label="@string/wpi_api_key_management_title"
android:launchMode="singleTop"
android:parentActivityName=".MainActivity"/>

<activity
android:name=".wpi.tab.create.FormActivity"
android:label="@string/wpi_tab_form_title"
android:parentActivityName=".wpi.WpiActivity" />

<!-- Urgent stuff -->
<!-- This is a music service without sensitive data and thus does not require permissions -->
<service
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ public interface Endpoints {
String ZEUS_V2 = "https://hydra.ugent.be/api/2.0/";

String TAP = "https://tap.zeus.gent/";
String TAB = "https://tab.zeus.gent/";
String TAB = "http://localhost:3000/";

String LIBRARY = "https://widgets.lib.ugent.be/";
}
Original file line number Diff line number Diff line change
@@ -27,9 +27,9 @@
/**
* @author Niko Strijbol
*/
class InvalidFormatException extends RequestException {
public class InvalidFormatException extends RequestException {

InvalidFormatException(String message, Throwable cause) {
public InvalidFormatException(String message, Throwable cause) {
super(message, cause);
}

Original file line number Diff line number Diff line change
@@ -37,14 +37,11 @@
import java.util.concurrent.TimeUnit;

import be.ugent.zeus.hydra.common.arch.data.BaseLiveData;
import be.ugent.zeus.hydra.common.reporting.Reporting;
import be.ugent.zeus.hydra.common.reporting.Tracker;
import be.ugent.zeus.hydra.common.request.Request;
import be.ugent.zeus.hydra.common.request.Result;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.Moshi;
import okhttp3.*;
import okhttp3.CacheControl;
import okhttp3.Response;

/**
* Common implementation base for requests that are network requests. This request provides built-in caching on the
@@ -64,16 +61,13 @@
* @author Niko Strijbol
*/
@SuppressWarnings("WeakerAccess")
public abstract class JsonOkHttpRequest<D> implements Request<D> {
public abstract class JsonOkHttpRequest<D> extends OkHttpRequest<D> {

private static final String TAG = "JsonOkHttpRequest";

private static final String ALLOW_STALENESS = "be.ugent.zeus.hydra.data.staleness";

private final Moshi moshi;
private final OkHttpClient client;
private final Type typeToken;
private final Tracker tracker;

/**
* Construct a new request. As this constructor is not type-safe, it must only be used internally.
@@ -82,10 +76,8 @@ public abstract class JsonOkHttpRequest<D> implements Request<D> {
* @param token The type token of the return type.
*/
JsonOkHttpRequest(@NonNull Context context, @NonNull Type token) {
this.moshi = InstanceProvider.getMoshi();
this.client = InstanceProvider.getClient(context);
super(context);
this.typeToken = token;
this.tracker = Reporting.getTracker(context);
}

/**
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2021 The Hydra authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package be.ugent.zeus.hydra.common.network;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.Moshi;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.UnknownServiceException;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import be.ugent.zeus.hydra.common.arch.data.BaseLiveData;
import be.ugent.zeus.hydra.common.reporting.Reporting;
import be.ugent.zeus.hydra.common.reporting.Tracker;
import be.ugent.zeus.hydra.common.request.Request;
import be.ugent.zeus.hydra.common.request.Result;
import okhttp3.CacheControl;
import okhttp3.OkHttpClient;
import okhttp3.Response;

/**
* Common implementation for requests using OkHttp.
*
* @author Niko Strijbol
*/
@SuppressWarnings("WeakerAccess")
public abstract class OkHttpRequest<D> implements Request<D> {

private static final String TAG = "OkHttpRequest";

protected final Moshi moshi;
protected final OkHttpClient client;
protected final Tracker tracker;

/**
* Construct a new request. As this constructor is not type-safe, it must only be used internally.
*
* @param context The context.
*/
protected OkHttpRequest(@NonNull Context context) {
this.moshi = InstanceProvider.getMoshi();
this.client = InstanceProvider.getClient(context);
this.tracker = Reporting.getTracker(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2022 Niko Strijbol
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package be.ugent.zeus.hydra.common.ui;

import android.text.Editable;
import android.text.TextWatcher;

/**
* @author Niko Strijbol
*/
public interface SimpleTextWatcher extends TextWatcher {

@Override
default void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing.
}

@Override
default void afterTextChanged(Editable s) {
// Do nothing.
}

@Override
default void onTextChanged(CharSequence s, int start, int before, int count) {
onTextChanged(s.toString());
}

void onTextChanged(String newText);
}
44 changes: 38 additions & 6 deletions app/src/main/java/be/ugent/zeus/hydra/wpi/WpiActivity.java
Original file line number Diff line number Diff line change
@@ -27,9 +27,7 @@
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.*;
import androidx.activity.result.ActivityResult;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -50,6 +48,7 @@
import be.ugent.zeus.hydra.wpi.account.AccountManager;
import be.ugent.zeus.hydra.wpi.account.ApiKeyManagementActivity;
import be.ugent.zeus.hydra.wpi.account.CombinedUserViewModel;
import be.ugent.zeus.hydra.wpi.tab.create.FormActivity;

/**
* Activity that allows you to manage your API key.
@@ -62,13 +61,26 @@
public class WpiActivity extends BaseActivity<ActivityWpiBinding> {

private static final String TAG = "ApiKeyManagementActivit";
private static final int ACTIVITY_API_MANAGEMENT = 963;
private static final int ACTIVITY_DO_REFRESH = 963;

private CombinedUserViewModel combinedUserViewModel;
private WpiPagerAdapter pageAdapter;

private final NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
private final NumberFormat decimalFormatter = NumberFormat.getNumberInstance();

private final ViewPager2.OnPageChangeCallback callback = new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
if (position == 0) {
binding.tabFab.hide();
binding.tapFab.show();
} else if (position == 1) {
binding.tapFab.hide();
binding.tabFab.show();
}
}
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -88,6 +100,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}
});
mediator.attach();

binding.tabFab.setOnClickListener(v -> {
Intent intent = new Intent(WpiActivity.this, FormActivity.class);
startActivityForResult(intent, ACTIVITY_DO_REFRESH);
});

combinedUserViewModel = new ViewModelProvider(this).get(CombinedUserViewModel.class);
combinedUserViewModel.getData().observe(this, PartialErrorObserver.with(this::onError));
@@ -99,6 +116,18 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}));
}

@Override
protected void onStart() {
super.onStart();
binding.viewPager.registerOnPageChangeCallback(callback);
}

@Override
protected void onStop() {
super.onStop();
binding.viewPager.unregisterOnPageChangeCallback(callback);
}

private void onError(Throwable throwable) {
Log.e(TAG, "Error while getting data.", throwable);
// TODO: better error message.
@@ -118,7 +147,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.action_manage_login) {
Intent intent = new Intent(this, ApiKeyManagementActivity.class);
startActivityForResult(intent, ACTIVITY_API_MANAGEMENT);
startActivityForResult(intent, ACTIVITY_DO_REFRESH);
}
return super.onOptionsItemSelected(item);
}
@@ -128,7 +157,10 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == ACTIVITY_API_MANAGEMENT && resultCode == Activity.RESULT_OK) {
Log.i(TAG, "onActivityResult: result");

if (requestCode == ACTIVITY_DO_REFRESH && resultCode == Activity.RESULT_OK) {
Log.i(TAG, "onActivityResult: refreshing for result...");
combinedUserViewModel.onRefresh();
if (pageAdapter != null) {
pageAdapter.notifyDataSetChanged();
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@
import androidx.viewpager2.adapter.FragmentStateAdapter;

import be.ugent.zeus.hydra.common.ui.AdapterOutOfBoundsException;
import be.ugent.zeus.hydra.wpi.tab.transaction.TransactionFragment;
import be.ugent.zeus.hydra.wpi.tab.list.TransactionFragment;
import be.ugent.zeus.hydra.wpi.tap.product.ProductFragment;

/**
Original file line number Diff line number Diff line change
@@ -22,10 +22,12 @@

package be.ugent.zeus.hydra.wpi.account;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MenuItem;
import androidx.annotation.Nullable;

import java.util.Objects;
@@ -69,6 +71,22 @@ protected void onStop() {
binding.apiUsername.removeTextChangedListener(this);
}

@Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
super.onBackPressed();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
}
return super.onOptionsItemSelected(item);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing.
Loading