Commit 1e0a5017 authored by Thomas Markiewicz's avatar Thomas Markiewicz
Browse files

Figured out how to make OnboardingActivity play nice with dependency injection...

Figured out how to make OnboardingActivity play nice with dependency injection and deriving from BaseActivity to pick up injected accountManager
parent 51912b09
Pipeline #50832 passed with stages
in 14 minutes and 29 seconds
......@@ -16,18 +16,21 @@
package com.keylesspalace.tusky
import android.accounts.AccountManager
import android.accounts.AccountManagerFuture
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Color
import android.net.Uri
import android.os.AsyncTask
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.webkit.WebView
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.browser.customtabs.CustomTabsIntent
import com.google.android.material.snackbar.Snackbar
......@@ -46,6 +49,7 @@ import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject
class LoginActivity : BaseActivity(), Injectable {
@Inject
......@@ -101,10 +105,37 @@ class LoginActivity : BaseActivity(), Injectable {
// hardcoding domain "social.librem.one" for now
// also assuming this token IS in fact for social
// TODO: figure out how to tell from code
var token = am.blockingGetAuthToken(a, "social", true)
accountManager.addAccount(token, "social.librem.one")
/*
var token = am.getAuthToken(a, "social", true)
am.getAuthToken(a, "social", null, this, )
accountManager.addAccount(token,"social.librem.one", a.name)
*/
}
startMainActivity()
} else {
/* see: https://www.zoftino.com/how-to-get-auth-token-from-accountmanager-in-android
// start system-wide Librem One login activity
val intent = Intent(this, ZoftinoAccountActivity::class.java)
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, "com.zoftino")
intent.putExtra("addAccount", true)
startActivityForResult(intent, 22)
Toast.makeText(this, "You'll get notification once latest coupon data is downloaded", Toast.LENGTH_LONG).show()
return
*/
/*
NOTE:
remove from the account manager by calling invalidateAuthToken() method.
After removing the token from account manager, you need to call getAuthToken method passing account, account type, and account manager callback to get new token.
*/
}
}
}
......
package com.keylesspalace.tusky
import android.accounts.AccountManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.webkit.WebView
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.keylesspalace.tusky.di.Injectable
class OnboardingActivity : AppCompatActivity() {
companion object {
@JvmStatic
fun getIntent(context: Context): Intent {
val intent = Intent(context, OnboardingActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
return intent
}
}
class OnboardingActivity : BaseActivity(), Injectable {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -53,6 +43,10 @@ class OnboardingActivity : AppCompatActivity() {
}
}
override fun requiresLogin(): Boolean {
return false
}
private fun displayInWebView(id: Int) {
val url = this.getString(id)
val wv = WebView(this)
......@@ -62,4 +56,13 @@ class OnboardingActivity : AppCompatActivity() {
.setPositiveButton(android.R.string.ok, null)
.show()
}
companion object {
@JvmStatic
fun getIntent(context: Context): Intent {
val intent = Intent(context, OnboardingActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
return intent
}
}
}
......@@ -35,6 +35,9 @@ class AccountManager(db: AppDatabase) {
private val accountDao: AccountDao = db.accountDao()
init {
// TODO: should system-wide Librem One accounts be loaded here?
accounts = accountDao.loadAll().toMutableList()
activeAccount = accounts.find { acc ->
......@@ -49,7 +52,7 @@ class AccountManager(db: AppDatabase) {
* @param accessToken the access token for the new account
* @param domain the domain of the accounts Mastodon instance
*/
fun addAccount(accessToken: String, domain: String) {
fun addAccount(accessToken: String, domain: String, username: String = "") {
activeAccount?.let {
it.isActive = false
......@@ -60,7 +63,8 @@ class AccountManager(db: AppDatabase) {
val maxAccountId = accounts.maxBy { it.id }?.id ?: 0
val newAccountId = maxAccountId + 1
activeAccount = AccountEntity(id = newAccountId, domain = domain.toLowerCase(), accessToken = accessToken, isActive = true)
activeAccount = AccountEntity(id = newAccountId, username = username, domain = domain.toLowerCase(), accessToken = accessToken, isActive = true)
}
......@@ -83,6 +87,8 @@ class AccountManager(db: AppDatabase) {
*/
fun logActiveAccountOut(): AccountEntity? {
// TODO: also log out of the system-wide Librem One social account here
if (activeAccount == null) {
return null
} else {
......
......@@ -71,6 +71,9 @@ abstract class ActivitiesModule {
@ContributesAndroidInjector
abstract fun contributesLoginActivity(): LoginActivity
@ContributesAndroidInjector
abstract fun contributesOnboardingActivity(): OnboardingActivity
@ContributesAndroidInjector
abstract fun contributesSplashActivity(): SplashActivity
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment