How to get the current View in RenderController on Android backbutton event - famo.us

How can we handle back button press in android, as of now I am catching backButtonPress event with following code :
document.addEventListener("backbutton", function({
document.location.reload(true);
},false);
I am trying improve this implementation by pushing renderController and the current view it is showing to the array and popping them on press of a back button. But the problem here is, I am not able to get current view that render controller is showing.
Is there any way we can get the current render node the RenderController is showing?
Is their any other way I can handle android back button press event?

There is not a way to get the current renderable from the RenderController
You could keep track of the renderable you passed using the show method.
OR
You could extend the RenderController
var RenderController = require("famous/views/RenderController");
RenderController.prototype.getRenderNode = function getRenderNode() {
return this._renderables[this._showing];
}
Later you can call the method and get the render node passed with the show method.
var node = renderController.getRenderNode();

Related

Dynamic theme color at run time jetpack compose

I'm new to Jetpack Compose, so I'm struggling to implement a feature which is dynamic colors (and font, size,... but I think they are the same so I'll just focus on color) at run time from backend. I'll let the app the some default colors, and a whole default splash screen just to load the colors setting from the backend. In case the API request failed, it would use the last succeeded requested colors or just the default color.
Tutorials I found round the internet was just about changing the dark/light theme at run time, not changing a specific color in the color pack. In those tutorials, the color is defined in Colors.kt file which is not a composable or class or object, ...
I imagine the color within lightColors or darkColors would be something like this.
return lightColors(
primary = Color(android.graphics.Color.parseColor("#" + dynamicColorMap["One"])),
...
}
And when dynamicColorMap changes in the splashscreen, all screen later will have reference to the new value, but I don't know how to update its variable outside of a composable.
I thought of using DB to store the colors, but getting the data from DB is async, so it cannot be query in the default Colors.kt like var colorOne = DBManager.getColor("One"), I can run the async task in my splash screen before changing to the next screen but then the problem again is how to have a global state that my theme composable wrapper can have access to on every screen?
I just don't know where to start for these case.
Thank you for your time
EDIT:
I currently having the project structured in MVVM. For now, only one activity (MainActivity) is present, and inside that activity, the splash screen component or home screen or login screen,... are being navigated. So is it a good practice to create a viewmodel for the mainactivity screen, that can holds the color state for the theme?
Thanks #Maciej Ciemiega for the suggestion. I ended up structure my code like that.
In my MainActivity.kt I create a viewmodel for it.
val mainActivityViewModel by viewModels<MainActivityViewModel>()
MyTheme(mainActivityViewModel = mainActivityViewModel) {
initNavigationController(navController)
Surface(color = MaterialTheme.colors.background) {
if (mainActivityViewModel.appSettingsState.value.appSettings.colorsMapLight.size != 0
&& mainActivityViewModel.appSettingsState.value.appSettings.colorsMapDark.size != 0) {
navController.navigate(NavigationDestinations.homeScreen)
}
}
}
my initNavigationController function shows the splashscreen first. But it doesn't do anything. The getting app settings configuration is called in MyTheme composable via the mainActivityViewModel, and MyTheme will use the state from the viewmodel to define the theme, and the navController.navigate is based on the state as you guys can see in the if above.
I don't know if this is a good practice or not, or when my app grows it would be a mess or not, but at least it works for me. I tried with font styles too and it works like a charm.

Downsides to NOT calling UIViewController.addChild, .didMove, etc when embedding views from other view controllers

What are the downsides to not following this process?
let parent = UIViewController()
let child = UIViewController()
parent.view.addSubview(child.view)
parent.addChild(child)
child.didMove(toParent: parent)
// and to remove
child.willMove(toParent: nil)
child.removeFromParent()
child.view.removeFromSuperview()
and instead just doing something more on the order of
let parent = UIViewController()
let child = UIViewController()
parent.view.addSubview(child.view)
// and to remove
child.view.removeFromSuperview()
My specific desire is to use SwiftUI views in place of UIViews sprinkled through my project, but officially you're supposed to use a UIHostingController and embed it as a child view controller of whatever parent view controller it belongs to.
I was previously under the impression that you have to call these methods, but then another developer suggested I just try not calling them with the assumption I'm only missing out on view controller lifecycle events (which I don't think matter to me in most cases). I've since tried it and it worked, but I'm worried about what I'm missing/why this might be a bad idea.
I recently came across an example of something you might lose if you don't add the UIHostingViewContoller as a child of the parent view controller in this article about using SwiftUI views in self-sizing table view cells. If you don't add it as a child, the height of the cell holding its view is not always calculated correctly.
https://noahgilmore.com/blog/swiftui-self-sizing-cells/#view-controller-containment

How to allow a button that creates a new object in SwiftUI from not making an object on reload?

So I'm making a button for a "New Note" in Swift UI similar to the Apple Notes app.
Right now my "New Button" is a "Navigation Link" like so:
NavigationLink(
destination: EditorView(makeNewNote())
) {
Text("New")
}
Unfortunately—this triggers my app to create a new note every time the view loaded. :(
:/
I've been looking for a way to initate a segue on button push but I'm not finding success on this yet.
When I tried a modal—I found myself having the same problem
Button("New") {
self.isNew = true
}.sheet(isPresented: $isNew, content: {
EditorView(makeNewNote())
})
I'm wondering what the best way to approach this would be.
Having no success :(
Edit:
I referred to this and the documentation but I haven’t found a way to segue via a button push which would be ideal. (The function dosent get triggered in the closure :)
https://www.hackingwithswift.com/quick-start/swiftui/how-to-push-a-new-view-onto-a-
Also...if you were curious what makeNewButton() does—it basically inserts a new Core Data object into my app’s managed context.
I'm not entirely sure, but it kinda sounds like to me your problem lies in your model. Because each time your View loads it calls the makeNewButton() function right?
Maybe you can fix the problem by displaying the "new note" view and having an extra "Save" button that only makes changes to your model once it's triggered.
Alternatively, you could use context.rollback() to discard changes. Also, check out this Project. It's Beta 4 but works just the same and imo is a good example how to use CoreData with SwiftUI. :)

Ember - ConnectOutlet - when does view change from preRender to inDom

I am trying to get my head round the connectOutlet method and when a view that is returned from connectOutet is actually inserted into the DOM.
The view that is created in connectOutlet leaves connectOutlet in the preRender state.
connectOutlet: function(name, context) {
// method body
view = this.createOutletView(outletName, viewClass);
if (controller) { set(view, 'controller', controller); }
set(this, outletName, view);
return view;
}
I've not tracked down where or when the view is inserted into the Dom and the view transitions to the inDom state.
I suspect the runloop is at play and it transitions after the current runloop has finished.
Can anyone shed any light on this?
The run loop is indeed in play here. The run loop processes events by draining an ordered collection of queues. In order, they are: sync, actions, render, afterRender, destroy, and timers. View rendering is where the view is actually inserted into the DOM and it is always scheduled on the render queue.
If you have other questions about this, leave a comment, and I will be happy to expand this answer to cover them.

Appcelerator. Buttons in rows are unclickable

Titanium SDK version: 1.6.2 (tried with 1.7 too)
iPhone SDK version: 4.2
I am developing an iPhone app and I am fetching data from my API and presenting it in a table. In this table I got a button on each row that should allow the user to add that person to his or her contacts. The only problem with the code (I think) is that only the last button responds when being clicked. Nothing happens when I click the other buttons.
This is my code: http://pastie.org/1932098
What is wrong?
You are adding the button.addEventListener outside of the for statement, and since you are overwriting the button var with each iteration, the eventListener only attaches to the last button created.
This probably isn't the best way to work this, but to fix your problem, move the button.addEventListener inside the for statement, and then check for a unique identifier in the object that gets sent to the event. Example:
for (x=0;x<5;x++) {
var button = Titanium.UI.createButton({
height:40,
width:100,
top:50*x,
id:x
});
var label = Titanium.UI.createLabel({
text:'LABEL '+x
});
button.add(label);
win1.add(button);
button.addEventListener('click', function(e){
Ti.API.info('Button clicked '+e.source.id);
});
}
The button.id property is just made up, but now you can see which button sends the event. You could also use title, or anything else that is unique.
Other options to look at are creating unique variable names for each button, but that's probably more work. Also, instead of working with putting a button in the table row, use a label or image, then listen for the event generated by the table or row.