-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathnavigation.rb
More file actions
146 lines (125 loc) · 3.73 KB
/
navigation.rb
File metadata and controls
146 lines (125 loc) · 3.73 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class UI::Navigation
attr_reader :root_screen
#In pixels
def self.bar_height
@bar_height ||= begin
value = Android::Util::TypedValue.new
if UI.context.theme.resolveAttribute(Android::R::Attr::ActionBarSize, value, true)
Android::Util::TypedValue.complexToDimensionPixelSize(value.data, UI::context.resources.displayMetrics)
else
resource_id = UI.context.resources.getIdentifier('action_bar_default_height', 'dimen', 'android')
if resource_id
UI.context.resources.getDimensionPixelSize(resource_id)
else
0
end
end
end
end
def initialize(root_screen)
@root_screen = root_screen
@root_screen.navigation = self
@current_screens = [@root_screen]
end
def screen
@current_screens.last
end
def hide_bar
bar = UI.context.supportActionBar
if bar.isShowing
bar.hide
Task.after 0.05 do
bar_height = bar.height>0 ? bar.height : self.class.bar_height
@current_screens.each do |e|
e.view.height += (bar_height / UI.density)
e.view.update_layout
end
end
end
end
def show_bar
bar = UI.context.supportActionBar
if !bar.isShowing
bar.show
Task.after 0.05 do
bar_height = bar.height>0 ? bar.height : self.class.bar_height
@current_screens.each do |e|
e.view.height -= (bar_height / UI.density)
e.view.update_layout
end
end
end
end
def title=(title)
UI.context.supportActionBar.title = title
end
def bar_color=(color)
UI.context.supportActionBar.backgroundDrawable = Android::Graphics::Drawable::ColorDrawable.new(UI::Color(color).proxy)
end
def items=(items)
fragment = @current_screens.last.proxy
has_menu = false
has_menu |= (fragment._buttons = items[:buttons])
has_menu |= (fragment._options_menu_items = items[:options_menu_items])
has_menu |= (UI.context.supportActionBar.displayHomeAsUpEnabled = (items[:home_button_enabled] or false))
fragment.hasOptionsMenu = has_menu
end
def push(screen, animated=true)
@stack_change_reason = :push
screen.navigation = self
fragment = screen.proxy
fragment._animate = animated ? :slide : false
current_fragment = @current_screens.last.proxy
current_fragment._animate = animated ? :fade : false
transaction = proxy.beginTransaction
transaction.hide(current_fragment)
transaction.add(UI.context.findViewById(Android::R::Id::Content).id, fragment, nil)
transaction.addToBackStack(nil)
transaction.commitAllowingStateLoss
@current_screens << screen
end
def pop(animated=true)
if @current_screens.size > 1
@stack_change_reason = :pop
current_screen = @current_screens.pop
current_screen.proxy._animate = animated ? :slide : false
previous_screen = @current_screens.last
previous_screen.proxy._animate = animated ? :fade : false
previous_screen.before_on_show
proxy.popBackStack
previous_screen.on_show
current_screen
else
nil
end
end
def proxy
@proxy ||= begin
manager = UI.context.fragmentManager
manager.addOnBackStackChangedListener(FlowFragmentManagerStackChangedListener.new(self))
manager
end
end
def stack_changed
case @stack_change_reason
when :push
when :pop
else
@current_screens.pop
previous_screen = @current_screens.last
if previous_screen
previous_screen.before_on_show
previous_screen.on_show
end
end
@stack_change_reason = nil
end
end
class FlowFragmentManagerStackChangedListener
def initialize(navigation)
@navigation = navigation
end
def onBackStackChanged
@navigation.stack_changed
end
end