Code Monkey home page Code Monkey logo

realmutil's Introduction

RealmUtil

This library using for saving local data for Android App into Realm data

Installing

We can install this library by using gradle

Step 1

allprojects {
	repositories {
			maven { url 'https://jitpack.io' }
		}
	}

Step 2

dependencies {
	        compile 'com.github.BondolTuon:RealmUtil:1.0.1'
	}

Sample Using

class BaseApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        Realm.init(this)
    }
}

 <application
        android:name=".BaseApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

open class UserData(
        @PrimaryKey public open var userId: Int = 0,
        var userEmail:String? = "",
        var username:String? = ""
) : RealmObject()

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnGetUserData.setOnClickListener {
            val userData = UserData()

            val result = getDataFromRealmDb(createRealmInstance(), userData)
            var str: String? = ""
            when (result.size) {
                0 -> {
                    Toast.makeText(this, "Realm Data is Empty", Toast.LENGTH_LONG).show()
                    tvUserData.text = ""
                }
                else -> {
                    for (data in result) {
                        data as UserData
                        str = str.plus("UserId: ${data.userId}\n Email: ${data.userEmail}\n Username: ${data.username}")
                    }
                    tvUserData.text = str
                }
            }
        }

        btnSaveA.setOnClickListener {
            saveData()
        }

        btnDelete.setOnClickListener {
            if (deleteAllDataInRealmDb(createRealmInstance())) {
                tvUserData.text = ""
            }
        }
    }

    fun saveData() {
        saveDataToRealmDb(true, createRealmInstance(), UserData(1, "[email protected]", "Chan Dara"))
    }
}

Min SDK Version

Support Min Sdk version >= 21

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the [LICENSE.md(https://github.com/BondolTuon/RealmUtil/blob/master/README.md) file for details

realmutil's People

Contributors

tuonbondol avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.