aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/data/Account.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence/data/Account.kt')
-rw-r--r--app/src/main/java/net/lacolaco/smileessence/data/Account.kt134
1 files changed, 134 insertions, 0 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/data/Account.kt b/app/src/main/java/net/lacolaco/smileessence/data/Account.kt
new file mode 100644
index 00000000..6dc8262c
--- /dev/null
+++ b/app/src/main/java/net/lacolaco/smileessence/data/Account.kt
@@ -0,0 +1,134 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2012-2014 lacolaco.net
+ *
+ * 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 net.lacolaco.smileessence.data
+
+import com.github.gfx.android.orma.annotation.Column
+import com.github.gfx.android.orma.annotation.PrimaryKey
+import com.github.gfx.android.orma.annotation.Table
+import net.lacolaco.smileessence.data.Account.Companion.cache
+import net.lacolaco.smileessence.entity.User
+import twitter4j.Twitter
+import twitter4j.TwitterFactory
+import twitter4j.TwitterStream
+import twitter4j.TwitterStreamFactory
+import twitter4j.auth.AccessToken
+import twitter4j.conf.ConfigurationBuilder
+
+import java.util.*
+
+@Table
+class Account {
+ @PrimaryKey
+ var userId: Long = 0
+ @Column
+ var screenName: String? = null
+ @Column
+ var oauthToken: String? = null
+ @Column
+ var oauthTokenSecret: String? = null
+ @Column(defaultExpr = "0")
+ var themeIndex: Int = 0
+
+ val user: User by lazy {
+ val _user = User.fetch(userId) ?: User._makeSkeleton(userId, screenName!!)
+ _user.addObserver(this) { objs ->
+ if (screenName!! != _user.screenName) {
+ screenName = _user.screenName
+ relation().upserter().execute(this)
+ }
+ }
+ _user
+ }
+
+ val twitter: Twitter
+ get() {
+ val cb = ConfigurationBuilder()
+ cb.setTweetModeExtended(true)
+ val twitter = TwitterFactory(cb.build()).instance
+ twitter.oAuthAccessToken = AccessToken(oauthToken, oauthTokenSecret)
+ return twitter
+ }
+
+ val twitterStream: TwitterStream
+ get() {
+ val stream = TwitterStreamFactory().instance
+ stream.oAuthAccessToken = AccessToken(oauthToken, oauthTokenSecret)
+ return stream
+ }
+
+
+ companion object {
+ private var cache: MutableMap<Long, Account>? = null
+
+ @Synchronized
+ operator fun get(i: Long): Account {
+ if (cache == null) {
+ throw IllegalStateException("[BUG] Load first")
+ }
+ return cache!![i] ?: throw IllegalStateException("[BUG] Account with userId == $i not found")
+ }
+
+ @Synchronized
+ fun all(): List<Account> {
+ return ArrayList(cache!!.values)
+ }
+
+ @Synchronized
+ fun load() {
+ cache = LinkedHashMap()
+ for (item in relation().selector())
+ cache!!.put(item.userId, item)
+ }
+
+ private fun relation(): Account_Relation {
+ return OrmaHolder.orma.relationOfAccount()
+ }
+
+ @Synchronized
+ fun register(token: String, tokenSecret: String, userId: Long, screenName: String): Account {
+ val account = all().find { a -> a.userId == userId } ?: Account()
+ account.userId = userId
+ account.screenName = screenName
+ account.oauthToken = token
+ account.oauthTokenSecret = tokenSecret
+ account.themeIndex = 0
+
+ relation().upserter().execute(account)
+
+ cache!!.put(account.userId, account)
+
+ return account
+ }
+
+ @Synchronized
+ fun unregister(id: Long): Account? {
+ val account = cache!!.remove(id)
+ if (account != null) {
+ relation().deleter().userIdEq(id).execute()
+ }
+ return account
+ }
+ }
+} \ No newline at end of file