GithubHelp home page GithubHelp logo

naseemakhtar994 / lifecyclemodel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jessyancoding/lifecyclemodel

0.0 1.0 0.0 204 KB

The LifecycleModel class is designed to store and manage UI-related data in a lifecycle conscious way, the LifecycleModel class allows data to survive configuration changes such as screen rotations, it also handles the communication of the Activity / Fragment with the rest of the application, base on https://developer.android.google.cn/topic/libraries/architecture/viewmodel.html

License: Apache License 2.0

Java 100.00%
lifecycle model mvc mvp

lifecyclemodel's Introduction

LifecycleModel

Bintray Build Status API License

The LifecycleModel class is designed to store and manage UI-related data in a lifecycle conscious way, the LifecycleModel class allows data to survive configuration changes such as screen rotations, it also handles the communication of the Activity / Fragment with the rest of the application, base on ViewModel

Introduction

LifecycleModel 基于 Google 在 2017 年 I/O 大会上发布的 Android 架构组件中的 ViewModel, 可以帮助 Activity 和 Fragment 储存和管理一些与 UI 相关以及他们必需的数据, 避免数据在屏幕旋转或配置更改时发生的数据丢失, 还可以帮助开发者轻松实现 Fragment 与 Fragment 之间, Activity 与 Fragment 之间的通讯以及共享数据, 因为看到 Google 让 MVVM 模式中的 ViewModel 具有了这些功能, 所以我也想让 MVP 模式中的 Presenter, 乃至其他更多的模块都具有这些功能, 所以 LifecycleModel 诞生了

Lifecycle

lifecycle

Download

 compile 'me.jessyan:lifecyclemodel:1.0.1'

Usage

Step 1

 public class UserLifecycleModel implements LifecycleModel {
         private int id;
 
         public UserLifecycleModel(int id) {
             this.id = id;
         }
         
         void doAction() {
         
         }
 
         @Override
         public void onCleared() {
             //release resources
         }
     }

Step 2

 //Put data
 LifecycleModelProviders.of(activity/fragment).put(UserLifecycleModel.class.getName(), new UserLifecycleModel(1));  

 //Get data
 LifecycleModelProviders.of(activity/fragment).get(UserLifecycleModel.class.getName());  
 
 //Remove data
 LifecycleModelProviders.of(activity/fragment).remove(UserLifecycleModel.class.getName());

Communication of the Activity / Fragment

 public class UserLifecycleModel implements LifecycleModel {
     private Subject<String, String> mSubject = PublishSubject.create();

     public void doAction(String s) {
         mSubject.onNext(s);
     }

     public void addAction(Action1<String> action) {
         mSubject.subscribe(action);
     }
 }
 
 public class AFragment extends Fragment {
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         UserLifecycleModel lifecycleModel = LifecycleModelProviders.of(getActivity()).get(UserLifecycleModel.class.getName());
         lifecycleModel.addAction(new Action1<String>() {
             @Override
             public void call(String s) {
                 // Update the UI.
             }
         });
     }
 }
 
 public class BFragment extends Fragment {
     @Override
     public void onStart() {
         super.onStart();
         UserLifecycleModel lifecycleModel = LifecycleModelProviders.of(getActivity()).get(UserLifecycleModel.class.getName());
         lifecycleModel.doAction("JessYan");
     }
 }

About Me

License

 Copyright 2017, jessyan

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

lifecyclemodel's People

Contributors

jessyancoding avatar

Watchers

Naseem Akhtar 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.