Skip to content

Commit

Permalink
Some bug fixes and version 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bplaat committed Aug 21, 2020
1 parent d02b0c3 commit 81fe6c8
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 63 deletions.
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.plaatsoft.warquest3" android:versionCode="4" android:versionName="2.0">
package="nl.plaatsoft.warquest3" android:versionCode="21" android:versionName="2.1">

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="29" />

Expand Down
8 changes: 6 additions & 2 deletions res/layout/item_account.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
<TextView android:id="@+id/account_nickname_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp" />
android:layout_marginBottom="4dp"
android:singleLine="true"
android:ellipsize="end" />

<TextView android:id="@+id/account_info_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/secondary_text_color" />
android:textColor="@color/secondary_text_color"
android:singleLine="true"
android:ellipsize="end" />
</LinearLayout>

<ImageView android:id="@+id/account_remove_button"
Expand Down
4 changes: 3 additions & 1 deletion res/layout/item_account_button.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
android:layout_weight="1"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:gravity="center_vertical" />
android:gravity="center_vertical"
android:singleLine="true"
android:ellipsize="end" />
</LinearLayout>
2 changes: 1 addition & 1 deletion src/nl/plaatsoft/warquest3/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void onClick(View view) {
// Login button handler
((Button)findViewById(R.id.login_login_button)).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Create request url
// Create request url for login request
String url = null;
try {
url = Config.WARQUEST_URL + "/api/auth/login?key=" + Config.WARQUEST_API_KEY +
Expand Down
161 changes: 103 additions & 58 deletions src/nl/plaatsoft/warquest3/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.net.URLEncoder;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;

// The main (webview) activity
public class MainActivity extends Activity {
public class MainActivity extends Activity implements FetchDataTask.OnLoadListener {
private static final int OPEN_SETTINGS_ACTIVITY = 1;

private SharedPreferences settings;
private Account activeAccount;
private WebView webview;

// Supress warnings to get deprecated old settings
@SuppressWarnings("deprecation")
public SharedPreferences getOldSettings() {
return android.preference.PreferenceManager.getDefaultSharedPreferences(this);
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Expand Down Expand Up @@ -119,45 +126,77 @@ public void onReceivedError(WebView view, WebResourceRequest webResourceRequest,
}
});

// Load account
try {
JSONArray jsonAccounts = new JSONArray(settings.getString("accounts", "[]"));
// Convert old settings to new format
SharedPreferences oldSettings = getOldSettings();
if (oldSettings.contains("username")) {
// Create request url for login request
String url = null;
try {
url = Config.WARQUEST_URL + "/api/auth/login?key=" + Config.WARQUEST_API_KEY +
"&username=" + URLEncoder.encode(oldSettings.getString("username", null), "UTF-8") +
"&password=" + URLEncoder.encode(oldSettings.getString("password", null), "UTF-8");
} catch (Exception exception) {
exception.printStackTrace();
}

// Do login request and save and open
FetchDataTask.fetchData(this, url, false, false, this);

// When there are no accounts create new one
if (jsonAccounts.length() == 0) {
createNewAccountAndOpen();
// Set and save old zoom when disabled
boolean oldZoom = oldSettings.getBoolean("zoom", true);
if (oldZoom == false) {
webSettings.setBuiltInZoomControls(false);

SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putBoolean("zoom", false);
settingsEditor.apply();
}
}

// Else get selected account and load webview
else {
// Get selected account id
long selectedAccountId = settings.getLong("selected_account_id", 0);
if (selectedAccountId == 0) {
selectedAccountId = Account.fromJson(jsonAccounts.getJSONObject(0)).getId();
// When no old account convert
else {
// Load account
try {
JSONArray jsonAccounts = new JSONArray(settings.getString("accounts", "[]"));

// When there are no accounts create new one
if (jsonAccounts.length() == 0) {
// Do register request and save and open
FetchDataTask.fetchData(this, Config.WARQUEST_URL + "/api/auth/register?key=" + Config.WARQUEST_API_KEY, false, false, this);
}

// Find selected account
for (int i = 0; i < jsonAccounts.length(); i++) {
Account account =Account.fromJson(jsonAccounts.getJSONObject(i));
if (account.getId() == selectedAccountId) {
activeAccount = account;
break;
// Else get selected account and load webview
else {
// Get selected account id
long selectedAccountId = settings.getLong("selected_account_id", 0);
if (selectedAccountId == 0) {
selectedAccountId = Account.fromJson(jsonAccounts.getJSONObject(0)).getId();
}
}

// Load the webview
loadWebview(Config.WARQUEST_URL + "/");
// Find selected account
for (int i = 0; i < jsonAccounts.length(); i++) {
Account account =Account.fromJson(jsonAccounts.getJSONObject(i));
if (account.getId() == selectedAccountId) {
activeAccount = account;
break;
}
}

// Load the webview
loadWebview(Config.WARQUEST_URL + "/");
}
}
}

// When there is an error reading the accounts json create a new account
catch (Exception exception) {
exception.printStackTrace();
// When there is an error reading the accounts json create a new account
catch (Exception exception) {
exception.printStackTrace();

createNewAccountAndOpen();
// Do register request and save and open
FetchDataTask.fetchData(this, Config.WARQUEST_URL + "/api/auth/register?key=" + Config.WARQUEST_API_KEY, false, false, this);
}
}

// Check rating
// Check rating alert
RatingAlert.check(MainActivity.this);
}

Expand Down Expand Up @@ -206,7 +245,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
catch (Exception exception) {
exception.printStackTrace();

createNewAccountAndOpen();
// Do register request and save and open
FetchDataTask.fetchData(this, Config.WARQUEST_URL + "/api/auth/register?key=" + Config.WARQUEST_API_KEY, false, false, this);
}
}
}
Expand All @@ -227,38 +267,43 @@ public void onBackPressed() {
}
}

// Create a new account and open the webview
private void createNewAccountAndOpen() {
FetchDataTask.fetchData(this, Config.WARQUEST_URL + "/api/auth/register?key=" + Config.WARQUEST_API_KEY, false, false, new FetchDataTask.OnLoadListener() {
public void onLoad(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
if (jsonResponse.getBoolean("success")) {
// Set the active account
activeAccount = Account.fromJsonApiResponse(jsonResponse);

// Add account to json accounts
JSONArray jsonAccounts = new JSONArray();
jsonAccounts.put(activeAccount.toJson());

// Save data
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putString("accounts", jsonAccounts.toString());
settingsEditor.putLong("selected_account_id", activeAccount.getId());
settingsEditor.apply();

// Reload the webview
loadWebview(Config.WARQUEST_URL + "/");
return;
}
} catch (Exception exception) {
exception.printStackTrace();
// Create a new account and open the webview onload
public void onLoad(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
if (jsonResponse.getBoolean("success")) {
// Set the active account
activeAccount = Account.fromJsonApiResponse(jsonResponse);

// Add account to json accounts
JSONArray jsonAccounts = new JSONArray();
jsonAccounts.put(activeAccount.toJson());

// Save data
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putString("accounts", jsonAccounts.toString());
settingsEditor.putLong("selected_account_id", activeAccount.getId());
settingsEditor.apply();

// Reload the webview
loadWebview(Config.WARQUEST_URL + "/");

// Remove old settings when we where converting
SharedPreferences oldSettings = getOldSettings();
if (oldSettings.contains("username")) {
SharedPreferences.Editor oldSettingsEditor = oldSettings.edit();
oldSettingsEditor.clear();
oldSettingsEditor.apply();
}

// When an error occurt or success is false show an error message
Toast.makeText(MainActivity.this, getResources().getString(R.string.register_error_message), Toast.LENGTH_SHORT).show();
return;
}
});
} catch (Exception exception) {
exception.printStackTrace();
}

// When an error occurt or success is false show an error message
Toast.makeText(MainActivity.this, getResources().getString(R.string.register_error_message), Toast.LENGTH_SHORT).show();
}

// Load url in webview
Expand Down
Binary file modified warquest.apk
Binary file not shown.

0 comments on commit 81fe6c8

Please sign in to comment.