Created
November 13, 2017 10:45
-
-
Save nPn-/e6145f830bcea70ad33d13d1e1edccb7 to your computer and use it in GitHub Desktop.
An Anko Example with the main content extracted to a seperate class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.john.ankobasic | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import org.jetbrains.anko.setContentView | |
/** | |
* Created by john on 11/12/17. | |
*/ | |
class MainActivity : AppCompatActivity() { | |
private lateinit var presenter: MainPresenter | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val mainView = MainView() | |
mainView.setContentView(this) | |
presenter = MainPresenter(mainView) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.john.ankobasic | |
import android.content.Context | |
import android.util.AttributeSet | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.ViewManager | |
import android.widget.Button | |
import android.widget.EditText | |
import android.widget.TextView | |
import org.jetbrains.anko.* | |
import org.jetbrains.anko.custom.ankoView | |
/** | |
* Created by john on 11/12/17. | |
*/ | |
class MainContentView : AnkoComponent<Context> { | |
lateinit var textBox: EditText | |
lateinit var button: Button | |
lateinit var clickCount: TextView | |
override fun createView(ui: AnkoContext<Context>) = with(ui) { | |
verticalLayout { | |
button = themedButton { | |
text = "ok" | |
} | |
textBox = themedEditText { | |
hint = "hi" | |
} | |
clickCount = themedTextView { | |
text = "0" | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.john.ankobasic | |
/** | |
* Created by john on 11/10/17. | |
*/ | |
class MainModel { | |
var clickCount = 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.john.ankobasic | |
import android.widget.TextView | |
/** | |
* Created by john on 11/8/17. | |
*/ | |
class MainPresenter(private val view: MainView) { | |
private val model : MainModel = MainModel() | |
private val clickCountText = view.clickCount | |
init { | |
setUpListeners() | |
} | |
private fun setUpListeners() { | |
view.button.setOnClickListener { | |
println("button was clicked") | |
model.clickCount += 1 | |
clickCountText.text = "${model.clickCount}" | |
} | |
view.settingItem.setOnMenuItemClickListener { | |
println("setting menu item was clicked") | |
true | |
} | |
view.otherItem.setOnMenuItemClickListener { | |
println("other menu item was clicked") | |
true | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.john.ankobasic | |
import android.support.design.widget.FloatingActionButton | |
import android.view.* | |
import android.widget.Button | |
import android.widget.EditText | |
import android.widget.TextView | |
import org.jetbrains.anko.* | |
import org.jetbrains.anko.design.* | |
/** | |
* Created by john on 11/5/17. | |
*/ | |
class MainView : AnkoComponent<MainActivity> { | |
lateinit var textBox: EditText | |
lateinit var button: Button | |
lateinit var clickCount: TextView | |
private lateinit var mainMenu: Menu | |
lateinit var settingItem: MenuItem | |
lateinit var otherItem: MenuItem | |
private lateinit var floatingActionButton: FloatingActionButton | |
override fun createView(ui: AnkoContext<MainActivity>) = with(ui) { | |
coordinatorLayout { | |
verticalLayout { | |
themedAppBarLayout { | |
themedToolbar(theme = R.style.Base_ThemeOverlay_AppCompat_Dark_ActionBar) { | |
title = resources.getString(R.string.app_name) | |
popupTheme = R.style.AppTheme | |
mainMenu = menu | |
settingItem = mainMenu.add("My Settings") | |
otherItem = mainMenu.add("My Other") | |
} | |
}.lparams(width = matchParent, height = wrapContent) | |
mainContentView { | |
button = it.button | |
textBox = it.textBox | |
clickCount = it.clickCount | |
}.lparams(width = matchParent, height = wrapContent) | |
}.lparams(width = matchParent, height = wrapContent) | |
floatingActionButton = floatingActionButton { | |
imageResource = android.R.drawable.ic_dialog_email | |
}.lparams { | |
margin = dip(10) | |
gravity = Gravity.BOTTOM or Gravity.END | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.john.ankobasic | |
import android.view.View | |
import android.view.ViewManager | |
import org.jetbrains.anko.AnkoContext | |
import org.jetbrains.anko.custom.ankoView | |
/** | |
* Created by john on 11/12/17. | |
*/ | |
inline fun ViewManager.mainContentView(theme: Int = 0) = mainContentView(theme) {} | |
inline fun ViewManager.mainContentView(theme: Int = 0, init: View.(mainContentView: MainContentView) -> Unit): View { | |
val mainContentView = MainContentView() | |
return ankoView({ mainContentView.createView(AnkoContext.create(it)) }, theme, { init(mainContentView)} ) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment