From 0aafe6ec6b5d53b310c5672cf30ea0a932a71cdc Mon Sep 17 00:00:00 2001 From: stom79 Date: Thu, 20 Dec 2018 18:17:08 +0100 Subject: [PATCH] Fix #660 - avoid instance = null during API fetches --- .../java/fr/gouv/etalab/mastodon/client/API.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 8250f7fbf..27c999206 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -16,6 +16,7 @@ package fr.gouv.etalab.mastodon.client; import android.content.Context; import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; import org.json.JSONArray; import org.json.JSONException; @@ -56,6 +57,8 @@ import fr.gouv.etalab.mastodon.client.Entities.Results; import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.client.Entities.Tag; import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.sqlite.AccountDAO; +import fr.gouv.etalab.mastodon.sqlite.Sqlite; /** @@ -120,7 +123,14 @@ public class API { tootPerPage = sharedpreferences.getInt(Helper.SET_TOOTS_PER_PAGE, 40); accountPerPage = sharedpreferences.getInt(Helper.SET_ACCOUNTS_PER_PAGE, 40); notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 15); - this.instance = Helper.getLiveInstance(context); + if( Helper.getLiveInstance(context) != null) + this.instance = Helper.getLiveInstance(context); + else { + SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + Account account = new AccountDAO(context, db).getAccountByID(userId); + this.instance = account.getInstance().trim(); + } this.prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); apiResponse = new APIResponse(); APIError = null;