Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
IOTClient
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lola
IOTClient
Commits
b010ef94
Commit
b010ef94
authored
Apr 19, 2019
by
DESKTOP-NFGF3PG\zxa01
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commit mvvm of recyclerView
parent
f055d88e
Changes
19
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
489 additions
and
226 deletions
+489
-226
app/build.gradle
app/build.gradle
+3
-0
app/src/main/AndroidManifest.xml
app/src/main/AndroidManifest.xml
+1
-0
app/src/main/java/com/example/zxa01/iotclient/bindings/CustomViewBindings.java
.../example/zxa01/iotclient/bindings/CustomViewBindings.java
+15
-0
app/src/main/java/com/example/zxa01/iotclient/home/HomeActivity.java
...n/java/com/example/zxa01/iotclient/home/HomeActivity.java
+3
-3
app/src/main/java/com/example/zxa01/iotclient/home/device/DeviceAdapter.java
...om/example/zxa01/iotclient/home/device/DeviceAdapter.java
+0
-62
app/src/main/java/com/example/zxa01/iotclient/home/device/DeviceViewModel.java
.../example/zxa01/iotclient/home/device/DeviceViewModel.java
+0
-36
app/src/main/java/com/example/zxa01/iotclient/home/device/model/DeviceModel.java
...xample/zxa01/iotclient/home/device/model/DeviceModel.java
+21
-0
app/src/main/java/com/example/zxa01/iotclient/home/device/model/DevicesModel.java
...ample/zxa01/iotclient/home/device/model/DevicesModel.java
+65
-0
app/src/main/java/com/example/zxa01/iotclient/home/device/view/DeviceAdapter.java
...ample/zxa01/iotclient/home/device/view/DeviceAdapter.java
+75
-0
app/src/main/java/com/example/zxa01/iotclient/home/device/view/DeviceFragment.java
...mple/zxa01/iotclient/home/device/view/DeviceFragment.java
+16
-24
app/src/main/java/com/example/zxa01/iotclient/home/device/view/create/DeviceCreateFragment.java
...tclient/home/device/view/create/DeviceCreateFragment.java
+3
-2
app/src/main/java/com/example/zxa01/iotclient/home/device/viewModel/DevicesViewModel.java
...a01/iotclient/home/device/viewModel/DevicesViewModel.java
+71
-0
app/src/main/java/com/example/zxa01/iotclient/home/service/CommonService.java
...m/example/zxa01/iotclient/home/service/CommonService.java
+0
-16
app/src/main/java/com/example/zxa01/iotclient/http/Api.java
app/src/main/java/com/example/zxa01/iotclient/http/Api.java
+45
-0
app/src/main/java/com/example/zxa01/iotclient/http/HttpService.java
...in/java/com/example/zxa01/iotclient/http/HttpService.java
+61
-0
app/src/main/java/com/example/zxa01/iotclient/pojo/Device.java
...rc/main/java/com/example/zxa01/iotclient/pojo/Device.java
+10
-1
app/src/main/res/layout/fragment_device.xml
app/src/main/res/layout/fragment_device.xml
+27
-27
app/src/main/res/layout/fragment_device_create.xml
app/src/main/res/layout/fragment_device_create.xml
+1
-1
app/src/main/res/layout/recycler_view_device.xml
app/src/main/res/layout/recycler_view_device.xml
+72
-54
No files found.
app/build.gradle
View file @
b010ef94
...
...
@@ -32,11 +32,14 @@ dependencies {
implementation
'com.android.support.constraint:constraint-layout:1.1.3'
implementation
'com.android.support:support-v4:28.0.0'
implementation
'com.android.support:recyclerview-v7:28.0.0'
implementation
"com.squareup.retrofit2:retrofit:2.1.0"
implementation
"com.squareup.retrofit2:converter-gson:2.1.0"
testImplementation
'junit:junit:4.12'
androidTestImplementation
'com.android.support.test:runner:1.0.2'
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
// 可移除
implementation
'com.squareup.okhttp3:okhttp:3.14.1'
implementation
'com.jakewharton:butterknife:9.0.0-rc2'
annotationProcessor
'com.jakewharton:butterknife-compiler:9.0.0-rc2'
}
app/src/main/AndroidManifest.xml
View file @
b010ef94
...
...
@@ -6,6 +6,7 @@
<uses-permission
android:name=
"android.permission.GET_ACCOUNTS"
/>
<uses-permission
android:name=
"android.permission.READ_PROFILE"
/>
<uses-permission
android:name=
"android.permission.READ_CONTACTS"
/>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<application
android:allowBackup=
"true"
...
...
app/src/main/java/com/example/zxa01/iotclient/bindings/CustomViewBindings.java
0 → 100644
View file @
b010ef94
package
com.example.zxa01.iotclient.bindings
;
import
android.databinding.BindingAdapter
;
import
android.support.v7.widget.LinearLayoutManager
;
import
android.support.v7.widget.RecyclerView
;
public
class
CustomViewBindings
{
@BindingAdapter
(
"setAdapter"
)
public
static
void
bindRecyclerViewAdapter
(
RecyclerView
recyclerView
,
RecyclerView
.
Adapter
<?>
adapter
)
{
recyclerView
.
setHasFixedSize
(
true
);
recyclerView
.
setLayoutManager
(
new
LinearLayoutManager
(
recyclerView
.
getContext
()));
recyclerView
.
setAdapter
(
adapter
);
}
}
app/src/main/java/com/example/zxa01/iotclient/home/HomeActivity.java
View file @
b010ef94
...
...
@@ -9,8 +9,8 @@ import android.support.v4.app.FragmentTransaction;
import
android.support.v7.app.AppCompatActivity
;
import
com.example.zxa01.iotclient.R
;
import
com.example.zxa01.iotclient.databinding.ActivityHomeBinding
;
import
com.example.zxa01.iotclient.home.device.DeviceFragment
;
import
com.example.zxa01.iotclient.home.device.
Device
ViewModel
;
import
com.example.zxa01.iotclient.home.device.
view.
DeviceFragment
;
import
com.example.zxa01.iotclient.home.device.
viewModel.Devices
ViewModel
;
import
com.example.zxa01.iotclient.home.privacy.PrivacyFragment
;
import
com.example.zxa01.iotclient.home.setting.SettingFragment
;
...
...
@@ -82,7 +82,7 @@ public class HomeActivity extends AppCompatActivity implements
public
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
Intent
intent
)
{
if
(
requestCode
==
0
)
{
if
(
resultCode
==
RESULT_OK
)
{
new
DeviceViewModel
().
createDevice
(
intent
.
getStringExtra
(
"SCAN_RESULT"
));
new
Device
s
ViewModel
().
createDevice
(
intent
.
getStringExtra
(
"SCAN_RESULT"
));
}
}
}
...
...
app/src/main/java/com/example/zxa01/iotclient/home/device/DeviceAdapter.java
deleted
100644 → 0
View file @
f055d88e
package
com.example.zxa01.iotclient.home.device
;
import
android.support.v7.widget.RecyclerView
;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
android.widget.TextView
;
import
com.example.zxa01.iotclient.R
;
import
com.example.zxa01.iotclient.pojo.Device
;
import
java.util.LinkedList
;
public
class
DeviceAdapter
extends
RecyclerView
.
Adapter
<
DeviceAdapter
.
MyViewHolder
>
{
private
final
LinkedList
<
Device
>
mList
;
public
DeviceAdapter
(
LinkedList
<
Device
>
mList
)
{
this
.
mList
=
mList
;
}
@Override
public
MyViewHolder
onCreateViewHolder
(
ViewGroup
parent
,
int
viewType
)
{
View
mItemView
=
LayoutInflater
.
from
(
parent
.
getContext
()).
inflate
(
R
.
layout
.
recycler_view_device
,
parent
,
false
);
return
new
MyViewHolder
(
mItemView
);
}
@Override
public
void
onBindViewHolder
(
MyViewHolder
mholder
,
int
position
)
{
Device
device
=
mList
.
get
(
position
);
mholder
.
position
=
position
;
mholder
.
textDeviceName
.
setText
(
device
.
getName
());
mholder
.
textDeviceSubtitle
.
setText
(
device
.
getSubTitle
());
mholder
.
textDeviceStatus
.
setText
(
device
.
isStatus
()
?
"Connection"
:
"Disconnection"
);
}
@Override
public
int
getItemCount
()
{
return
mList
.
size
();
}
class
MyViewHolder
extends
RecyclerView
.
ViewHolder
implements
View
.
OnClickListener
{
public
int
position
;
public
TextView
textDeviceName
;
public
TextView
textDeviceSubtitle
;
public
TextView
textDeviceStatus
;
public
MyViewHolder
(
View
view
)
{
super
(
view
);
this
.
textDeviceName
=
view
.
findViewById
(
R
.
id
.
text_device_name
);
this
.
textDeviceSubtitle
=
view
.
findViewById
(
R
.
id
.
text_device_subtitle
);
this
.
textDeviceStatus
=
view
.
findViewById
(
R
.
id
.
text_device_status
);
}
@Override
public
void
onClick
(
View
v
)
{
}
}
}
\ No newline at end of file
app/src/main/java/com/example/zxa01/iotclient/home/device/DeviceViewModel.java
deleted
100644 → 0
View file @
f055d88e
package
com.example.zxa01.iotclient.home.device
;
import
android.arch.lifecycle.ViewModel
;
import
com.example.zxa01.iotclient.home.service.CommonService
;
import
com.example.zxa01.iotclient.pojo.Device
;
import
java.util.LinkedList
;
public
class
DeviceViewModel
extends
ViewModel
{
private
LinkedList
<
Device
>
mList
=
new
LinkedList
<>();
private
CommonService
service
;
public
DeviceViewModel
(){
updateDevices
();
}
public
void
updateDevices
(){
// TODO api service.queryDevices();
Device
device1
=
new
Device
(
1
,
"test1"
,
"test"
,
"test"
,
true
);
Device
device2
=
new
Device
(
1
,
"test2"
,
"test"
,
"test"
,
true
);
Device
device3
=
new
Device
(
1
,
"test3"
,
"test"
,
"test"
,
true
);
mList
.
add
(
device1
);
mList
.
add
(
device2
);
mList
.
add
(
device3
);
}
public
LinkedList
<
Device
>
getDevices
()
{
return
mList
;
}
public
void
createDevice
(
String
address
){
// TODO query and update mList
// service.createDevice(address);
}
}
app/src/main/java/com/example/zxa01/iotclient/home/device/model/DeviceModel.java
0 → 100644
View file @
b010ef94
package
com.example.zxa01.iotclient.home.device.model
;
import
com.example.zxa01.iotclient.pojo.Device
;
import
android.databinding.BaseObservable
;
public
class
DeviceModel
extends
BaseObservable
{
private
Device
device
;
public
DeviceModel
(
Device
device
){
this
.
device
=
device
;
}
public
Device
getDevice
()
{
return
device
;
}
public
void
setDevice
(
Device
device
)
{
this
.
device
=
device
;
}
}
app/src/main/java/com/example/zxa01/iotclient/home/device/model/DevicesModel.java
0 → 100644
View file @
b010ef94
package
com.example.zxa01.iotclient.home.device.model
;
import
android.arch.lifecycle.MutableLiveData
;
import
android.databinding.BaseObservable
;
import
android.util.Log
;
import
com.example.zxa01.iotclient.http.Api
;
import
com.example.zxa01.iotclient.pojo.Device
;
import
java.util.ArrayList
;
import
java.util.List
;
import
retrofit2.Call
;
import
retrofit2.Callback
;
import
retrofit2.Response
;
public
class
DevicesModel
extends
BaseObservable
{
private
String
status
;
private
List
<
DeviceModel
>
deviceList
=
new
ArrayList
<>();
private
MutableLiveData
<
List
<
DeviceModel
>>
devices
=
new
MutableLiveData
<>();
public
void
addDevice
(
DeviceModel
device
)
{
deviceList
.
add
(
device
);
}
public
MutableLiveData
<
List
<
DeviceModel
>>
getDevices
()
{
return
devices
;
}
public
void
fetchDevices
()
{
Callback
<
Object
>
callback
=
new
Callback
<
Object
>()
{
@Override
public
void
onResponse
(
Call
<
Object
>
call
,
Response
<
Object
>
response
)
{
// TODO transfer response
Log
.
i
(
"Test"
,
response
.
message
());
DevicesModel
devicesModel
=
new
DevicesModel
();
devicesModel
.
addDevice
(
new
DeviceModel
(
new
Device
(
1
,
"test"
,
"test"
,
"test"
,
true
)));
devices
.
setValue
(
devicesModel
.
deviceList
);
status
=
"success"
;
}
@Override
public
void
onFailure
(
Call
<
Object
>
call
,
Throwable
t
)
{
Log
.
e
(
"Test"
,
t
.
getMessage
(),
t
);
}
};
Api
.
getApi
().
getDevices
().
enqueue
(
callback
);
}
public
void
createDevice
(
String
address
)
{
// TODO api post-createDevice & update
}
public
String
getStatus
()
{
return
status
;
}
public
void
setStatus
(
String
statue
)
{
this
.
status
=
statue
;
}
}
app/src/main/java/com/example/zxa01/iotclient/home/device/view/DeviceAdapter.java
0 → 100644
View file @
b010ef94
package
com.example.zxa01.iotclient.home.device.view
;
import
android.databinding.DataBindingUtil
;
import
android.databinding.ViewDataBinding
;
import
android.support.annotation.LayoutRes
;
import
android.support.annotation.NonNull
;
import
android.support.v7.widget.RecyclerView
;
import
android.view.LayoutInflater
;
import
android.view.ViewGroup
;
import
com.example.zxa01.iotclient.BR
;
import
com.example.zxa01.iotclient.home.device.model.DeviceModel
;
import
com.example.zxa01.iotclient.home.device.viewModel.DevicesViewModel
;
import
java.util.List
;
public
class
DeviceAdapter
extends
RecyclerView
.
Adapter
<
DeviceAdapter
.
MyViewHolder
>
{
private
int
layoutId
;
private
List
<
DeviceModel
>
deviceList
;
private
DevicesViewModel
viewModel
;
public
DeviceAdapter
(
@LayoutRes
int
layoutId
,
DevicesViewModel
viewModel
)
{
this
.
layoutId
=
layoutId
;
this
.
viewModel
=
viewModel
;
}
private
int
getLayoutIdForPosition
(
int
position
)
{
return
layoutId
;
}
@Override
public
MyViewHolder
onCreateViewHolder
(
@NonNull
ViewGroup
parent
,
int
viewType
)
{
return
new
MyViewHolder
(
DataBindingUtil
.
inflate
(
LayoutInflater
.
from
(
parent
.
getContext
()),
viewType
,
parent
,
false
));
}
@Override
public
void
onBindViewHolder
(
MyViewHolder
holder
,
int
position
)
{
holder
.
bind
(
viewModel
,
position
);
}
@Override
public
int
getItemCount
()
{
return
deviceList
==
null
?
0
:
deviceList
.
size
();
}
@Override
public
int
getItemViewType
(
int
position
)
{
return
getLayoutIdForPosition
(
position
);
}
public
void
setDevices
(
List
<
DeviceModel
>
deviceList
)
{
this
.
deviceList
=
deviceList
;
}
class
MyViewHolder
extends
RecyclerView
.
ViewHolder
{
final
ViewDataBinding
binding
;
MyViewHolder
(
ViewDataBinding
binding
)
{
super
(
binding
.
getRoot
());
this
.
binding
=
binding
;
}
void
bind
(
DevicesViewModel
viewModel
,
Integer
position
)
{
viewModel
.
getDeviceAt
(
position
);
binding
.
setVariable
(
BR
.
viewModel
,
viewModel
);
binding
.
setVariable
(
BR
.
position
,
position
);
binding
.
executePendingBindings
();
}
}
}
\ No newline at end of file
app/src/main/java/com/example/zxa01/iotclient/home/device/DeviceFragment.java
→
app/src/main/java/com/example/zxa01/iotclient/home/device/
view/
DeviceFragment.java
View file @
b010ef94
package
com.example.zxa01.iotclient.home.device
;
package
com.example.zxa01.iotclient.home.device
.view
;
import
android.content.Context
;
import
android.databinding.DataBindingUtil
;
...
...
@@ -7,17 +7,18 @@ import android.os.Bundle;
import
android.support.v4.app.Fragment
;
import
android.support.v4.app.FragmentTransaction
;
import
android.support.v7.widget.LinearLayoutManager
;
import
android.support.v7.widget.RecyclerView
;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
com.example.zxa01.iotclient.R
;
import
com.example.zxa01.iotclient.databinding.FragmentDeviceBinding
;
import
com.example.zxa01.iotclient.home.device.viewModel.DevicesViewModel
;
import
com.example.zxa01.iotclient.home.device.view.create.DeviceCreateFragment
;
public
class
DeviceFragment
extends
Fragment
{
private
FragmentDeviceBinding
binding
;
private
Device
ViewModel
viewModel
=
new
Device
ViewModel
();
private
Device
sViewModel
viewModel
=
new
Devices
ViewModel
();
public
DeviceFragment
()
{
}
...
...
@@ -26,19 +27,12 @@ public class DeviceFragment extends Fragment {
return
new
DeviceFragment
();
}
@Override
public
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
}
@Override
public
View
onCreateView
(
LayoutInflater
inflater
,
ViewGroup
container
,
Bundle
savedInstanceState
)
{
binding
=
DataBindingUtil
.
inflate
(
inflater
,
R
.
layout
.
fragment_device
,
container
,
false
);
binding
=
DataBindingUtil
.
inflate
(
inflater
,
R
.
layout
.
fragment_device
,
container
,
false
);
binding
.
setViewModel
(
viewModel
);
refreshDevices
(
binding
.
deviceRecyclerView
);
setupDialog
(
binding
.
fab
);
setup
();
return
binding
.
getRoot
();
}
...
...
@@ -52,20 +46,14 @@ public class DeviceFragment extends Fragment {
super
.
onDetach
();
}
public
interface
OnFragmentInteractionListener
{
void
onDeviceFragment
(
Uri
uri
);
}
private
void
refreshDevices
(
RecyclerView
view
){
view
.
setLayoutManager
(
new
LinearLayoutManager
(
getActivity
()));
view
.
setAdapter
(
new
DeviceAdapter
(
viewModel
.
getDevices
()));
}
private
void
setupDialog
(
View
view
){
view
.
setOnClickListener
(
item
->
drawDialog
());
private
void
setup
()
{
binding
.
fab
.
setOnClickListener
(
item
->
drawDialog
());
binding
.
deviceRecyclerView
.
setLayoutManager
(
new
LinearLayoutManager
(
getActivity
()));
viewModel
.
refreshDevices
();
viewModel
.
getDevices
().
observe
(
this
,
deviceList
->
viewModel
.
setAdapter
(
deviceList
));
}
private
void
drawDialog
(){
private
void
drawDialog
()
{
FragmentTransaction
fragmentTransaction
=
getFragmentManager
().
beginTransaction
();
Fragment
fragment
=
getFragmentManager
().
findFragmentByTag
(
String
.
valueOf
(
R
.
string
.
dialog
));
if
(
fragment
!=
null
)
{
...
...
@@ -75,4 +63,8 @@ public class DeviceFragment extends Fragment {
new
DeviceCreateFragment
().
show
(
fragmentTransaction
,
String
.
valueOf
(
R
.
string
.
dialog
));
}
public
interface
OnFragmentInteractionListener
{
void
onDeviceFragment
(
Uri
uri
);
}
}
app/src/main/java/com/example/zxa01/iotclient/home/device/DeviceCreateFragment.java
→
app/src/main/java/com/example/zxa01/iotclient/home/device/
view/create/
DeviceCreateFragment.java
View file @
b010ef94
package
com.example.zxa01.iotclient.home.device
;
package
com.example.zxa01.iotclient.home.device
.view.create
;
import
android.app.AlertDialog
;
import
android.app.Dialog
;
...
...
@@ -13,6 +13,7 @@ import android.view.ViewGroup;
import
com.example.zxa01.iotclient.R
;
import
com.example.zxa01.iotclient.databinding.FragmentDeviceCreateBinding
;
import
com.example.zxa01.iotclient.home.device.viewModel.DevicesViewModel
;
public
class
DeviceCreateFragment
extends
DialogFragment
{
...
...
@@ -39,7 +40,7 @@ public class DeviceCreateFragment extends DialogFragment {
)
.
setPositiveButton
(
R
.
string
.
button_correct
,
(
dialog
,
whichButton
)
->
{
new
DeviceViewModel
().
createDevice
(
"address"
);
new
Device
s
ViewModel
().
createDevice
(
"address"
);
}
)
.
setNegativeButton
(
R
.
string
.
button_cancel
,
(
dialog
,
whichButton
)
->
{
...
...
app/src/main/java/com/example/zxa01/iotclient/home/device/viewModel/DevicesViewModel.java
0 → 100644
View file @
b010ef94
package
com.example.zxa01.iotclient.home.device.viewModel
;
import
com.example.zxa01.iotclient.R
;
import
com.example.zxa01.iotclient.home.device.model.DeviceModel
;
import
com.example.zxa01.iotclient.home.device.view.DeviceAdapter
;
import
com.example.zxa01.iotclient.home.device.model.DevicesModel
;
import
com.example.zxa01.iotclient.pojo.Device
;
import
android.arch.lifecycle.MutableLiveData
;
import
android.arch.lifecycle.ViewModel
;
import
android.databinding.ObservableArrayList
;
import
android.databinding.ObservableBoolean
;
import
android.databinding.ObservableList
;
import
android.util.Log
;
import
java.util.List
;
import
retrofit2.Call
;
import
retrofit2.Response
;
public
class
DevicesViewModel
extends
ViewModel
{
public
ObservableList
<
Device
>
mDevices
=
new
ObservableArrayList
<>();
public
ObservableBoolean
isLoading
=
new
ObservableBoolean
(
false
);
private
DevicesModel
devicesModel
=
new
DevicesModel
();
private
DeviceAdapter
adapter
=
new
DeviceAdapter
(
R
.
layout
.
recycler_view_device
,
this
);
/**
* model
*/
public
void
refreshDevices
()
{
devicesModel
.
fetchDevices
();
}
public
MutableLiveData
<
List
<
DeviceModel
>>
getDevices
()
{
return
devicesModel
.
getDevices
();
}
public
void
createDevice
(
String
address
)
{
devicesModel
.
createDevice
(
address
);
this
.
refreshDevices
();
}
/**
* child model
*/
public
DeviceModel
getDeviceAt
(
Integer
index
)
{
if
(
devicesModel
.
getDevices
().
getValue
()
!=
null
&&
index
!=
null
&&
devicesModel
.
getDevices
().
getValue
().
size
()
>
index
)
{
return
devicesModel
.
getDevices
().
getValue
().
get
(
index
);
}
return
null
;
}
/**
* adapter
*/
public
DeviceAdapter
getAdapter
()
{
return
adapter
;
}
public
void
setAdapter
(
List
<
DeviceModel
>
deviceList
)
{
this
.
adapter
.
setDevices
(
deviceList
);
this
.
adapter
.
notifyDataSetChanged
();
}
}
app/src/main/java/com/example/zxa01/iotclient/home/service/CommonService.java
deleted
100644 → 0
View file @
f055d88e
package
com.example.zxa01.iotclient.home.service
;
public
class
CommonService
{
public
void
createDevice
(
String
address
){
}
public
void
queryDevices
(){
}
public
void
connectGateway
(
String
address
){
}
}
app/src/main/java/com/example/zxa01/iotclient/http/Api.java
0 → 100644
View file @
b010ef94
package
com.example.zxa01.iotclient.http
;
import
com.example.zxa01.iotclient.home.device.model.DeviceModel
;
import
com.example.zxa01.iotclient.home.device.model.DevicesModel
;
import
com.google.gson.Gson
;
import
com.google.gson.GsonBuilder
;
import
okhttp3.OkHttpClient
;
import
retrofit2.Call
;
import
retrofit2.Retrofit
;
import
retrofit2.converter.gson.GsonConverterFactory
;
import
retrofit2.http.GET
;
public
class
Api
{
private
static
ApiInterface
api
;
private
static
final
String
BASE_URL
=
"https://dog.ceo"
;
public
static
ApiInterface
getApi
()
{
if
(
api
==
null
)
{
OkHttpClient
client
=
new
OkHttpClient
.
Builder
()
.
build
();
Gson
gson
=
new
GsonBuilder
()
.
create
();
Retrofit
retrofit
=
new
Retrofit
.
Builder
()
.
baseUrl
(
BASE_URL
)
.
client
(
client
)
.
addConverterFactory
(
GsonConverterFactory
.
create
(
gson
))
.
build
();
api
=
retrofit
.
create
(
ApiInterface
.
class
);
}
return
api
;
}
public
interface
ApiInterface
{
@GET
(
"/api/breeds/list/all"
)
Call
<
Object
>
getDevices
();
// @GET("/api/breed/{breed}/images")
// Call<DogBreedImages> getImagesByBreed(@Path("breed") String breed);
}
}
app/src/main/java/com/example/zxa01/iotclient/http/HttpService.java
0 → 100644
View file @
b010ef94
package
com.example.zxa01.iotclient.http
;
import
java.io.IOException
;
import
okhttp3.Call
;
import
okhttp3.Callback
;
import
okhttp3.MediaType
;
import
okhttp3.OkHttpClient
;
import
okhttp3.Request
;
import
okhttp3.RequestBody
;
import
okhttp3.Response
;
public
class
HttpService
{
public
static
final
MediaType
JSON
=
MediaType
.
parse
(
"application/json; charset=utf-8"
);
public
String
doGetSyncRequest
(
String
url
)
throws
IOException
{
OkHttpClient
client
=
new
OkHttpClient
();
Request
request
=
new
Request
.
Builder
()
.
url
(
url
)
.
build
();
Call
call
=
client
.
newCall
(
request
);
Response
mResponse
=
call
.
execute
();
if
(
mResponse
.
isSuccessful
())
{
return
mResponse
.
body
().
string
();
}
else
{
throw
new
IOException
(
"Unexpected code "
+
mResponse
);
}
// call.enqueue(new Callback(){
//
// @Override
// public void onFailure(Call call, IOException e) {
//
// }
//
// @Override
// public void onResponse(Call call, Response response) throws IOException {
// System.out.println(response);
// }
// });
// return client.newCall(
// new Request.Builder()
// .url(HttpUrl.parse(url).newBuilder().build().toString())
// .build()
// ).execute().body().string();
}
public
String
doPostRequest
(
String
url
,
String
json
)
throws
IOException
{
OkHttpClient
client
=
new
OkHttpClient
();
return
client
.
newCall
(
new
Request
.
Builder
()
.
url
(
url
)
.
post
(
RequestBody
.
create
(
JSON
,
json
))
.
build
()
).
execute
().
body
().
string
();
}
}
app/src/main/java/com/example/zxa01/iotclient/pojo/Device.java
View file @
b010ef94
package
com.example.zxa01.iotclient.pojo
;
public
class
Device
{
import
android.databinding.BaseObservable
;
import
android.databinding.Bindable
;
public
class
Device
extends
BaseObservable
{
private
int
id
;
private
String
name
;
private
String
subTitle
;
...
...
@@ -19,6 +23,7 @@ public class Device {
}
@Bindable
public
int
getId
()
{
return
id
;
}
...
...
@@ -27,6 +32,7 @@ public class Device {
this
.
id
=
id
;
}
@Bindable
public
String
getName
()
{
return
name
;
}
...
...
@@ -35,6 +41,7 @@ public class Device {
this
.
name
=
name
;
}
@Bindable
public
String
getSubTitle
()
{
return
subTitle
;
}
...
...
@@ -43,6 +50,7 @@ public class Device {
this
.
subTitle
=
subTitle
;
}
@Bindable
public
String
getType
()
{
return
type
;
}
...
...
@@ -51,6 +59,7 @@ public class Device {
this
.
type
=
type
;
}
@Bindable
public
boolean
isStatus
()
{
return
status
;
}
...
...
app/src/main/res/layout/fragment_device.xml
View file @
b010ef94
...
...
@@ -5,35 +5,35 @@
<data>
<variable
name=
"viewModel"
type=
"com.example.zxa01.iotclient.home.device.DeviceViewModel"
/>
<variable
name=
"viewModel"
type=
"com.example.zxa01.iotclient.home.device.viewModel.DevicesViewModel"
/>
</data>
<FrameLayout
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
tools:context=
".home.device.DeviceFragment"
>
<android.support.v7.widget.RecyclerView
android:id=
"@+id/device_recycler_view"
android:layout_width=
"wrap_content"
<FrameLayout
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:paddingTop=
"@dimen/padding"
android:clipToPadding=
"false"
tools:listitem=
"@layout/recycler_view_device"
/>
<android.support.design.widget.FloatingActionButton
android:id=
"@+id/fab"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"bottom|end"
android:layout_margin=
"@dimen/margin"
android:src=
"@drawable/ic_add_black_24dp"
app:backgroundTint=
"@color/colorPrimary"
/>
</FrameLayout>
tools:context=
".home.device.view.DeviceFragment"
>
<android.support.v7.widget.RecyclerView
android:id=
"@+id/device_recycler_view"
android:layout_width=
"wrap_content"
android:layout_height=
"match_parent"
android:clipToPadding=
"false"
android:paddingTop=
"@dimen/padding"
app:setAdapter=
"@{viewModel.getAdapter()}"
tools:listitem=
"@layout/recycler_view_device"
/>
<android.support.design.widget.FloatingActionButton
android:id=
"@+id/fab"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"bottom|end"
android:layout_margin=
"@dimen/margin"
android:src=
"@drawable/ic_add_black_24dp"
app:backgroundTint=
"@color/colorPrimary"
/>
</FrameLayout>
</layout>
app/src/main/res/layout/fragment_device_create.xml
View file @
b010ef94
...
...
@@ -5,7 +5,7 @@
<FrameLayout
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
tools:context=
".home.device.DeviceCreateFragment"
>
tools:context=
".home.device.
view.create.
DeviceCreateFragment"
>
<LinearLayout
android:layout_width=
"match_parent"
...
...
app/src/main/res/layout/recycler_view_device.xml
View file @
b010ef94
<android.support.constraint.ConstraintLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:app=
"http://schemas.android.com/apk/res-auto"
xmlns:tools=
"http://schemas.android.com/tools"
android:id=
"@+id/device_layout"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_margin=
"@dimen/margin_sm"
android:background=
"@drawable/shadow"
android:elevation=
"2dp"
android:padding=
"@dimen/margin"
>
<ImageView
android:id=
"@+id/image_device"
android:layout_width=
"50dp"
android:layout_height=
"50dp"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
android:src=
"@drawable/ic_settings_remote_black_24dp"
tools:src=
"@tools:sample/avatars"
/>
<TextView
android:id=
"@+id/text_device_name"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginStart=
"16dp"
android:layout_marginBottom=
"8dp"
android:textColor=
"@color/colorGray"
android:textSize=
"20sp"
app:layout_constraintBottom_toTopOf=
"@+id/text_device_subtitle"
app:layout_constraintStart_toEndOf=
"@+id/image_device"
app:layout_constraintTop_toTopOf=
"@+id/image_device"
tools:text=
"device name"
/>
<TextView
android:id=
"@+id/text_device_subtitle"
android:layout_width=
"285dp"
android:layout_height=
"20dp"
android:layout_marginStart=
"16dp"
android:layout_marginEnd=
"8dp"
app:layout_constraintBottom_toBottomOf=
"@+id/image_device"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintHorizontal_bias=
"0.050"
app:layout_constraintStart_toEndOf=
"@+id/image_device"
tools:text=
"subtitle"
/>
<TextView
android:id=
"@+id/text_device_status"
android:layout_width=
"wrap_content"
xmlns:tools=
"http://schemas.android.com/tools"
>
<data>
<variable
name=
"position"
type=
"java.lang.Integer"
/>
<variable
name=
"viewModel"
type=
"com.example.zxa01.iotclient.home.device.viewModel.DevicesViewModel"
/>
</data>
<android.support.constraint.ConstraintLayout
android:id=
"@+id/device_layout"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_marginEnd=
"8dp"
android:textSize=
"12sp"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintTop_toTopOf=
"@+id/image_device"
tools:text=
"connection"
/>
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
android:layout_margin=
"@dimen/margin_sm"
android:background=
"@drawable/shadow"
android:elevation=
"2dp"
android:padding=
"@dimen/margin"
>
<ImageView
android:id=
"@+id/image_device"
android:layout_width=
"50dp"
android:layout_height=
"50dp"
android:src=
"@drawable/ic_settings_remote_black_24dp"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
tools:src=
"@tools:sample/avatars"
/>
<TextView
android:id=
"@+id/text_device_name"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginStart=
"16dp"
android:layout_marginBottom=
"8dp"
android:textColor=
"@color/colorGray"
android:textSize=
"20sp"
app:layout_constraintBottom_toTopOf=
"@+id/text_device_subtitle"
app:layout_constraintStart_toEndOf=
"@+id/image_device"
app:layout_constraintTop_toTopOf=
"@+id/image_device"
android:text=
"@{viewModel.getDeviceAt(position).device.name}"
tools:text=
"device name"
/>
<TextView
android:id=
"@+id/text_device_subtitle"
android:layout_width=
"285dp"
android:layout_height=
"20dp"
android:layout_marginStart=
"16dp"
android:layout_marginEnd=
"8dp"
app:layout_constraintBottom_toBottomOf=
"@+id/image_device"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintHorizontal_bias=
"0.050"
app:layout_constraintStart_toEndOf=
"@+id/image_device"
android:text=
"@{viewModel.getDeviceAt(position).device.subTitle}"
tools:text=
"subtitle"
/>
<TextView
android:id=
"@+id/text_device_status"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginEnd=
"8dp"
android:textSize=
"12sp"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintTop_toTopOf=
"@+id/image_device"
tools:text=
"connection"
/>
</android.support.constraint.ConstraintLayout>
</layout>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment