aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentContract.kt
blob: 10c612c506d2fe0c07c79be74ae354b5a1a5b04c (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.fragments

import com.pitchedapps.frost.contracts.FrostContentContainer
import com.pitchedapps.frost.contracts.FrostContentCore
import com.pitchedapps.frost.contracts.FrostContentParent
import com.pitchedapps.frost.contracts.MainActivityContract
import com.pitchedapps.frost.contracts.MainFabContract
import com.pitchedapps.frost.views.FrostRecyclerView
import kotlinx.coroutines.channels.ReceiveChannel

/**
 * Created by Allan Wang on 2017-11-07.
 */

interface FragmentContract : FrostContentContainer {

    val content: FrostContentParent?

    /**
     * Defines whether the fragment is valid in the viewpager
     * or if it needs to be recreated
     * May be called from any thread to toggle status.
     * Note that calls beyond the fragment lifecycle will be ignored
     */
    var valid: Boolean

    /**
     * Helper to retrieve the core from [content]
     */
    val core: FrostContentCore?
        get() = content?.core

    /**
     * Specifies position in Activity's viewpager
     */
    val position: Int

    /**
     * Specifies whether if current load
     * will be fragment's first load
     *
     * Defaults to true
     */
    var firstLoad: Boolean

    /**
     * Called when the fragment is first visible
     * Typically, if [firstLoad] is true,
     * the fragment should call [reload] and make [firstLoad] false
     */
    fun firstLoadRequest()

    fun updateFab(contract: MainFabContract)

    /**
     * Single callable action to be executed upon creation
     * Note that this call is not guaranteed
     */
    fun post(action: (fragment: FragmentContract) -> Unit)

    /**
     * Call whenever a fragment is attached so that it may listen
     * to activity emissions.
     * Returns a means of closing the listener, which can be called from [detachMainObservable]
     */
    fun attachMainObservable(contract: MainActivityContract): ReceiveChannel<Int>

    /**
     * Call when fragment is detached so that any existing
     * observable is disposed
     */
    fun detachMainObservable()

    /*
     * -----------------------------------------
     * Delegates
     * -----------------------------------------
     */

    fun onBackPressed(): Boolean

    fun onTabClick()
}

interface RecyclerContentContract {

    fun bind(recyclerView: FrostRecyclerView)

    /**
     * Completely handle data reloading, within a non-ui thread
     * The progress function allows optional emission of progress values (between 0 and 100)
     * and can be called from any thread.
     * Returns [true] for success, [false] otherwise
     */
    suspend fun reload(progress: (Int) -> Unit): Boolean
}