package com.example.witch.value;

import com.example.witch.R;
import com.example.witch.utils.TextWatcherAdapter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import se.snylt.witch.annotations.BindTo;
import se.snylt.witch.annotations.Binds;
import se.snylt.witch.viewbinder.Witch;
import se.snylt.witch.viewbinder.bindaction.Binder;
import se.snylt.witch.viewbinder.bindaction.SyncOnBind;

public class ValueFragment extends Fragment {

    private String text = "";

    @BindTo(R.id.value_view_fragment_input)
    TextWatcher textWatcher = new TextWatcherAdapter() {

        @Override
        public void afterTextChanged(Editable s) {
            // Use update to flag value as dirty
            text = s.toString();
            bind();
        }
    };

    @Binds
    Binder bindTextWatcher = Binder.create(new SyncOnBind<EditText, TextWatcher>() {
        @Override
        public void onBind(EditText editText, TextWatcher textWatcher) {
            editText.addTextChangedListener(textWatcher);
        }
    });

    @BindTo(R.id.value_view_fragment_text)
    final String text() {
        return text;
    }

    @Binds
    final Binder<TextView, String> bindText() {
        return Binder.create(new SyncOnBind<TextView, String>() {
            @Override
            public void onBind(TextView textView, String text) {
                textView.setText(text);
            }
        });
    }

    private void bind() {
        Witch.bind(this, getView());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.value_view_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bind();
    }
}


@@ -484,49 +484,8 @@
 To;%0A
-import se.snylt.witch.annotations.Binds;%0A
 impo
@@ -627,16 +627,73 @@
 cOnBind;
+%0Aimport se.snylt.witch.viewbinder.bindaction.ValueBinder;
 %0A%0Apublic
@@ -737,39 +737,8 @@
  %7B%0A%0A
-    private String text = %22%22;%0A%0A
     
@@ -780,16 +780,38 @@
 put)%0A   
+ ValueBinder%3CEditText,
  TextWat
@@ -806,32 +806,33 @@
 ext, TextWatcher
+%3E
  textWatcher = n
@@ -830,16 +830,35 @@
 tcher = 
+ValueBinder.create(
 new Text
@@ -1011,19 +1011,34 @@
     text
- = 
+.update().setText(
 s.toStri
@@ -1041,16 +1041,17 @@
 String()
+)
 ;%0A      
@@ -1083,50 +1083,9 @@
    %7D
-;%0A%0A    @Binds%0A    Binder bindTextWatcher =
+,
  Bin
@@ -1302,16 +1302,17 @@
 %7D%0A    %7D)
+)
 ;%0A%0A    @
@@ -1363,119 +1363,87 @@
 nal 
-String text() %7B%0A        return text;%0A    %7D%0A%0A    @Binds%0A    final Binder%3CTextView, String%3E bindText() %7B%0A
+ValueBinder%3CTextView, Model%3E text = ValueBinder.create(new Model(),%0A   
         
 retu
@@ -1438,22 +1438,16 @@
         
-return
  Binder.
@@ -1478,22 +1478,21 @@
 xtView, 
-String
+Model
 %3E() %7B%0A  
@@ -1493,32 +1493,36 @@
 ) %7B%0A            
+   
+ 
 @Override%0A      
@@ -1507,32 +1507,36 @@
       @Override%0A
+    
             publ
@@ -1573,19 +1573,15 @@
 ew, 
-String text
+Model m
 ) %7B%0A
@@ -1592,24 +1592,28 @@
             
+ 
+   
 textView.set
@@ -1617,20 +1617,27 @@
 setText(
-t
+m.getT
 ext
+()
 );%0A     
@@ -1639,24 +1639,28 @@
             
+ 
+   
 %7D%0A        %7D)
@@ -1661,17 +1661,16 @@
     
-%7D);%0A
     %7D
+));
 %0A%0A  
@@ -2159,12 +2159,233 @@
 );%0A    %7D
+%0A%0A    static class Model %7B%0A%0A        private String text = %22%22;%0A%0A        public String getText() %7B%0A            return text;%0A        %7D%0A%0A        public void setText(String text) %7B%0A            this.text = text;%0A        %7D%0A    %7D
 %0A%7D%0A%0A

Revert value fragment example.