Skip to content
Snippets Groups Projects
Commit 2b494cb0 authored by John Carlson's avatar John Carlson
Browse files

Prefs as object

parent 508dc778
No related branches found
No related tags found
No related merge requests found
Pipeline #
Showing
with 43 additions and 42 deletions
Loading
Loading
@@ -52,7 +52,6 @@ open class App : Application() {
lateinit var gitLab: GitLab
lateinit var gitLabRss: GitLabRss
lateinit var picasso: Picasso
lateinit var prefs: Prefs
 
override fun onCreate() {
super.onCreate()
Loading
Loading
@@ -64,7 +63,7 @@ open class App : Application() {
setupLeakCanary()
instance = this
 
prefs = Prefs(this)
Prefs.init(this)
//So that we don't get weird half translations
forceLocale(Locale.ENGLISH)
setupCrashReporting()
Loading
Loading
Loading
Loading
@@ -38,7 +38,7 @@ class ActivityActivity : BaseActivity() {
 
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App.get().prefs.setStartingView(Prefs.STARTING_VIEW_ACTIVITY)
Prefs.setStartingView(Prefs.STARTING_VIEW_ACTIVITY)
setContentView(R.layout.activity_activity)
ButterKnife.bind(this)
 
Loading
Loading
Loading
Loading
@@ -71,7 +71,7 @@ class AddLabelActivity : BaseActivity() {
toolbar.inflateMenu(R.menu.create)
toolbar.setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener { item ->
when (item.itemId) {
R.id.action_add_label -> {
R.id.action_create -> {
Navigator.navigateToAddNewLabel(this@AddLabelActivity, projectId, REQUEST_NEW_LABEL)
return@OnMenuItemClickListener true
}
Loading
Loading
Loading
Loading
@@ -75,7 +75,7 @@ class GroupsActivity : BaseActivity() {
 
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App.get().prefs.setStartingView(Prefs.STARTING_VIEW_GROUPS)
Prefs.setStartingView(Prefs.STARTING_VIEW_GROUPS)
setContentView(R.layout.activity_groups)
ButterKnife.bind(this)
App.bus().register(this)
Loading
Loading
Loading
Loading
@@ -11,6 +11,7 @@ import butterknife.BindView
import butterknife.ButterKnife
import com.commit451.gitlab.App
import com.commit451.gitlab.R
import com.commit451.gitlab.data.Prefs
import com.commit451.gitlab.model.Account
import com.commit451.gitlab.navigation.Navigator
import com.commit451.gitlab.ssl.CustomKeyManager
Loading
Loading
@@ -55,7 +56,7 @@ class LaunchActivity : BaseActivity() {
if (accounts.isEmpty()) {
Navigator.navigateToLogin(this)
finish()
} else if (App.get().prefs.isRequiredDeviceAuth()) {
} else if (Prefs.isRequiredDeviceAuth()) {
showKeyguard()
} else {
if (PRIVATE_KEY_ENABLED) {
Loading
Loading
Loading
Loading
@@ -24,6 +24,7 @@ import com.commit451.gitlab.BuildConfig
import com.commit451.gitlab.R
import com.commit451.gitlab.api.GitLabFactory
import com.commit451.gitlab.api.OkHttpClientFactory
import com.commit451.gitlab.data.Prefs
import com.commit451.gitlab.dialog.HttpLoginDialog
import com.commit451.gitlab.event.LoginEvent
import com.commit451.gitlab.event.ReloadDataEvent
Loading
Loading
@@ -360,7 +361,7 @@ class LoginActivity : BaseActivity() {
progress.visibility = View.GONE
account.user = userFull
account.lastUsed = Date()
App.get().prefs.addAccount(account)
Prefs.addAccount(account)
App.get().setAccount(account)
App.bus().post(LoginEvent(account))
//This is mostly for if projects already exists, then we will reload the data
Loading
Loading
@@ -489,7 +490,7 @@ class LoginActivity : BaseActivity() {
}
 
fun isAlreadySignedIn(url: String, usernameOrEmailOrPrivateToken: String): Boolean {
val accounts = App.get().prefs.getAccounts()
val accounts = Prefs.getAccounts()
for (account in accounts) {
if (account.serverUrl == Uri.parse(url)) {
if (usernameOrEmailOrPrivateToken == account.user.username
Loading
Loading
Loading
Loading
@@ -122,7 +122,7 @@ class ProjectActivity : BaseActivity() {
 
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App.get().prefs.setStartingView(Prefs.STARTING_VIEW_PROJECTS)
Prefs.setStartingView(Prefs.STARTING_VIEW_PROJECTS)
setContentView(R.layout.activity_project)
ButterKnife.bind(this)
var project: Project? = Parcels.unwrap<Project>(intent.getParcelableExtra<Parcelable>(EXTRA_PROJECT))
Loading
Loading
Loading
Loading
@@ -11,8 +11,8 @@ import android.view.ViewGroup
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.commit451.gitlab.App
import com.commit451.gitlab.R
import com.commit451.gitlab.data.Prefs
 
/**
* Settings
Loading
Loading
@@ -52,10 +52,10 @@ class SettingsActivity : BaseActivity() {
}
 
bindPrefs()
switchRequireAuth.setOnCheckedChangeListener { compoundButton, b -> App.get().prefs.setRequiredDeviceAuth(b) }
switchRequireAuth.setOnCheckedChangeListener { compoundButton, b -> Prefs.setRequiredDeviceAuth(b) }
}
 
fun bindPrefs() {
switchRequireAuth.isChecked = App.get().prefs.isRequiredDeviceAuth()
switchRequireAuth.isChecked = Prefs.isRequiredDeviceAuth()
}
}
Loading
Loading
@@ -44,7 +44,7 @@ class TodosActivity : BaseActivity() {
 
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App.get().prefs.setStartingView(Prefs.STARTING_VIEW_TODOS)
Prefs.setStartingView(Prefs.STARTING_VIEW_TODOS)
setContentView(R.layout.activity_todos)
ButterKnife.bind(this)
App.bus().register(this)
Loading
Loading
Loading
Loading
@@ -5,6 +5,7 @@ import android.widget.Toast
import com.commit451.gitlab.App
import com.commit451.gitlab.R
import com.commit451.gitlab.activity.LoginActivity
import com.commit451.gitlab.data.Prefs
import com.commit451.gitlab.model.Account
import com.commit451.gitlab.util.ThreadUtil
import okhttp3.Authenticator
Loading
Loading
@@ -38,7 +39,7 @@ class OpenSignInAuthenticator(private val account: Account) : Authenticator {
Timber.wtf(RuntimeException("Got a 401 and showing sign in for url: " + response.request().url()))
ThreadUtil.postOnMainThread(Runnable {
//Remove the account, so that the user can sign in again
App.get().prefs.removeAccount(account)
Prefs.removeAccount(account)
Toast.makeText(App.get(), R.string.error_401, Toast.LENGTH_LONG)
.show()
val intent = LoginActivity.newIntent(App.get())
Loading
Loading
Loading
Loading
@@ -13,30 +13,28 @@ import java.util.*
/**
* Shared prefs things
*/
class Prefs(context: Context) {
object Prefs {
 
companion object {
val KEY_ACCOUNTS = "accounts"
val KEY_STARTING_VIEW = "starting_view"
val KEY_REQUIRE_DEVICE_AUTH = "require_device_auth"
 
val KEY_ACCOUNTS = "accounts"
val KEY_STARTING_VIEW = "starting_view"
val KEY_REQUIRE_DEVICE_AUTH = "require_device_auth"
const val STARTING_VIEW_PROJECTS = 0
const val STARTING_VIEW_GROUPS = 1
const val STARTING_VIEW_ACTIVITY = 2
const val STARTING_VIEW_TODOS = 3
 
const val STARTING_VIEW_PROJECTS = 0
const val STARTING_VIEW_GROUPS = 1
const val STARTING_VIEW_ACTIVITY = 2
const val STARTING_VIEW_TODOS = 3
@Retention(AnnotationRetention.SOURCE)
@IntDef(STARTING_VIEW_PROJECTS.toLong(), STARTING_VIEW_GROUPS.toLong(), STARTING_VIEW_ACTIVITY.toLong(), STARTING_VIEW_TODOS.toLong())
annotation class StartingView
 
@Retention(AnnotationRetention.SOURCE)
@IntDef(STARTING_VIEW_PROJECTS.toLong(), STARTING_VIEW_GROUPS.toLong(), STARTING_VIEW_ACTIVITY.toLong(), STARTING_VIEW_TODOS.toLong())
annotation class StartingView
}
private val prefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
lateinit private var prefs: SharedPreferences
 
init {
fun init(context : Context) {
if (context !is Application) {
throw IllegalArgumentException("This should be the application context. Not the activity context")
}
prefs = PreferenceManager.getDefaultSharedPreferences(context)
}
 
fun getAccounts(): MutableList<Account> {
Loading
Loading
Loading
Loading
@@ -5,7 +5,7 @@ import android.support.annotation.NonNull;
 
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import com.commit451.gitlab.App;
import com.commit451.gitlab.data.Prefs;
import com.commit451.gitlab.model.api.UserFull;
import com.commit451.gitlab.util.ObjectUtil;
 
Loading
Loading
@@ -25,7 +25,7 @@ public class Account implements Comparable<Account>{
 
@NonNull
public static List<Account> getAccounts() {
List<Account> accounts = new ArrayList<>(App.Companion.get().getPrefs().getAccounts());
List<Account> accounts = new ArrayList<>(Prefs.INSTANCE.getAccounts());
Collections.sort(accounts);
Collections.reverse(accounts);
return accounts;
Loading
Loading
Loading
Loading
@@ -53,7 +53,7 @@ object Navigator {
}
 
fun navigateToStartingActivity(activity: Activity) {
val startingActivity = App.get().prefs.getStartingView()
val startingActivity = Prefs.getStartingView()
when (startingActivity) {
Prefs.STARTING_VIEW_PROJECTS -> navigateToProjects(activity)
Prefs.STARTING_VIEW_GROUPS -> navigateToGroups(activity)
Loading
Loading
Loading
Loading
@@ -22,6 +22,7 @@ import com.commit451.gitlab.activity.GroupsActivity
import com.commit451.gitlab.activity.ProjectsActivity
import com.commit451.gitlab.activity.TodosActivity
import com.commit451.gitlab.adapter.AccountAdapter
import com.commit451.gitlab.data.Prefs
import com.commit451.gitlab.event.CloseDrawerEvent
import com.commit451.gitlab.event.LoginEvent
import com.commit451.gitlab.event.ReloadDataEvent
Loading
Loading
@@ -118,7 +119,7 @@ class LabCoatNavigationView : NavigationView {
}
 
override fun onAccountLogoutClicked(account: Account) {
App.get().prefs.removeAccount(account)
Prefs.removeAccount(account)
val accounts = Account.getAccounts()
 
if (accounts.isEmpty()) {
Loading
Loading
@@ -198,7 +199,7 @@ class LabCoatNavigationView : NavigationView {
}
 
fun setAccounts() {
val accounts = App.get().prefs.getAccounts()
val accounts = Prefs.getAccounts()
Timber.d("Got %s accounts", accounts.size)
Collections.sort(accounts)
Collections.reverse(accounts)
Loading
Loading
@@ -220,7 +221,7 @@ class LabCoatNavigationView : NavigationView {
// in local storage
val account = App.get().getAccount()
account.user = userFull
App.get().prefs.updateAccount(account)
Prefs.updateAccount(account)
bindUser(userFull)
}
})
Loading
Loading
@@ -262,7 +263,7 @@ class LabCoatNavigationView : NavigationView {
Timber.d("Switching to account: %s", account)
account.lastUsed = Date()
App.get().setAccount(account)
App.get().prefs.updateAccount(account)
Prefs.updateAccount(account)
bindUser(account.user)
toggleAccounts()
App.bus().post(ReloadDataEvent())
Loading
Loading
Loading
Loading
@@ -12,9 +12,9 @@ import android.view.View
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
import com.commit451.gitlab.App
import com.commit451.gitlab.R
import com.commit451.gitlab.activity.BaseActivity
import com.commit451.gitlab.data.Prefs
import com.commit451.gitlab.model.Account
import com.commit451.gitlab.model.api.Project
import org.parceler.Parcels
Loading
Loading
@@ -85,7 +85,7 @@ class ProjectFeedWidgetConfigureActivity : BaseActivity() {
}
 
fun loadAccounts() {
val accounts = App.get().prefs.getAccounts()
val accounts = Prefs.getAccounts()
Timber.d("Got %s accounts", accounts.size)
Collections.sort(accounts)
Collections.reverse(accounts)
Loading
Loading
Loading
Loading
@@ -11,9 +11,9 @@ import android.view.View
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
import com.commit451.gitlab.App
import com.commit451.gitlab.R
import com.commit451.gitlab.activity.BaseActivity
import com.commit451.gitlab.data.Prefs
import com.commit451.gitlab.model.Account
import timber.log.Timber
import java.util.*
Loading
Loading
@@ -63,7 +63,7 @@ class UserFeedWidgetConfigureActivity : BaseActivity() {
}
 
fun loadAccounts() {
val accounts = App.get().prefs.getAccounts()
val accounts = Prefs.getAccounts()
Timber.d("Got %s accounts", accounts.size)
Collections.sort(accounts)
Collections.reverse(accounts)
Loading
Loading
Loading
Loading
@@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto">
 
<item
android:id="@+id/action_add_label"
android:id="@+id/action_create"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_add_24dp"
android:title="@string/action_create"/>
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment