Saturday, June 5, 2021

Edward Lance Lorilla

Edward Lance Lorilla


【ANDROID STUDIO】Room Data Persistence Library

Posted: 04 Jun 2021 08:20 AM PDT

 apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"

defaultConfig {

applicationId "com.example.roomdemo"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding{
enabled = true
}

}

dependencies {
def lifecycle_version = "2.3.1"
def room_version = "2.3.0"

// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Annotation processor
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"

// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"

//coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.recyclerview:recyclerview:1.2.0'
}
package com.example.roomdemo.db

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "subscriber_data_table")
data class Subscriber (

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "subscriber_name")
val id : Int,

@ColumnInfo(name = "subscriber_id")
val name : String,

@ColumnInfo(name = "subscriber_email")
val email : String
)


package com.example.roomdemo.db

import androidx.lifecycle.LiveData
import androidx.room.*

@Dao
interface SubscriberDAO {
@Insert
suspend fun insertSubscriber(subscriber: Subscriber): Long

@Update
suspend fun updateSubscriber(subscriber: Subscriber)

@Delete
suspend fun deleteSubscriber(subscriber: Subscriber)

@Query("DELETE FROM subscriber_data_table")
suspend fun deleteAll()

@Query("SELECT * FROM subscriber_data_table")
fun getAllSubscribers(): LiveData<List<Subscriber>>
}

package com.example.roomdemo.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [Subscriber::class],version = 1)
abstract class SubscriberDatabase : RoomDatabase() {

abstract val subscriberDAO: SubscriberDAO

companion object {
@Volatile
private var INSTANCE: SubscriberDatabase? = null
fun getInstance(context: Context): SubscriberDatabase {
synchronized(this) {
var instance = INSTANCE
if (instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
SubscriberDatabase::class.java,
"subscriber_data_database"
).build()
}
return instance
}
}

}
}


package com.example.roomdemo.db

class SubscriberRepository(private val dao : SubscriberDAO) {

val subscribers = dao.getAllSubscribers()

suspend fun insert(subscriber: Subscriber) {
dao.insertSubscriber(subscriber)
}

suspend fun update(subscriber: Subscriber) {
dao.updateSubscriber(subscriber)
}

suspend fun delete(subscriber: Subscriber) {
dao.deleteSubscriber(subscriber)
}

suspend fun deleteAll() {
dao.deleteAll()
}
}

package com.example.roomdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.roomdemo.databinding.ActivityMainBinding
import com.example.roomdemo.db.SubscriberDatabase
import com.example.roomdemo.db.SubscriberRepository

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var subscriberViewModel: SubscriberViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
val dao = SubscriberDatabase.getInstance(application).subscriberDAO
val repository = SubscriberRepository(dao)
val factory = SubscriberViewModelFactory(repository)
subscriberViewModel = ViewModelProvider(this,factory).get(SubscriberViewModel::class.java)
binding.myViewModel = subscriberViewModel
binding.lifecycleOwner = this
displaySubscribersList()
}

private fun displaySubscribersList() {
subscriberViewModel.subscribers.observe(this, Observer {
Log.i("MYTAG",it.toString())
})

}
}

package com.example.roomdemo

import androidx.databinding.Bindable
import androidx.databinding.Observable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.roomdemo.db.Subscriber
import com.example.roomdemo.db.SubscriberRepository
import kotlinx.coroutines.launch

class SubscriberViewModel(private val repository: SubscriberRepository) : ViewModel(),Observable {

val subscribers = repository.subscribers
@Bindable
val inputName = MutableLiveData<String>()
@Bindable
val inputEmail = MutableLiveData<String>()
@Bindable
val saveOrUpdateButtonText = MutableLiveData<String>()
@Bindable
val clearAllOrDeleteButtonText = MutableLiveData<String>()

init {
saveOrUpdateButtonText.value = "Save"
clearAllOrDeleteButtonText.value = "Clear All"
}

fun saveOrUpdate() {
val name = inputName.value!!
val email = inputEmail.value!!
insert(Subscriber(0, name, email))
inputName.value = null
inputEmail.value = null
}

fun clearAllOrDelete() {
clearAll()
}

fun insert(subscriber: Subscriber)= viewModelScope.launch {
repository.insert(subscriber)
}

fun update(subscriber: Subscriber) = viewModelScope.launch {
repository.update(subscriber)
}

fun delete(subscriber: Subscriber) = viewModelScope.launch {
repository.delete(subscriber)
}

fun clearAll()=viewModelScope.launch {
repository.deleteAll()
}

override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {

}

override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {

}
}

package com.example.roomdemo

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.roomdemo.db.SubscriberRepository

class SubscriberViewModelFactory(private val repository: SubscriberRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(SubscriberViewModel::class.java)) {
return SubscriberViewModel(repository) as T
}
throw IllegalArgumentException("Unknown View Model class")
}
}


【Visual Studio Visual Csharp】Progess Bar Marquee

Posted: 04 Jun 2021 08:17 AM PDT

 using System;

using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;// make sure that using System.Diagnostics; is included using System.Diagnostics;namespace progressBarMarrqueC{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } int StartPos = 0; string TextToShow = "https://edwardize.blogspot.com"; int Position = 20; private void Form1_Load(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { Graphics gra = this.CreateGraphics(); gra.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; gra.DrawString(TextToShow, new Font("Arial", Position), new SolidBrush(Color.Black), StartPos, 35); StartPos += 10; if (StartPos >= this.Width) StartPos = TextToShow.Length * Position * -1; this.Refresh(); } }}

【PYTHON】pickle can be used for saving and loading raw Python objects

Posted: 04 Jun 2021 08:14 AM PDT

 import pickle as pk


data_dictionary = {'books'12'articles'100'subjects': ['math''programming''data science']}

with open('readings.pk''wb'as f:
  pk.dump(data_dictionary, f)
with open('readings.pk''rb'as f:
  data = pk.load(f)

print(data)

【GAMEMAKER】Snake

Posted: 04 Jun 2021 08:13 AM PDT

 Information about object: objHead

Sprite: SprHeadLeft
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

Init();
MoveDir="Left";
SwitchDir="Left";
MoveSpeed=4;
SafeStart=true;
alarm[0]=8;
Alarm Event for alarm 0:
execute code:

SafeStart=false;
Step Event:
execute code:

Store();
if keyboard_check(vk_left) {
SwitchDir="Left";
}

if keyboard_check(vk_right) {
SwitchDir="Right";
}

if keyboard_check(vk_up) {
SwitchDir="Up";
}

if keyboard_check(vk_down) {
SwitchDir="Down";
}

if (place_snapped(32,32)) {
switch (SwitchDir) {
case "Up":
sprite_index = (SprHeadUp);
MoveDir="Up"
break;
case "Down":
sprite_index = (SprHeadDown);
MoveDir="Down"
break;
case "Left":
sprite_index = (SprHeadLeft);
MoveDir="Left"
break;
case "Right":
sprite_index = (SprHeadRight);
MoveDir="Right"
break;
}
}

MoveX=0;
MoveY=0;

switch (MoveDir) {
case "Up":
y-=MoveSpeed;
MoveY=-MoveSpeed;
break;
case "Down":
y+=MoveSpeed;
MoveY=MoveSpeed;
break;
case "Left":
x-=MoveSpeed;
MoveX=-MoveSpeed;
break;
case "Right":
x+=MoveSpeed;
MoveX=MoveSpeed;
break;
}

Collision Event with object objBody:
execute code:

if SafeStart=true then exit;
sound_play(sound0);
Collision Event with object objFood:
execute code:

with (other) instance_destroy();
score+=10;
aa=instance_create(0,0,objFood);
with (aa) move_random(32,32);
AddBody();
Mouse Event for Left Button:
execute code:

AddBody();
io_clear();
Other Event: Game Start:
execute code:

aa=instance_create(x,y,objBody1);
aa.Follows=objHead;
global.LastBody=aa;
for (z=1; z<4; z+=1) {
AddBody();
}
aa=instance_create(0,0,objFood);
with (aa) move_random(32,32);

Information about object: objBody
Sprite: sprBody
Solid: false
Visible: true
Depth: 0
Persistent:
false
Parent:
Children:
Mask:

No Physics Object

Create Event:
execute code:

Init();
Step Event:
execute code:

Store();
Get();

Information about object: objBody1
Sprite: sprBody
Solid: false
Visible: true
Depth: 0
Persistent:
false
Parent:
Children:
Mask:

No Physics Object

Create Event:
execute code:

Init();
Step Event:
execute code:

Store();
Get();

Information about object: objFood
Sprite: sprFood
Solid: false
Visible: true
Depth: 0
Persistent:
false
Parent:
Children:
Mask:

No Physics Object

addBody()
aa=instance_create(global.LastBody.x, global.LastBody.y, objBody)
aa.Follows=global.LastBody
global.LastBody=aa;

init()
for (z=0; z<9; z+=1) {
LastX[z]=x;
LastY[z]=y;
}

store()

for (z=8; z>1; z-=1) {
LastX[z]=LastX[z-1];
}
for (z=8; z>1; z-=1) {
LastY[z]=LastY[z-1];
}
LastX[1]=x;
LastY[1]=y;

get()
x=Follows.LastX[7];
y=Follows.LastY[7];

No comments:

Post a Comment