Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1 ANDROID:
- Learn Basic kotlin
- Architecture components
- Coroutine/RX
- MVVM/livedata
- clean architecture
- Modular
- Unit testing Junit5
- Ui Testing espresso/ui automator
- ci cd jenkins/circle ci/bitrise
- Kotlin:
- -Statically typed, resolves on compilation,
- - runs on jvm, open jdk,
- - jvm compat compiles down to java bytecode
- -interoperatable with java
- -official language of Android, developed by jetbrains
- Features:
- Concise - 40% less code than java
- Interoperable - works well with java
- Opensource - Under apache 2.0
- Trustworthy - Made by jetbrains and backed by google
- Easy - easier than java
- Less error - Compile time only error
- IDES supported:
- Eclipse, IntelliJ, Android Studio
- ###################################################CODE GENETICS#################
- //variables: var or val. val is constant var is variable hence variable.
- val GRAVITY_MARS = 3.711
- /*
- visibility modifiers:
- private - visible (can be accessed) from inside the class only.
- public - visible everywhere. (default if no modifier is stated)
- protected - visible to the class and its subclass.
- internal - any client inside the module can access them.
- */
- //functions: fun short for function, parameter declaration is name:Data_type, return declaration is : data_type, $variable_name to //embed in string. If you dont want to return any value you can explicitly declare it as "Unit" or "Nothing". By default, methods
- //in kotlin use Unit as return if no return type is specified
- fun demandMoney(message : String, amount : Int) : String {
- return "$message $";
- }
- //To call function:
- robbery("Give me my damn ",5000);
- //Class and Objects: All classes are final by default and "open" make it inheritable.
- //init block is called when an object of it is initialized
- //no constructor but in fact all classes have default no argument constructor
- Class Animal{ }
- //with 1 param "primary" constructor. When "()" is put after classname, it's considered as primary constructor with no body
- Class Animal(name : String) { }
- //with 1 param constructor with init block and default value. Init blocks are the body of the primary constructor
- Class Animal(_name : String, _address : String = "HOME") {
- var name : String = _name;
- var address : String = _address;
- init { println(name) }
- }
- //with multiple constructors using constructor keyword. "?" primarily means it can accept null values and is also used if you want to //assign a later value
- Class Animal{
- private var name : String?
- private var id : Int = 10;
- constructor(_name : String){
- name = _name;
- }
- constructor(_name : String, _id : Int){
- name = _name;
- id = _id;
- }
- }
- //shortcut functions, code inside returns a string then whole "run block" runs the code and acts as a string which enable you to call
- //string functions and properties ##################RUN WITH LET APPLY ALSO ##############################
- run{
- //executecode here and return something
- if(x<18) "you are not allowed here" else "Enter"
- }.length
- //similar to run is "with" keyword. Only, "with" along the object acts as receiver and makes it easier to manipulate that object
- //returns a new object not the original x
- with(x){
- plus(1)
- someMethodThatMultiplies(x) //this line wont do anything because it needs x as parameter. It only affects the receiver "x" if that
- //method is declared within x or declared in Int if x is Int
- x
- //the last line means the return of the with block
- }.hashCode()
- //theres also a way to have a "with" block of code using run keyword object.run also returns a new object not the original x
- x.run {
- plus(1)
- times(2)
- }.hashCode()
- //let is similar but instead of receiver, it acts as argument. x is renamed as xy for semantic purposes and does the same thing
- //if not renamed however, you use "it" to access fields/methods e.g: it.plus...
- x.let{ xy->
- xy.plus(1)
- xy.times(2)
- }.hashCode()
- //"also" is similar to let but it returns the original object instead of returning a new one
- x.also{xy->
- xy.plus(1) //by executing this line, you change x value
- }
- //apply is similar to "also" for returning the original object but it uses "this" instead of "it". "this" can be ommited same as run //and with
- x.apply{
- plus(1)
- }
- //basic instantiation of class
- var x = Animal();
- x.someMethod();
- //basic instantiation of class with parameter
- var x = Animal("Giraffe");
- x.someMethod();
- //basic input
- var text : String? = readLine()
- var age : Int? = readLine()?.toInt()
- //basic null check. If text is null without !! it wont display errors
- println(text!!)
- //basic variable conversion, use toString() vice versa. Also toDouble() toFloat() etc.
- var serial : String="43";
- var id : Int = serial.toInt()
- //if statements are the same with java, however there is a when keyword similar to switch
- var x : Int =0;
- when(x){
- 1,11,111->println("range from 1 to 10")
- in 2..5>->println("value is two")
- !in 2..5>->println("not between 2 and 5")
- else{
- println("else called")
- }
- }
- //you can also use if or when in initialization
- var num : Int = 8;
- var stuff : String = when(num){ 8->"eight" else->"just another number"}
- //basic loop use .. to specify range. format (any variable)..(any variable)
- var num : Int = 10
- for(item in 1..num){
- println(item)
- }
- var x = ArrayList<Int>(5){0}
- for(elements in x){
- }
- //basic string functions
- var name : String = "home";
- println(name[1]) //prints h
- println(name.toUpperCase())//prints HOME
- println(name.split("h"))
- //arrays and other collections. Use "to" keyword for maps
- var b = Array<String>(5){""}; //regular array
- var x = ArrayList<String>(); //can have duplicates
- var p = HashSet<String>(); //no duplicates
- var m = HashMap<Int,String>(); //no duplicates, only 1 null key allowed
- var m = HashTable<Int,String>(); //preserves order
- var f = HashMapOf(1 to "firstItem", 2 to "2nd");//can also be declared this way for convenience, add elements by f.put and so on
- var n = ArrayOf(1,2,3,)//same concept different presentation
- var l = listOf(1,2,3)//immutable
- var ll = mutableListOf//mutable version/editable
- var ll = setOf("asd","d")//immutable version/editable
- var ll = mutableSetOf("asd","d")//immutable version/editable
- //basic inheritance with constructor chain using "this" keyword. prints the ff in order:
- 1. from entity
- 2. from human
- 3. empty constuctor from human
- //take note it calls super constructors first before itself
- var d = entity();
- open class entity{
- var x : Int =0;
- constructor(z : Int){
- println("from entity");
- x = z;
- }
- }
- class human : entity {//take note that
- var x : Int =0;
- constructor () : this(88){
- println("empty constructor from human");
- }
- constructor(z : Int) : super(99){
- println("from human");
- x = z;
- }
- }
- //tradional getter kotlin way
- class Animal(var name:String){
- fun getName():String{
- return this.name;
- }
- }
- //getter and setter. Value is the parameter value like setter in java. field is getter variable. Here, we put logic if the setted value //is more than 3, field would be 9. final value would be 9
- var age : Int = 0
- get() = field
- set(value){
- field = if (value > 3)
- 9
- else
- value
- }
- //for singleton, use object instead of class.
- //to explicitly hide constructor, use "private constructor()" keyword
- //for usual pojos, use "data" keyword before class
- //static methods are of java but "companion object" are of kotlin
- //const keyword is compile time constant, val is runtime constant. Basically you cant assign method value to const values because //methods are evaluated at runtime
- class A {
- companion object {//static block
- fun a() {}
- fun b() {} //static but can be accessed by A.companion
- @JvmStatic fun c(){} //equivalent to static method which can be accessed by A.c
- @JvmField val x = 42 //equivalent to static field which can be accessed by A.x
- var y = "foo"
- }
- }
- #########RETROFIT##########
- 1.2. Using Retrofit
- To work with Retrofit you basically need the following three classes:
- ------------Model class which is used as a JSON model
- class Pokemon(var id : Int, var name : String){
- init{//do stuff here}
- }
- ------------Interfaces that define the possible HTTP operations
- e.g:
- append style for apis like "pokeapi.co/api/v2/pokemon/234"
- @POST("api/v2/pokemon/{num}")
- fun retrieveList(@Path("num") num : Int):
- parameter style like "sample.org/api&name=master
- @POST("api")
- fun retrieveList(@Query("num") num : Int):
- as multipart parameter
- @POST("api")
- fun retrieveList(@Body product: Product):
- ------------Retrofit.Builder class - Instance which uses the interface and the Builder API to allow defining the URL end point for the HTTP operations.
- public class Controller implements Callback<List<Change>> {
- static final String BASE_URL = "https://git.eclipse.org/r/";
- public void start() {
- Gson gson = new GsonBuilder()
- .setLenient()
- .create();
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(BASE_URL)
- .addConverterFactory(GsonConverterFactory.create(gson))
- .build();
- GerritAPI gerritAPI = retrofit.create(GerritAPI.class);
- Call<List<Change>> call = gerritAPI.loadChanges("status:open");
- call.enqueue(this);
- }
- @Override
- public void onResponse(Call<List<Change>> call, Response<List<Change>> response) {
- if(response.isSuccessful()) {
- List<Change> changesList = response.body();
- changesList.forEach(change -> System.out.println(change.subject));
- } else {
- System.out.println(response.errorBody());
- }
- }
- @Override
- public void onFailure(Call<List<Change>> call, Throwable t) {
- t.printStackTrace();
- }
- }
- then call by instantiating the object then calling create()
- ----------------------------------------------------------
- QUICK REFERESH JSON:
- //paste this on json editor
- {
- "a string":"this is string input",
- "z hobbies":[1,true,"string",{"num":"0.d2","nums":"2.d2"}],
- "c datum":{"type":"data","date":"12-09-19"}
- }
- {} is used for json objects
- : is used for value pairs
- [] is used for pairs.
- You cannot put value pairs in [] without {} (e.g: ["name":"game"] is wrong, should be [{"name:"game}]) this is because [] left hand elements are always numbers as indexes.
- You cannot put string without "", without "" your value should be either boolean or numeral
- GSON parsing tips:
- Each time gson sees a {}, it creates a Map (actually a gson StringMap )
- Each time gson sees a '', it creates a String
- Each time gson sees a true or false, it creates a Boolean
- Each time gson sees a number, it creates a Double
- Each time gson sees a [], it creates an ArrayList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement