aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/events/FbAccountEvent.kt
blob: 538a79199bb96d08cca6215cb747d3aca2df727c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.pitchedapps.frost.events

import com.mikepenz.materialdrawer.AccountHeader
import com.mikepenz.materialdrawer.model.ProfileDrawerItem
import com.pitchedapps.frost.dbflow.CookieModel
import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL
import com.pitchedapps.frost.facebook.UsernameFetcher
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.web.FrostWebViewCore

/**
 * Created by Allan Wang on 2017-06-02.
 *
 * An emitter for whenever a change occurs relating to the active facebook account
 * All subscribers will call one of the execute methods below so the logic is handled within this class
 * [data]   [CookieModel] content
 * [sender] Webview position that sent the event; or -1 otherwise
 * [flag]   See companion object
 */
class FbAccountEvent(val data: CookieModel, val sender: Int, val flag: Int) {

    init {
        L.d(toString())
    }

    companion object {
        const val FLAG_LOGOUT = -2
        const val FLAG_RESET = -1
        const val FLAG_NEW = 0
        const val FLAG_SWITCH = 1
        const val FLAG_USER_NAME = 2
    }

    fun execute(webView: FrostWebViewCore) {
        if (sender != -1 && sender == webView.position) return
        when (flag) {
            FLAG_LOGOUT, FLAG_RESET, FLAG_NEW, FLAG_SWITCH -> webView.loadBaseUrl()
        }
    }

    /**
     * If new user id is found; create an account header and fetch the username
     * If the username is found and the current account is nameless, set the name
     * Ignore other flags
     */
    fun execute(accountHeader: AccountHeader) {
        when (flag) {
            FLAG_NEW -> {
                val profile = ProfileDrawerItem()
                        .withName(data.name)
                        .withIcon(PROFILE_PICTURE_URL(data.id))
                accountHeader.addProfile(profile, 0)
                accountHeader.setActiveProfile(profile, true)
                if (data.name == null)
                    UsernameFetcher.fetch(data, sender)
            }
            FLAG_USER_NAME -> {
                if (accountHeader.activeProfile.name == null)
                    accountHeader.activeProfile.withName(data.name)
            }
            FLAG_LOGOUT -> {

            }
        }
    }
}