aboutsummaryrefslogtreecommitdiffstats
path: root/lib/state/state.go
blob: 4943102970df5e3b3c98f4965caed6a68a2b5941 (plain) (blame)
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
package state

import (
	"fmt"
)

type AccountState struct {
	Connected    bool
	connActivity string
	passthrough  bool
	folders      map[string]*folderState
}

type folderState struct {
	Search         string
	Filter         string
	FilterActivity string
	Sorting        bool
	Threading      bool
}

func (s *AccountState) folderState(folder string) *folderState {
	if s.folders == nil {
		s.folders = make(map[string]*folderState)
	}
	if _, ok := s.folders[folder]; !ok {
		s.folders[folder] = &folderState{}
	}
	return s.folders[folder]
}

type SetStateFunc func(s *AccountState, folder string)

func SetConnected(state bool) SetStateFunc {
	return func(s *AccountState, folder string) {
		s.connActivity = ""
		s.Connected = state
	}
}

func ConnectionActivity(desc string) SetStateFunc {
	return func(s *AccountState, folder string) {
		s.connActivity = desc
	}
}

func SearchFilterClear() SetStateFunc {
	return func(s *AccountState, folder string) {
		s.folderState(folder).Search = ""
		s.folderState(folder).FilterActivity = ""
		s.folderState(folder).Filter = ""
	}
}

func FilterActivity(str string) SetStateFunc {
	return func(s *AccountState, folder string) {
		s.folderState(folder).FilterActivity = str
	}
}

func FilterResult(str string) SetStateFunc {
	return func(s *AccountState, folder string) {
		s.folderState(folder).FilterActivity = ""
		s.folderState(folder).Filter = concatFilters(s.folderState(folder).Filter, str)
	}
}

func concatFilters(existing, next string) string {
	if existing == "" {
		return next
	}
	return fmt.Sprintf("%s && %s", existing, next)
}

func Search(desc string) SetStateFunc {
	return func(s *AccountState, folder string) {
		s.folderState(folder).Search = desc
	}
}

func Sorting(on bool) SetStateFunc {
	return func(s *AccountState, folder string) {
		s.folderState(folder).Sorting = on
	}
}

func Threading(on bool) SetStateFunc {
	return func(s *AccountState, folder string) {
		s.folderState(folder).Threading = on
	}
}

func Passthrough(on bool) SetStateFunc {
	return func(s *AccountState, folder string) {
		s.passthrough = on
	}
}